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

#************************************************************************
#   NAME
#       stdRoot  -  check/install standard files
#
#   SYNOPSIS
#       stdRoot  [INSTALL] [template_name]
#
#   DESCRIPTION
#   stdRoot builds a configuration database by successively reading:
#
#     1) /vlt/System/templates/Config-Files
#     2) ./Config-Files
#     3) ./Config-Files.`hostname`
#
#   What written in 3 overwrites 2 that overwrites 1, allowing 
#   tailoring the actions to be executed on a machine per machine basis.
#
#   Every line of these files have the following format:
#      "template_name"
#      "target_name"
#      "target_directory"
#      "protection"
#      "owner"
#      "group"
#      "type"         possible values: FCCS,CCSL,NOCCS 
#      "action"       to be executed. The system search an executable
#                     in the following order:
#                        ../actions/"action".`hostname`
#                        ../actions/"action"    
#                        /vlt/System/actions/"action"
#      "parameters"   additional ones to be passed to the action
#
#   Another part of the database is the "type" of the machine.
#   This is obtained from ./hosts.conf.dat. Only the templates that
#   matches the "type" of the machine are used.
#
#   Once obtained the database, each line is processed as follows:
#
#    - find the appropriate action to be performed. The search order is:
#        ../actions/<action>.`hostname     
#        ../actions/<action>   
#        /vlt/System/actions/<action>
#
#    - call the selected action and passing as parameters:
#
#         <action>  INSTALL|CHECK  
#                   "template_name"
#                   "target_name"
#                   "target_directory"
#                   "protection"
#                   "owner"
#                   "group"
#                   "parameters"
#
#   If a second optional parameter is specified, the action is 
#   limited to such a file. 
#
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#
#   BUGS
#
#------------------------------------------------------------------------

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

MODE=$1
ONLY_THIS=$2

echo "----------------------------------------------------------------------"
echo "           INSTALLATION/CHECK of VLT SW Standar Files                 "
echo ""
if [ "$MODE" != "INSTALL" ]
then
    MODE=CHECK
    echo "Check only. Do not INSTALL"
else
    echo "INSTALL selected."
fi
echo "----------------------------------------------------------------------"

#
# get the "type" of installation requested
#
#
for i in `cat hosts.conf.dat`
do 
    if [ "$i" = "$HOST" ]
    then 
        read_the_next=true
    else
        if  [ "$read_the_next" = "true" ]
        then
            TYPE=$i
            read_the_next=false
        fi
    fi
done

echo " HOST = $HOST       TYPE = $TYPE  "

#
# handling of several databases: the possible files are read starting from
# the one with lowest priority. Then the full list is sorted out to eliminate
# multiple entry. The sorting is such that only the last line is kept,
# therefore giving priority to what is written in the last read file.
#

tmp=/tmp/std_$$
touch $tmp

if [ -f /vlt/System/templates/Config-Files ]
then
    cat /vlt/System/templates/Config-Files >> $tmp
fi 

if [ -f ./Config-Files ]
then
    cat ./Config-Files >> $tmp
fi 

if [ -f ./Config-Files.${HOST} ]
then
    cat ./Config-Files.${HOST} >> $tmp
fi

if [ -s $tmp ]
then
    # ... sort the file. If multiple entries, only the last is kept!
    sort -r -k 1,1 ${tmp} | \
    awk '{ if ( $1 == "#" ) \
               { print $0 ; last = $1 } \
           else \
               { current= $1 ; \
               if ( current != last)\
               print $0 " EndOfLine";\
               last = current } \
         } ' | sort  >  ${tmp}.Config-Files
else 
    # no template available
    echo "no configuration files (Config-Files) available. EXIT "
    exit
fi

#rm  $tmp

#
# eliminate the actions that do not apply to the current machine
#
grep $TYPE ${tmp}.Config-Files > ${tmp}.list
#rm  ${tmp}.Config-Files

#
# read the data base ad perform the actions as required:
#
field=template_name
PARAMETER=""

for i in `cat  ${tmp}.list `
do
    if [ "$i" = "EndOfLine" ]
    then
        # look for the action file
        # execute the action
        ACTION_FILE=""
        if [ -f ../actions/${ACTION}.${HOST} ]
        then 
            ACTION_FILE=../actions/${ACTION}.${HOST}
        else
            if [ -f ../actions/${ACTION} ]
            then 
                ACTION_FILE=../actions/${ACTION}
            else
                if [ -f /vlt/System/actions/${ACTION} ]
                then 
                    ACTION_FILE=/vlt/System/actions/${ACTION}
                fi
            fi
        fi

        if [ "$ONLY_THIS" = "" -o "$ONLY_THIS" = "$TEMPLATE_NAME" ]
        then 
            if [ "$ACTION_FILE" != "" ]
            then
            # execute the action
                $ACTION_FILE            \
                         $MODE          \
                         $TEMPLATE_NAME \
                         $TARGET_FILE   \
                         $TARGET_DIR    \
                         $PERMISSION    \
                         $OWNER         \
                         $GROUP         \
                         $PARAMETER
            else
                echo " ERROR: No action found for $TEMPLATE_NAME"
            fi
        fi
        #
        # reset the index for the next record.
        field=template_name
        PARAMETER=""

    else
        case $field in
        template_name)
            TEMPLATE_NAME=$i
            field=target_file
            ;;
        target_file)
            TARGET_FILE=$i
            field=target_dir
            ;;
        target_dir)
            TARGET_DIR=$i
            field=permission
            ;;
        permission)
            PERMISSION=$i
            field=owner
            ;;
        owner)
            OWNER=$i
            field=group
            ;;
        group)
            GROUP=$i
            field=type
            ;;
        type)
            # ignore this field
            field=action
            ;;
        action)
            ACTION=$i
            field=parameter
            ;;
        parameter)
            PARAMETER="$PARAMETER $i"
            # keep storing parameters until EndOfLine
            ;;
        *)
            echo "error in procedure"
            exit 2
            ;;
        esac
    fi
done

#rm ${tmp}.list

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

exit

#___oOo___
