#!/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

# Syntax: StartOLB [-c cfile] [-d] [-D] [-m] [-s] [oth] [-v] [ver]

# Where:  -c  specifies the configuration file to be used.
#         -d  passes the -d option to olbd
#         -D  turns on internal debugging.
#         -m  specifies manager mode (default in StartOLB.cf)
#         -s  specifies server  mode (default in StartOLB.cf)
#         -t  types the commands and does not execute them
#         -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
#             $OLBBASE/<ver>/bin, the default is prod.
#
# Set default values
#
. $0.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
       /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 ..."
    if test -r $2 ; then
       if test ! -w $2 ; then
          Notify "$1 $2 is not writable by $MYNAME."
       fi
    fi
    }
  
Wait4File () {
    Debug "Checking file $2 ..."
    tcnt=$count
    until test -${1} $2 -o $tcnt -eq 0; do
          /bin/echo StartOLB: 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 StartOLB: $1
    exit 4
    }

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

# Pick up options
#
HAVEVER=
OLBOPTS=
OLBPARM=$*
OLBMS=0

while test -n "$1"; do
     isopt=0
     case $1 in [-]*) isopt=1;
     esac
     if [ "$isopt" = "0" -a -z "$HAVEVER" ]; then
        OLBVER=$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
        OLBCONFIGFN=$2
        shift
   elif [ "$1" = "-D" ]; then
        set -x
   elif [ "$1" = "-m" ]; then
        if [ "$OLBMS" = "1" ]; then
           OLBMODE="-m -s"
           OLBTYPE='supervisor'
           else
           OLBMODE="-m"
           OLBTYPE='manager'
        fi
        OLBMS=1
   elif [ "$1" = "-n" ]; then
        shift
        OLBOPTS="$OLBOPTS -n $1"
   elif [ "$1" = "-s" ]; then
        if [ "$OLBMS" = "1" ]; then
           OLBMODE="-m -s"
           OLBTYPE='supervisor'
           else
           OLBMODE="-s"
           OLBTYPE='server'
        fi
        OLBMS=1
   elif [ "$1" = "-t" ]; then
        TEST=echo
   elif [ "$1" = "-v" ]; then
        VERBOSE=1
   else
        OLBOPTS="$OLBOPTS $1"
     fi
   shift
   done

# Establish location of the OLB executables
#
  OLBBASE=`(cd $OLBBASE/$OLBVER; pwd)`
  if [ $? != 0 ]; then
     exit 4
  fi

  OLBPROG=$OLBBASE/bin/$OLBPROG
  PROGRAM=$OLBBASE/etc/$PROGRAM
  if [ "$OLBCONFIGFN" = "" ]; then
     OLBCONFIGFN=$OLBBASE/etc/$OLBCONFIG
  fi

# Establish log file name
#
  OLBLOGFILE=$OLBLOGDIR/$OLBLOGFN
  if [ "x$OLBMODE" = "x-m -s" ]; then
     OLBLOGFILE=$OLBLOGFILE.super
  fi

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

# Verify that all required directories are present
#
for FN in $OLBBASE $OLBLOGDIR $OLBHOMEDIR
    do
    MustExist Directory d $FN
done

# Verify that all owned directories are writable
#
for FN in $OLBLOGDIR $OLBHOMEDIR
    do
    Writable Directory $FN
done

# Verify that we can overwrite the pid file
#
Writable File $OLBPIDFILE
  
# Verify that all required readable files are present
#
for FN in $OLBCONFIGFN
    do
    Wait4File r $FN
done
  
# Verify that all required executable files are present
#
XLIST="$OLBPROG"
for FN in $XLIST
    do
    Wait4File x $FN
done

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

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

# Now start the daemon
#
/bin/echo Starting $OLBTYPE OLB ...
$TEST cd $OLBHOMEDIR
$TEST $OLBPROG $OLBOPTS $OLBMODE -l $OLBLOGFILE -c $OLBCONFIGFN &
stat=$?

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