#!/bin/sh
# $Id: lsf_daemons,v 1.12 1999/04/09 19:19:28 georgeg Exp $
#
# Start and stop LSF daemons, Sun HPC 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
    PIDS=`(ps -ef; ps auxww) 2>/dev/null | egrep " lim |/lim | lim$|/lim$|lim.26|lim.27| sbatchd |/sbatchd | sbatchd$|/sbatchd$|sbatchd.real| mbatchd |/mbatchd | mbatchd$|/mbatchd$| res |/res | res$|/res$|res.real"| sed -n "/grep/d;s/^ *root *\([0-9]*\).*/\1/p" | sort -u`
    if [ "$PIDS" != "" ]
    then
        kill $PIDS
    fi

    # specifial clean-up based on SUN's request
    for HPCSHM_FILE in  /tmp/.hpcshm_*
     do
      if [ -f $HPCSHM_FILE ];then 
           PROC_LIST=`/usr/sbin/fuser $HPCSHM_FILE 2>/dev/null`
           if [ "$PROC_LIST" = "" ] ; then
                /bin/rm -f $HPCSHM_FILE
           fi
      fi
     done
}

# 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
	    exit $EXIT_ERR
	fi
	exit $EXIT_NA
        ;;
esac


