#!/bin/sh
# $Id: lsf,v 1.1.1.1 2003/08/11 16:22:48 dawson Exp $
#
# Start and stop LSF daemons, System V / OSF version
# Make sure we're running a shell that understands functions
if test "$SH5" != "/bin/sh5" -a -r /bin/sh5
then
    SH5=/bin/sh5
    export SH5
    exec /bin/sh5 "$0" "$@"
fi


kill_daemons() {
    # make sure all old daemons are dead first
    # This would be "/etc/killall lim res sbatch mbatchd" if everyone had
    # killall
    # On CRAY, the command to detect processes is different, so treat it apart
    MACHINETYPE=`uname -a | cut -d" " -f 5`
    if [ "$MACHINETYPE" = "CRAY" ]; then
        PIDS=`(ps -ef; ps auxww) 2>/dev/null | egrep ".*[/\[ \t]lim[] \t]*$|.*[/\[ \t]res[] \t]*$|.*[/\[ \t]mbatchd[] \t]*$|.*[/\[ \t]sbatchd[] \t]*$"| sed -n "/grep/d;s/^ *root *\([0-9]*\).*/\1/p" | sort -u`
    else
        PIDS=`(ps -ef; ps auxww) 2>/dev/null | egrep " lim |/lim | lim$|/lim$| sbatchd |/sbatchd | sbatchd$|/sbatchd$| mbatchd |/mbatchd | mbatchd$|/mbatchd$| res |/res | res$|/res$"| sed -n "/grep/d;s/^ *root *\([0-9]*\).*/\1/p" | sort -u`
    fi
    if [ "$PIDS" != "" ]
    then
        kill $PIDS
    fi
}

# On hp10, the correct exit status must be returned
EXIT_OK=0
EXIT_ERR=1  # Failed to startup LSF
EXIT_NA=2    # LSF not configured

case "$1" in
  'start_msg')
	# Used for hp10
	echo "Starting the LSF subsystem"
	exit $EXIT_OK
	;;
  'stop_msg')
	# Used for hp10
	echo "Stopping the LSF subsystem"
	exit $EXIT_OK
	;;
  'stop')
	kill_daemons
	exit $EXIT_OK
        ;;

  *)
        if [ x$LSF_ENVDIR = x ]
	then
	    #       Using default path of lsf.conf...
	    LSF_CONF=/etc/lsf.conf
	else
	    #       Using modified path of lsf.conf...
            LSF_CONF=$LSF_ENVDIR/lsf.conf
	fi

	if [ -f $LSF_CONF ]
	then
	    kill_daemons

	    # Get the location of the LSF daemons
	    . $LSF_CONF
            # Export this env.variable to notify LSF daemons the loc. of
	    # lsf.conf
            export LSF_ENVDIR

	    # If they are really there, start them
	    if [ -f $LSF_SERVERDIR/lim -a -f $LSF_SERVERDIR/res -a -f $LSF_SERVERDIR/sbatchd ]
	    then
		$LSF_SERVERDIR/lim;	echo ' lim\c'  > /dev/console
		$LSF_SERVERDIR/res; echo ' res\c'  > /dev/console
		$LSF_SERVERDIR/sbatchd; echo ' sbatchd\c'  > /dev/console
	        exit $EXIT_OK
	    fi
	    if [ "$LSF_DB_HOST" = `hostname` ]; then 
		$LSF_DB_CONFDIR/bin/lsdbserver start; echo 'lsdbserver start' > /dev/console 
            fi
	    exit $EXIT_ERR
	fi
	exit $EXIT_NA
        ;;
esac