#! /bin/sh
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: lcumodCreateEnv,v 1.2 1999/04/22 14:52:51 vltsccm Exp $"
#
# who       when      what
# --------  --------  ----------------------------------------------
# bgustafs  23/04/96  modified header
# bgustafs  10/08/95  created
#

#************************************************************************
#   NAME
#   lcumodCreateEnv - create the files needed by lcumod in a LCU environment 
#   
#   SYNOPSIS
#   lcumodCreateEnv <lcuenv>
# 
#   DESCRIPTION
#   Create all the files needed by lcumod in an LCU environment.
#   The environment must be created already by vccEnv.
#   lcumodCreateEnv updates the database, the process table and 
#   the devicesFile with the necessary entries for the lcumod module.
#   The following files are affected:
#
#     $INTROOT/ ($VLTROOT)
#       |
#       |--ENVIRONMENTS/
#       |     |--<lcuenv>
#       |     :
#       |
#       |--vw/BOOT/
#       |        |--<lcuenv>/
#       |        |    |--dbl/
#       |        |    |    |--DATABASE.db
#       |        |    | 
#       |        |    |-- bootTime
#       |        |    |-- devicesFile
#       :        :     
#           
#   Each file is created from a template available in $VLTROOT.          
#    
#   <lcuenv>  name of the lcu environment. Normally is l<host>.
#             The environment shall not be already defined.
#
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   CAUTIONS
#
#   EXAMPLES
#
#       $ lcumodCreateEnv lte8
#
#   SEE ALSO
#
#   BUGS     
#
#------------------------------------------------------------------------
#


if [ $# -ne 1 ]
then
    echo ""
    echo "    Usage: lccCreateEnv <lcuenv>"
    echo ""
    exit 1
fi

lcuenv=$1

#
# Check that the environment is already existing
#
lcuenvDir=$INTROOT/vw/BOOT/$lcuenv
if [ ! -d $lcuenvDir ]
then
lcuenvDir=$VLTROOT/vw/BOOT/$lcuenv
fi

if [ ! -d $lcuenvDir ]
then
    echo ""
    echo "   lcumodCreateEnv: ERROR - cannot create."
    echo "           >>$lcuenvDir<< does not exist."
    echo ""
    exit 1
fi

#
# add lcumodServer to the process table 
#
file=$INTROOT/ENVIRONMENTS/$lcuenv
if [ ! -w $file ]
then
    file=$VLTROOT/ENVIRONMENTS/$lcuenv
fi
if [ ! -w $file ]
then
    echo ""
    echo "   lcumodCreateEnv: ERROR - cannot write into file >>$file<<."
    echo ""
    exit 1
fi

if grep -q lcumodServer $file
then
    echo ".\c"
else
    echo "lcumodServer" >> $file
fi

#
# create LCC files for lcumod
#

for name in devicesFile
do
    template=$VLTROOT/vw/include/lcumod$name.template
    if [ ! -r $template ]
    then
        template=$INTROOT/vw/include/lcumod$name.template
    fi
    file=$lcuenvDir/$name
#    if rm -f $file
#    then
#        echo " .\c"
#    else
#        echo ""
#        echo "   lcumodCreateEnv: ERROR - cannot remove file:"
#        echo "                 >>$file<<"
#        echo ""
#        exit 1
#    fi
    if cp $template $file
    then 
        echo " .\c"
    else
        echo ""
        echo "   lcumodCreateEnv: ERROR - cannot create file:"
        echo "                 >>$file<<"
        echo ""
        exit 1
    fi
done

file=$lcuenvDir/bootTime
if rm -f $file
then
    echo " .\c"
else
    echo ""
    echo "   lcumodCreateEnv: ERROR - cannot remove file:"
    echo "                 >>$file<<"
    echo ""
    exit 1
fi
if touch $file
then 
    echo " .\c"
    if chmod 666 $file
    then 
        echo " .\c"
    else
        echo ""
        echo "   lcumodCreateEnv: ERROR - cannot set write permission to:"
        echo "                 >>$file<<"
        echo ""
        exit 1
    fi
else
    echo ""
    echo "   lcumodCreateEnv: ERROR - cannot create file:"
    echo "                 >>$file<<"
    echo ""
    exit 1
fi

# 
# Install database
#
template=$VLTROOT/vw/dbl/DATABASE.db
if [ ! -r $template ]
then
    template=$INTROOT/vw/dbl/DATABASE.db
fi
file=$lcuenvDir/dbl/DATABASE.db
if cp $template $file
then 
    echo " .\c"
else
    echo ""
    echo "   lcumodCreateEnv: ERROR - cannot create file:"
    echo "                 >>$file<<"
    echo ""
    exit 1
fi

echo "\n"
# ___oOo___
exeName=$1
