#!/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
#       vccmakeFile  - update or check a file maniipulated by vccmake
#
#   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}  "

#
# remove from the file what vccmake can build from the ACC database:
#
tmp=/tmp/std_$$
awk '{ if ( $1 == "#vccmake::BEGIN" ) \
               { print $0 ; skip = 1 } \
           else \
               if (  $1 == "#vccmake::END" ) \
                   { print $0 ; skip = 0 }   \
               else  \
                   if ( skip == 0 ) { print $0 } \
     } ' ${TARGET_DIR}/${TARGET_FILE}  > $tmp

#
# for the time being, the check is limited to the difference between the
# two files.
#
if ! diff $FROM_FILE ${tmp}
then 
    echo "The variant part of $TEMPLATE_NAME  is different"
else 
    echo "$TEMPLATE_NAME  OK"
fi

rm ${tmp}

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}
    echo " EXECUTE VCCMAKE AGAIN TO FILL THE VARIANT PART !!!!!!!!!"
fi

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

exit
