#!/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
#       updateFile  - update or check a file
#
#   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
#
#   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

echo "----------------------------------------------------------------------"
echo "updateFile: $MODE $TEMPLATE_NAME $TARGET_FILE $TARGET_DIR $PERMISSION $OWNER $GROUP" 

#
# 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 ..... "
    cp $FROM_FILE ${TARGET_DIR}/${TARGET_FILE}
    chown ${OWNER}:${GROUP}  ${TARGET_DIR}/${TARGET_FILE}
    chmod ${PERMISSION} ${TARGET_DIR}/${TARGET_FILE}
    ll ${TARGET_DIR}/${TARGET_FILE}
fi

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

exit
