#!/usr/bin/ksh
##*************************************
##
## E.S.O. - VLT project
##
## "@(#) $Id: rtap,v 1.139 2000/06/20 17:59:47 vltsccm Exp $" 
##
## who      when     what
## -------- -------- ------------------
## awalland 21/04/97 Startup of rtap at boot time
## vltmgr   06/19/98 created for cmm
## ahuxley  17/10/99 totally rewritten - changed to ksh script
##                                       added more rigorous checks and defaults
##                                       log start and stop to ~$RTAPRUNNER/$RTAPENV.bootlog
##                                       made OS-generic
## ahuxley  19/10/99 no RtapLicServer no longer generates an error (HP-CCSlite doesn't have it)
## ahuxley  21/10/99 fixed quoting problem whereby RTAPENV appeared not set, stdin on su's set to 
##                                       /dev/null to prevent vccEnv{Start,Stop} going interactive
## ahuxley  13/12/99 corrected exit code 
## ahuxley  19/01/00 removed spurious '-' in RtapLicServer parameter to '-n' option.
## ahuxley  07/02/00 added '-f <timeout>' option to vccEnvStart to fix boot-time environment
##                                       startup failures
#
#  To install this file on Suns:
#       cp <full_path_of_this_file> /etc/init.d/rtap
#       ln -s /etc/init.d/rtap /etc/rc3.d/S92rtap
#       ln -s /etc/init.d/rtap /etc/rc0.d/K07rtap
#       
#  To install this file on HPs:
#       cp <full_path_of_this_file> /sbin/init.d/rtap
#       ln -s /sbin/init.d/rtap /sbin/rc3.d/S920rtap
#       ln -s /sbin/init.d/rtap /sbin/rc2.d/K079rtap
#
#  To install this file on Redhat 6.* Linux:
#       cp <full_path_of_this_file> /etc/rc.d/init.d/rtap
#       ln -s /etc/rc.d/init.d/rtap /etc/rc.d/rc3.d/S92rtap
#       ln -s /etc/rc.d/init.d/rtap /etc/rc.d/rc2.d/K07rtap  (run level has not been verified - AHU 17/10/99)
#
#  The configuration file read by this script *must* exist and is:
#      HP-UX: /etc/rc.config.d/rtap
#      SunOS: /etc/rtap.conf   
#      Linux: /etc/rtap.conf   
# 
#  It *must* contain definitions for the following:
#      RTAPROOT  (HP-UX only)
#
#  It *should* contain definitions for the following:
#      RTAPRUNNER
#
#  It *should not* contain definitions for the following:
#      RTAPENV 
#      RTAPROOT (SunOS only)
#
#******************************************************

PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH
UNAMES=`uname -s`

case $UNAMES in
    HP-UX) RTAPCONF=/etc/rc.config.d/rtap ;;
    SunOS) RTAPCONF=/etc/rtap.conf ;;
    Linux) RTAPCONF=/etc/rtap.conf ;;
    *)     echo "$0: ERROR: $UNAMES is an unsupported OS"; exit 1 ;;
esac

[ ! -r $RTAPCONF     ] && { echo "$0: ERROR: can't read $RTAPCONF"; exit 1; }
. $RTAPCONF
[ $UNAMES =  HP-UX -a "X$RTAPROOT" =  X ] && { echo "$0: ERROR: RTAPROOT not defined and this is $UNAMES - define it in $RTAPCONF!"; exit 1; }
[ $UNAMES != HP-UX -a "X$RTAPROOT" != X ] && { echo "$0: WARNING: RTAPROOT is defined but this is $UNAMES - undefine it in $RTAPCONF!"; exit 1; }
[ "X$RTAPRUNNER" = X ] && { RTAPRUNNER=vlt; echo "$0: WARNING: RTAPRUNNER not defined in $RTAPCONF, using $RTAPRUNNER."; }
[ "X$RTAPENV"   != X ] && { echo "$0: WARNING: RTAPENV is defined - undefine it in $RTAPCONF! (The correct place to define the default environment is in $RTAPRUNNER's dot files.)"; }
export RTAPROOT RTAPRUNNER

case $1 in
    start_msg) echo "Start default RTAP environment" ;;
    stop_msg)  echo "Stop default RTAP environment" ;;

    start)     [ $UNAMES = SunOS ] && echo "Starting default RTAP environment ... \c"
               #  Only HP-UX FullCCS has RTAP license server - it is not an error for it not to exist.
               [ $UNAMES = HP-UX -a -f $RTAPROOT/bin/RtapLicServer ] && $RTAPROOT/bin/RtapLicServer -n 1
               #  we cannot rely on CCSlite lock files having been removed, therefore we
               #  now remove the lock file - unfortunately, we should be root to be certain we
               #  remove it, but root doesn't know $VLTDATA  or the name of the default 
               #  environment, so we need to get those from $RTAPRUNNER's environment.
               VLTDATA=`su - $RTAPRUNNER -c 'echo $VLTDATA'`
               RTAPENV=`su - $RTAPRUNNER -c 'echo $RTAPENV'`
               [ "X$VLTDATA" = X ] && { echo "$0: ERROR: VLTDATA not defined in $RTAPRUNNER's enviroment"; exit 1; }
               [ "X$RTAPENV" = X ] && { echo "$0: ERROR: RTAPENV not defined in $RTAPRUNNER's enviroment"; exit 1; }
               [ ! -d "$VLTDATA/ENVIRONMENTS" ] && { echo "$0: ERROR: $VLTDATA/ENVIRONMENTS does not exist!"; exit 1; }
               rm -f $VLTDATA/ENVIRONMENTS/$RTAPENV/.*.lock
               #  Now start default environment - note ksh wrapper to avoid shell-specific redirection syntax
               su - $RTAPRUNNER -c 'ksh -c "nohup vccEnvStart -f 60 -e $RTAPENV -h $HOST >>$RTAPENV.bootlog 2>&1"' < /dev/null
               RC=$?
               [ $UNAMES = SunOS ] && echo "done" ;;

    stop)      [ $UNAMES = SunOS ] && echo "Stopping default RTAP environment ... \c"
               #  Note ksh wrapper to avoid shell-specific redirection syntax
               su - $RTAPRUNNER -c 'ksh -c "vccEnvStop -e $RTAPENV >>$RTAPENV.bootlog 2>&1"' < /dev/null
               RC=$?
               [ $UNAMES = SunOS ] && echo "done" ;;

    *)         echo "usage: $0 { start | stop | start_msg | stop_msg }"
               RC=2 ;
esac

#  Assume that if exit code not explicitly set, then error has not occurred.
exit ${RC:-0}
