#! /bin/sh
#-----------------------------------------------------------------     
#     
#  Startup script for LSF.
#     
#  lsfstartup - start up all daemons of LSF on all hosts of the
#  local cluster.
#     
#  Usage: lsfstartup [-f] [-V]
#
#     
#----------------------------------------------------------------     

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 -f 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  Starting up all LIMs ...

lsadmin limstartup 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 
echo $enf "Waiting for Master LIM to start up ..." $enl
until lsid 1>/dev/null 2>&1
do
    echo $enf "." $enl
    sleep 3
done
echo "  Master LIM is ok" 
    
echo Starting up all RESes ...

lsadmin resstartup 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
else 
    echo 
fi

echo Restarting all slave daemons on LSBATCH hosts ...

badmin hstartup 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
else 
    echo ""
fi

echo Done starting up LSF daemons on the local LSF cluster ...

echo ""
exit $error
