#! /bin/sh
#$Id: lsfshutdown,v 5.13 1994/11/04 18:25:36 jwang Exp $
#-----------------------------------------------------------------     
#     
#  Shutdown script for LSF.
#     
#  lsfshutdown - shutdown all daemons of LSF.
#     
#  Usage: lsfshutdown [-V] [-f]
#     
#  The order of the operation is important :
#      sbatchd, res, lim, mbatchd.     
#     
#     
#----------------------------------------------------------------     

usage="Usage: $0 [-V] [-f]"
confirm="Error(s) found in previous operation, continue ? [y/n] "

error=0                        # exit code 

# handle difference between system V and BSD echo
# To echo "foo" with no newline, do
# echo $enf "foo" $enl

if [ "`echo -n`" = "-n" ] ; then
    enf=
    enl="\c"
else
    enf=-n
    enl=
fi

if [ "$LSF_ENVDIR" = "" ] ; then
    if [ -f /etc/lsf.conf ] ; then
        echo $enf "Is /etc/lsf.conf your current installation ? [y/n] y" $enl
        read N
        case "$N" in
            ""| Y* | y*) CONF=/etc/lsf.conf;;
            *) if [ "$N" = "" ] ; then
		   CONF=/etc/lsf.conf
               fi
	       ;;
        esac
    fi

    if [ "$CONF" = "" ] ; then
        while [ 1 ] ; do
            echo $enf "Enter the full pathname of the lsf.conf file" $enl
            read N
            if [ ! -f $N ] ; then
                echo $N is not a file or does not exist.
            else
                CONF=$N
                break;
            fi
        done
    fi
    . $CONF
else
    . $LSF_ENVDIR/lsf.conf
fi

PATH=$PATH:$LSF_BINDIR

if [ $# -gt 1 ] ; then
    echo $usage; exit 1;
fi

# check the option is provided ?
if [ x$1 != x ]; then
    case $1 in
        -f) FORCE=y;;
        -V) echo ""; lshosts -V; echo ""; exit;;
        *)  echo $usage; exit 1;;
    esac
fi

echo Shutting down all slave batch daemons ...

badmin hshutdown all
if [ $? -ne 0 ]; then
    error=1
    echo 
    while [ "$FORCE" != "y" ] ; do
        echo $enf $confirm $enl
	read answer
	case $answer in
	    Y* | y*) break;;
	    N* | n*) exit 1;;
	    *) continue;;
	esac
    done
fi

echo Shutting down all RESes ...

lsadmin resshutdown all 
if [ $? -ne 0 ]; then
    error=1
    echo 
    while [ "$FORCE" != "y" ] ; do
        echo $enf $confirm $enl
	read answer
	case $answer in
	    Y* | y*) break;;
	    N* | n*) exit 1;;
	    *) continue;;
	esac
    done
fi

echo Shutting down all LIMs ...

lsadmin limshutdown all

exit $error
