#!/bin/sh
#
#  $Id$
#
# (C) 2005 by the Board of Trustees of the Leland Stanford, Jr., University
#                          All Rights Reserved
# Produced by Andrew Hanushevsky for Stanford University under contract
#            DE-AC03-76-SFO0515 with the Deprtment of Energy
# Modified by Fabrizio Furano 12/7/2007

# Syntax: StartCMS [-c cfile] [-D] [-t] [-v] [oth]

# Where:  -c  specifies the configuration file to be used.
#         -D  turns on internal debugging.
#         -t  types the commands and does not execute them
#         -v  produces verbose output.
#         oth Any other options to be passed to the executable.

#
# Set default values from the StartXRD.cf file
#
. `dirname $0`/StartXRD.cf

VERBOSE=0

# Set TEST to equal /bin/echo to only display lines to be executed
#
TEST=

umask 002

##############################################################################
#                           s u b r o u t i n e s                            #
##############################################################################

Debug () {
    if test $VERBOSE -eq 1; then
       echo $1
    fi
    }
  
MustExist () {
    Debug "Checking existence of $1 $3 ..."
    if test ! -${2} $3 ; then
       Notify "$1 $3 not found."
    fi
    }

Writable () {
    Debug "Checking writability of $1 $2 ..."
    if test ! -w $2 ; then
       Notify "$1 $2 is not writable by $MYNAME."
    fi
    }
  
Wait4File () {
    Debug "Checking file $2 ..."
    tcnt=$count
    until test -${1} $2 -o $tcnt -eq 0; do
          echo StartCMS: File $2 not found\; waiting $time seconds...
          sleep $time
          tcnt=`/usr/bin/expr $tcnt - 1`
    done
    if [ $tcnt -le 0 ]; then
       Notify "File $2 not found."
    fi
    }

Notify () {
    echo StartCMS: $1
    exit 4
    }

##############################################################################
#                          m a i n   p r o g r a m                           #
##############################################################################

# Pick up options
#
CMSOPTS=
CMSPARM=$*

while test -n "$1"; do
     if [ "$1" = "-c" ]; then
        if [ -z "$2" ]; then
           Notify "Configuration file not specified."
        fi
        if [ ! -r "$2" ]; then
           Notify "Configuration file '$2' not found."
        fi
        CMSCONFIGFN=$2
        shift
   elif [ "$1" = "-D" ]; then
        set -x
   elif [ "$1" = "-t" ]; then
        TEST=echo
   elif [ "$1" = "-v" ]; then
        VERBOSE=1
   else
        CMSOPTS="$CMSOPTS $1"
     fi
   shift
   done

# Establish location of the CMS executables
#
  XRDBASE=`(cd $XRDBASE; pwd)`
  Debug "XRDBASE has been set to '$XRDBASE'"
  if [ $? != 0 ]; then
     exit 4
  fi

  CMSPROG=$XRDBASE/bin/$XRDARCH/cmsd
  XRDLIBBASE=$XRDBASE/lib/$XRDARCH
  PROGRAM=$XRDCFG/$PROGRAM
  if [ "$CMSCONFIGFN" = "" ]; then
     CMSCONFIGFN=$XRDCFG/$XRDCONFIG
  fi

# Establish log file name
#
  CMSLOGFILE=$XRDLOGDIR/$CMSLOGFN

# Verify that we are not executing this script as root (if we are, switch)
#
if [ $MYNAME = root ]; then
   if [ $XRDUSER != root ]; then
      $TEST exec su $XRDUSER -c "$PROGRAM $CMSPARM"
      fi
   elif [ $MYNAME != $XRDUSER ]; then
        Notify "Attempt to start $CMSUSER cmsd as user $MYNAME."
   fi

# Just in case we don't have the basic directories, try to create them
#
$TEST mkdir -m 0744 -p $XRDLOGDIR $CMSHOMEDIR 2> /dev/null

# Verify that all required directories are present
#
for FN in $XRDBASE $XRDLOGDIR $CMSHOMEDIR
    do
    MustExist Directory d $FN
done

# Verify that all owned directories are writable
#
for FN in $XRDLOGDIR $CMSHOMEDIR
    do
    Writable Directory $FN
done
  
# Verify that all required readable files are present
#
for FN in $XRDCFG
    do
    Wait4File r $FN
done
  
# Verify that all required executable files are present
#
XLIST="$CMSPROG"
for FN in $XLIST
    do
   Wait4File x $FN
done

# Export the variables required in the config file
#
$TEST export XRDCFG
$TEST export XRDBASE
$TEST export XRDLIBBASE

# Set appropriate limits
#
$TEST ulimit -n $MAXFD
$TEST ulimit -c unlimited

# Add our "lib" directory to LD_LIBRARY_PATH
#
  CMSLIBBASE=$XRDBASE/lib/$XRDARCH
  if [ -z "$LD_LIBRARY_PATH" ]; then
     LD_LIBRARY_PATH=$CMSLIBBASE
     else
     LD_LIBRARY_PATH=$CMSLIBBASE:$LD_LIBRARY_PATH
  fi
  export LD_LIBRARY_PATH

# Now start the daemon
#
echo Starting cmsd ...
$TEST cd $CMSHOMEDIR
$TEST $CMSPROG $CMSOPTS -l $CMSLOGFILE -c $CMSCONFIGFN &
stat=$?

# Check if we were successful
#
if test $stat -gt 0 ; then
   Notify "$CMSPROG returned a status of ${stat}."
   fi
