#!/bin/sh
#
#  $Id: StartXRD,v 1.10 2005/04/15 18:36:24 abh Exp $
#
# (C) 2003 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

# Syntax: StartXRD [-c cfile] [-D] [-f] [-r] [-s service] [-t] [oth] [-v] [ver]

# Where:  -c  specifies the configuration file to be used.
#         -D  turns on script debugging.
#         -f  does not use an arbitrary port even when it is possible to do so.
#         -r  designates this an a redirecting xrootd. The option is the
#             default if an olb master is enabled (see StartXRD.cf)
#         -s  is the service name whose port number is to be used by xrootd.
#         -t  designates this as a redirection target xrootd. The option is the
#             default if an olb server is enabled (see StartXRD.cf)
#         -v  produces verbose output.
#         oth Any other single letter options to be passed to the executable.

#        ver  is version number to use. This is the string that is put in
#             $XRDBASE/<ver>/bin, the default is prod.
#
# Notes: 1. If both -r and -t are specified, then the xrootd is started as
#           a redirecting supervisor.
# Set default values
#
. $0.cf

VERBOSE=0

# Change TEST to be /bin/echo to see what will be executed
#
TEST=

umask 002

# Determine where the access command resides
#
  if test -x /usr/bin/access; then
     ACCESS=/usr/bin/access
  elif test -x /bin/access; then
     ACCESS=/bin/access
  else
     ACCESS=/usr/local/bin/access
  fi

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

Debug () {
    if test $VERBOSE -eq 1; then
       /bin/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 ..."
    $ACCESS w $2
    if test $? -gt 0 ; 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
          /bin/echo StartXRD: File $2 not found\; waiting $time seconds...
          /bin/sleep $time
          tcnt=`/usr/bin/expr $tcnt - 1`
    done
    if [ $tcnt -le 0 ]; then
       Notify "File $2 not found."
    fi
    }

Notify () {
    /bin/echo StartXRD: $1
    exit 4
    }

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

# Pick up options
#
XRDOPTIONS=
HAVEVER=
XRDPARMS=$*
XRDRT=0
XRDPORT=
XRDPFIX=0
XRDLOGSFX=

while test -n "$1"; do
     isopt=0
     case $1 in [-]*) isopt=1;
     esac
     if [ "$isopt" = "0" -a -z "$HAVEVER" ]; then
        XRDVER=$1
        HAVEVER=1
   elif [ "$1" = "-c" ]; then
        if [ -z "$2" ]; then
           Notify "Configuration file not specified."
        fi
        if [ ! -r "$2" ]; then
           Notify "Configuration file '$2' not found."
        fi
        XRDCONFIGFN=$2
        shift
   elif [ "$1" = "-D" ]; then
        set -x
   elif [ "$1" = "-f" ]; then
        XRDPFIX=1
   elif [ "$1" = "-r" ]; then
        if [ "$XRDRT" = "1" ]; then
           XRDMODE='-r -t'
           XRDTYPE='supervisor'
           else
           XRDMODE='-r'
           XRDTYPE='manager'
        fi
        XRDRT=1
   elif [ "$1" = "-s" ]; then
        if [ -z "$2" ]; then
           Notify "Service not specified."
        fi
        xrd_XRDALT=`/bin/grep $2 /etc/services | /bin/awk '{split($2,x,"/");print x[1]}'`
        if [ -z "$xrd_XRDALT" ]; then
           Notify "Service $2 not found."
        fi
        XRDPORT=$xrd_XRDALT
        XRDLOGSFX=_$2
        shift
   elif [ "$1" = "-t" ]; then
        if [ "$XRDRT" = "1" ]; then
           XRDMODE='-r -t'
           XRDTYPE='supervisor'
           else
           XRDMODE='-t'
           XRDTYPE='server'
        fi
        XRDRT=1
   elif [ "$1" = "-v" ]; then
        VERBOSE=1
   else
        XRDOPTIONS="$XRDOPTIONS $1"
     fi
   shift
   done

# Establish location of the XRD executable and libraries
#
  XRDBASE=`(cd $XRDBASE/$XRDVER; pwd)`
  if [ $? != 0 ]; then
     exit 4
  fi

  XRDPROG=$XRDBASE/bin/$XRDPROG
  XRDLIBBASE=$XRDBASE/lib
  PROGRAM=$XRDBASE/etc/$PROGRAM
  if [ "$XRDCONFIGFN" = "" ]; then
     XRDCONFIGFN=$XRDBASE/etc/$XRDCONFIG
  fi

  XRDLOGFILE=$XRDLOGDIR/${XRDLOGFN}$XRDLOGSFX
  if [ "x$XRDMODE" = "x-r -t" ]; then
     XRDLOGFILE=$XRDLOGFILE.super
  fi

  if [ -z "$LD_LIBRARY_PATH" ]; then
     LD_LIBRARY_PATH=$XRDLIBBASE
     else
     LD_LIBRARY_PATH=$XRDLIBBASE:$LD_LIBRARY_PATH
  fi
  export LD_LIBRARY_PATH

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

# Establish the port number to be used
#
  if [ "$XRDPFIX" = "0" -a "x$XRDPORT" = "x" ]; then
     if [ "x$XRDMODE" = "x-r -t" -o "x$XRDMODE" = "x-t" ]; then
        XRDPORT=any
     fi
  fi
  if [ "x$XRDPORT" != "x" ]; then
     XRDPORT="-p $XRDPORT"
  fi

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

# Verify that all owned directories are writable
#
for FN in $XRDLOGDIR $XRDHOMEDIR
    do
    Writable Directory $FN
done
  
# Verify that all required readable files are present
#
for FN in $XRDCONFIGFN
    do
    Wait4File r $FN
done
  
# Verify that all required executable files are present
#
for FN in $XRDPROG
    do
    Wait4File x $FN
done

# Export required variables
#
$TEST export LD_LIBRARY_PATH

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

# Now start the daemon
#
/bin/echo Starting $XRDTYPE xrootd ...
$TEST cd $XRDHOMEDIR
$TEST $XRDPROG $XRDOPTIONS $XRDMODE $XRDPORT -l $XRDLOGFILE -c $XRDCONFIGFN &
stat=$?

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