#!/bin/sh
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $I>-<d$"
#
# who       when      what
# --------  --------  ----------------------------------------------
# mcarrasc  01/02/99  created
# gfilippi  07/03/99  modified as a single action
#

#************************************************************************
#   NAME
#       installBoot  - install the automatic boot
#
#   SYNOPSIS
#       updateFile 
#           INSTALL|CHECK     select operational mode (CHECK= no changes)
#           TEMPLATE_NAME     template file name. The search order for the 
#                             template is:
#                               ../templates/$TEMPLATE_NAME.`hostname`
#                               ../templates/$TEMPLATE_NAME
#                               /vlt/System/templates/$TEMPLATE_NAME
#           TARGET_FILE       the name of the file to be installed
#           TARGET_DIR        the directory where to install
#           PERMISSION        protection mask for the target file
#           OWNER             UID for the target file
#           GROUP             GUI for the target file
#
#           APPLICATION       rtap|msql|vcsolac|tornado|frame
#           RTAPRUNNER        username to be used to start the RTAP environment
#
#   DESCRIPTION
#
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#
#   BUGS
#
#------------------------------------------------------------------------

date=`date '+%y%m%d'`

MODE=$1
HOST=`hostname`

TEMPLATE_NAME=$2
TARGET_FILE=$3
TARGET_DIR=$4
PERMISSION=$5
OWNER=$6
GROUP=$7
APPLICATION=$8
RTAPRUNNER=$9

echo "----------------------------------------------------------------------"
echo "installBoot: $MODE $TEMPLATE_NAME $TARGET_FILE $TARGET_DIR $PERMISSION $OWNER $GROUP $APPLICATION $RTAPRUNNER" 

#
# Locate the template file
#
if [ -f ../templates/$TEMPLATE_NAME.$HOST ]
then 
    FROM_FILE=../templates/$TEMPLATE_NAME.$HOST

else
    if [ -f ../templates/$TEMPLATE_NAME ]
    then 
        FROM_FILE=../templates/$TEMPLATE_NAME

    else
        if [ -f /vlt/System/templates/$TEMPLATE_NAME ]
        then 
            FROM_FILE=/vlt/System/templates/$TEMPLATE_NAME

        else
            # no template available
            echo "ERROR: no template available"
            echo "----------------------------------------------------------------------"
            exit
        fi
    fi
fi 

echo " file: $FROM_FILE    ---->    ${TARGET_DIR}/${TARGET_FILE}  "

#
# for the time being, the check is limited to the difference between the
# two files. GID,UID and protection still to be checked.
#
if ! diff $FROM_FILE ${TARGET_DIR}/${TARGET_FILE}
then 
    echo "$TEMPLATE_NAME  is different"
else 
    echo "$TEMPLATE_NAME  OK"
fi

if [ "$MODE" = "INSTALL" ]
then
    echo " installing $APPLICATION at boot ..... "
    if [ "$APPLICATION" = "rtap" ]
    then 
        if [ "$RTAPRUNNER" != "" ]
        then
            # use default
            RTAPRUNNER=vlt
        fi
        # edit the file to setup the appropriate username
        rm -f ${TARGET_DIR}/${TARGET_FILE}
        sed 's/RTAPRUNNER=vlt/RTAPRUNNER=$RTAPRUNNER/'  $FROM_FILE > ${TARGET_DIR}/${TARGET_FILE}
    else
        cp $FROM_FILE ${TARGET_DIR}/${TARGET_FILE}
    fi
    chown ${OWNER}:${GROUP}  ${TARGET_DIR}/${TARGET_FILE}
    chmod ${PERMISSION} ${TARGET_DIR}/${TARGET_FILE}
    ll ${TARGET_DIR}/${TARGET_FILE}
    #
    # create the links
    case $APPLICATION in
    rtap)
         S=S120
         K=K910
         ;;
    tornado)
         S=S130
         K=K920
         ;;
    vcsolac)
         S=S170
         K=K810
         ;;
    msql)
         S=S110
         K=K920
         ;;
    frame)
         S=S140
         K=K940
         ;;
    *)
         echo "APPLICATION: >>${APPLICATION}<< unknown"
         ;;
    esac

    ln -s  ${TARGET_DIR}/${TARGET_FILE} /sbin/rc3.d/${S}${APPLICATION}
    ln -s  ${TARGET_DIR}/${TARGET_FILE} /sbin/rc2.d/${K}${APPLICATION}
    ls -la ${TARGET_DIR}/${TARGET_FILE} /sbin/rc3.d/${S}${APPLICATION} /sbin/rc2.d/${K}${APPLICATION}

fi

echo "----------------------------------------------------------------------"

exit
