#!/bin/sh
#
# $Id: lsfsetup,v 1.74.10.1.2.2 1999/03/29 17:32:15 hupeng Exp $
#

# 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


hosttype=`uname`
if [ "$hosttype" = "HP-UX" ] ; then
    version=`uname -a |awk '{split($3, a, "."); print a[2]}'`
fi

# HP-UX 11 needn't this work around

if [ "$version" != "11" ] ; then

    # Work around weird HP-UX 10 POSIX shell problem
    if test "$SH5" != "/usr/old/bin/sh" -a -r /usr/old/bin/sh
    then
	SH5=/usr/old/bin/sh
        export SH5
	exec /usr/old/bin/sh "$0" "$@"
    fi
fi

CP=cp
export CP

# When SETRC=y, hostsetup will try to ask question about set rc 
# Introduce this parameter for rhostsetup. In rhostsetup, when one host
# don't do setup_rc, we needn't ask this question in other host setup 
SETRC=y
export SETRC

EDITOR=${EDITOR:-vi}

if test "$LSF_ENVDIR" = ""
then
    LSF_ENVDIR=/etc
fi

#if [ -f $LSF_ENVDIR/lsf.conf ]
#then
#    . $LSF_ENVDIR/lsf.conf
#fi

if [ -f ./lsfsetup  -a -f ./scripts/funcs.sh ] ; then
    sp=./scripts
elif [ -f $LSF_ENVDIR/lsf.conf ] ; then
    . $LSF_ENVDIR/lsf.conf
    sp=$LSF_SERVERDIR/scripts
else
    echo Unable to figure out the context for setup. You have to run lsfsetup
    echo from the release directory or set the LSF_ENVDIR variable.
    exit
fi

export sp

hosttype=`uname`
if [ $hosttype = 'Windows_NT' ]; then
   LSNULFILE=NUL
   LSHOSTTYPE=NT
   LSEXEDITOR=NUL
   LS_BASE_BATCH_ONLY=NUL
   LSBASEBATCHONLY_NOGUI=NUL
else
   LSNULFILE=/dev/null
   LSHOSTTYPE=UNIX
   LSEXEDITOR="ex -"
   LSBASEBATCHONLY_NOGUI=n
   LS_BASE_BATCH_ONLY=n
fi
export LSNULFILE
export LSHOSTTYPE
if [ $hosttype = 'Linux' ]; then
   LSEXEDITOR="ex -s"
#
# for Red Hat 5.1 users on Oct. 15, 1998
# in the future, it should be "n"
#
   if uname -a | grep alpha > /dev/null 2>&1; then
      LSBASEBATCHONLY_NOGUI=y
      LS_BASE_BATCH_ONLY=y
   fi
fi
export LSEXEDITOR
export LSBASEBATCHONLY_NOGUI
export LS_BASE_BATCH_ONLY

LSF_DEFAULT_INSTALL=n
DEFAULT_ANOTHER=n
export LSF_DEFAULT_INSTALL

FIRST_TIME=
export FIRST_TIME 
if [ "$1" != "-hostsetup" ]; then
    # lsfsetup -hostsetup don't display this copyright information 

    # Display copyright information
cat << COPYRIGHT
    L O A D   S H A R I N G   F A C I L I T Y   V E R S I O N   3 . 2 . 2

COPYRIGHT

    # Use sed to get copyright info from lsf/lsf.h
    if CPR=`sed -e '/LS_VERSION/!d; /DATE/d; s/\\(.*\\)\\\\n.*/\1/; s/^.*\\\\n/    /' lsf/lsf.h 2> $LSNULFILE` && [ "$CPR" != "" ]
    then
        echo "$CPR"
    else
        echo "    Copyright 1992 -- 1999 by Platform Computing Corporation."
    fi
    echo "    All rights reserved."
    echo ""

    echo Initializing ...
fi

. $sp/funcs.sh
. $sp/default.config
. $sp/help
if is_root ; then
    CHOWN=chown
else
    CHOWN=true
fi

# We have our standard function defs now
GOT_FUNCS=y


# We don't have valid lsf.conf info yet
GOT_CONFIG=

if [ "$1" = "-hostsetup" ]; then
     do_hostsetup $2 $3
     exit
fi

while [ 1 ] ;
do
# Do not log this menu interactions because we only need to catch interactions
# for installations, not for config, etc.
    cat << MENU1

HELP:
    If you want help with any of the questions asked, enter ? at the prompt.


Please choose one of the following options:

    1.  Default Install  
    2.  Custom Install
    3.  Host Setup
    4.  License Management
    5.  Upgrade From a Previous Version
    6.  Install Additional Product(s)
    7.  De-install LSF

    ?.  Help
    q.  Quit

MENU1

    echo $enf "Please enter your choice: [1] " $enl
   
    read N
    if [ "$N" = "" ]; then
	N=1
    fi
    case "$N" in
    1|2|5|6) 
    unset GOT_CONFIG
    if [ "$N" != 2 ] ; then 
       # for custom install, we check this in customInstall scripts.
       # This is because when config we needn't  run lsfsetup in release dir   

        if [ ! -d ./examples -o ! -d ./base -o ! -d ./scripts ] 
        then 
            echo "Sorry, you must be in the release directory to install LSF"
	    exit
        fi
    fi
    echo `date` >> $PREFIX_LOG/Install.log
    echo lsfsetup installation started by `who am i` >> $PREFIX_LOG/Install.log
    echo Current dir is `pwd` >> $PREFIX_LOG/Install.log
    ;;
    3|7|4) 
        if test "$GOT_CONFIG" != "y" ; then 
	    # first get an lsf.conf file since we need one
	    find_conf LOCAL
            # reset sp variable, since even LSF_ENVDIR is defined, user
            # maybe changed it in find_conf
	    if [ ! -f ./lsfsetup  -a ! -f ./scripts/funcs.sh ] ; then
                sp=$LSF_SERVERDIR/scripts
            else
		sp=./scripts
            fi
	fi
	# look for the cluster name of this host
	find_cluster
	;;
    *) ;;
    esac

    case "$N" in
    1)
	. $sp/product; product
        . $sp/defInstall; defInstall
	exit
	;;

    2)  . $sp/product
        . $sp/customInstall; customInstall
	exit
	;;

    3)  . $sp/hostsetup; hostsetup; exit ;;

    4)  
        if [ -d $LSF_SERVERDIR ] ; then 
            LICENSE_MANAGER=y
	    . $sp/license; license; exit 
        else
            echo "$LSF_SERVERDIR does not seem to exist."  
            echo "Please run Host Setup first."
        fi
        ;;

	# Modified by Wynstan Tong, Apr 9, Problem 2690
    5)  UPGRADE=y
	export UPGRADE
	. $sp/product; product
        . $sp/upgrade; upgrade
#	check_setuid
#	echo "    Finished upgrading."
	exit
	;;
# add product always need ask user to choose the product 
    6)  FIRST_TIME=y  
	. $sp/product; product
	. $sp/addProduct; addProduct
	exit
	;;
    7)	. $sp/uninstall; exit ;;

    [Qq]*) exit ;;

    *)	more << HELP

    To install LSF for the first time, we recommend you choose Default 
    Install (option 1).  This performs all the installation steps necessary.
    Default Install requires that all LSF machine dependent and independent
    files are installed on a single file server and the installation directory
    is accessible with the same path name from all machines that will run LSF.

    If you have already understood LSF quite well, you can choose Custom
    Install (option 2).  This will give you more chance to choose install 
    parameters and do much more configuration.  

    To set up per host environment for LSF (registering LSF services if
    necessary, configuring host into cluster file and  the system to start 
    the LSF daemons at boot time, and starting daemons), choose Host Setup 
    (option 3).  If you install LSF using Default Install and have privilege 
    to run rsh as root, you can do host setup in one host for all hosts in 
    your cluster using rsh <hostname> -l root lsfsetup -hostsetup 
    <lsf.conf pathname> <clustername>. Otherwise, this step must be done 
    once on each host in the cluster.  If you want more control over how 
    your systems are set up, you can look at the scripts/hostsetup shell 
    script to see what is done in this step.

    To set up the LSF software license, choose License Management
    (option 4).  This option allows you to:
	- get the necessary information to obtain a license key
	- install a license key
	- set up the license server
	- update a license key
    License Management should be done once for the cluster, on the
    license server host.

    To upgrade from an earlier version of LSF, choose Upgrade From a
    Previous Version (option 5).  This should be done on the first host
    in the cluster to upgrade the configuration files and the
    machine-dependent files for that host type, and once on each other
    host type to upgrade the machine-dependent files for the host type.

    To install additional product into your cluster, choose Install 
    Additional Product(s) (option 6). If your new product(s) version
    is different with current installed LSF, you must choose upgrade
    (option 5) to install.

    To remove LSF from a host, choose De-install LSF (option 7).  This
    must done on each host, to clean up the system configuration files
    and shut down the LSF daemons.

    To install for a client-only host (that does not run LSF servers), 
    install server hosts first. Then if the binaries and man pages 
    installed for server machines of the same host type (architecture)
    not accessible from this host, choose Install Additional Product(s)
    to install the missing part. Choose Host Setup option to setup 
    execution environment for this host. You also need to make sure 
    that client-only hosts are also defined in your cluster configuration 
    with SERVER column being 0.

    To see this help information, choose Help (?).

    To return to shell prompt, choose Quit (q).

HELP
        ;;
    esac

    echo $enf "Press Return to continue ..." $enl
    read N
done
