#!/bin/sh
#
# Grid Engine configuration script (Installation/Uninstallation/Upgrade/Downgrade)
# Scriptname: inst_sge
#
# (c) 2004 Sun Microsystems, Inc. Use is subject to license terms.  
#
# set -x

#Reset PATH to a safe value
#
PATH=/bin:/usr/bin:/usr/sbin:/usr/bsd:/usr/ucb

# Easy way to prevent clearing of screen
#
CLEAR=clear
$CLEAR

# The same as clear!
#
ECHO=echo

# Sourcing common function module
#
PWD=`pwd`
. ./util/arch_variables
. ./util/install_modules/inst_common.sh

#---------------------------------------
# commandline argument parsing
#---------------------------------------

UPDATE=false
UPDATEDB=false
DOWNGRADE=false
BACKUP=false
RESTORE=false
AUTO=false
RESCHEDULE=true
RESPORT=false
CSP=false
AFS=false
NOREMOTE=false
START_RPC_SERVICE=false
ADD_TO_RC=false
REMOVE_RC=false
SET_FILE_PERMS=false
MAKE_RC=false

QMASTER="undef"
EXECD="undef"
SHADOW="undef"
BERKELEY="undef"
TAR="undef"
FILE="undef"
HOST="undef"
HOSTRANGE="undef"
LOCAL_EXECD_SPOOL="undef"
SHELL_NAME="ssh"
EXEC_HOST_LIST="undef"
CONFIG_FILE="undef"

SGE_NOMSG="undef"

BasicSettings
SetUpInfoText
CheckBinaries


ARGC=$#

if [ $ARGC = 0 ]; then
   ErrUsage
   exit 1
fi

while [ $ARGC != 0 ]; do
   case $1 in
   -auto)
     AUTO=true
     FILE=$2
     if [ ! -f $2 ]; then
        ErrUsage
     fi
     shift
     ARGC=`expr $ARGC - 1`
     CLEAR=:
     SGE_NOMSG=1
     export SGE_NOMSG
     CreateLog
     exec 1>/tmp/$LOGSNAME 2>&1
     ;;
   -m)
     QMASTER="install"
     . ./util/install_modules/inst_qmaster.sh
     . ./util/install_modules/inst_berkeley.sh
     ;;
   -um)
     QMASTER="uninstall"
     . ./util/install_modules/inst_qmaster_uninst.sh
     ;; 
   -x)
     EXECD="install"
     . ./util/install_modules/inst_execd.sh
     if [ "$2" = "-upd" ]; then
         UPDATE=true
         shift
         ARGC=`expr $ARGC - 1`
     fi
     ;;
   -ux)
     EXECD="uninstall"
     if [ "$2" = "all" ]; then
        ALL_EXECDS=true
        shift
        ARGC=`expr $ARGC - 1`
     fi
     . ./util/install_modules/inst_execd_uninst.sh
     . ./util/install_modules/inst_qmaster.sh
     ;;
   -sm)
     SHADOW="install"
     . ./util/install_modules/inst_qmaster.sh
     #echo Install Shadowhost
     ;;
   -usm)
     SHADOW="uninstall"
     #echo uninstall Shadowhost
     ;;
   -db)
     BERKELEY="install"
     SPOOLING_SERVER=`hostname`
     . ./util/install_modules/inst_qmaster.sh
     . ./util/install_modules/inst_berkeley.sh
     ;;
   -host)
     if [ $AUTO = "false" -a $QMASTER = "install" ]; then
        EXEC_HOST_LIST=$2
     else
        HOST=$2
     fi
     shift
     ARGC=`expr $ARGC - 1`
     ;;
   -bup)
     BACKUP=true
     #do configuration database backup
     ;;
   -rst)
     RESTORE=true
     #restore a backuped database
     ;;
   -upd)
     UPDATE=true
     OLD_SGE_ROOT=$2
     OLD_SGE_CELL=$3
     if [ "$OLD_SGE_ROOT" = "" -o "$OLD_SGE_CELL" = "" -a "$EXECD" != "install" ]; then
        ErrUsage
        exit 1    
     fi
     shift
     ARGC=`expr $ARGC - 1`
     shift
     ARGC=`expr $ARGC - 1`
     QMASTER="install"
     . ./util/install_modules/inst_qmaster.sh
     . ./util/install_modules/inst_berkeley.sh
     . ./util/install_modules/inst_update.sh
     #update from 5.3 to 6.0
     ;;
   -rccreate)
     MAKE_RC=true
     #Generate new rc scripts from settings file
     ;; 
   -updatedb)
     UPDATEDB=true
     . ./util/install_modules/inst_update.sh
     #Needed for 6.0 to 6.0u2 BDB update
     ;;
   -noremote)
     NOREMOTE=true
     #Disable remote installation
     ;;
   -rsh)
     SHELL_NAME=rsh
     ;;
   -nr)
     RESCHEDULE=false
     #echo set reschedule to false
     ;;
   -resport)
     RESPORT=true
     #echo set resport to true
     ;;
   -csp)
     CSP=true
     ;;
   -afs)
     AFS=true
     ;;
   -help)
     ErrUsage
     ;;
   *)
     option=$1
     ErrUsage
     ;;
   esac
   shift
   ARGC=`expr $ARGC - 1`
done

if [ $AUTO = "false" -a $QMASTER = "undef" -a $EXECD = "install" -a $SHADOW = "install" ]; then
   ErrUsage
fi


if [ $UPDATE = "true" -a "$OLD_SGE_ROOT" != "" -a "$OLD_SGE_CELL" != "" ]; then
   WelcomeTheUserUpgrade
   CheckForLocalHostResolving
   CheckWhoInstallsSGE
   ProcessSGERoot 
   GetQmasterPort
   GetExecdPort
   GetCell
   GetQmasterSpoolDir $euid
   SetCellDependentVariables
   SetPermissions
   SelectHostNameResolving
   SetProductMode
   MakeDirsMaster
   SetSpoolingOptions
   AddBootstrap
   InitSpoolingDatabase
   GetConfiguration
   AddActQmaster
   CreateSGEStartUpScripts $euid true master
   CreateSGEStartUpScripts $euid true execd 
   UpdateConfiguration
   CreateSettingsFile
   InitCA
   AddSGEStartUpScript $euid master
   StartQmaster
   AddDefaultQueue
   GetOldScheddConfig
   UpdateHints
fi

if [ $UPDATE = "true" -a $EXECD = "install" ]; then
      WelcomeTheUserExecHost
      CheckForLocalHostResolving
      ProcessSGERoot
      CheckQmasterInstallation
      CheckCellDirectory
      . $SGE_ROOT/$SGE_CELL/common/settings.sh
      CheckCSP
      CheckHostNameResolving
      AddSGEStartUpScript $euid execd
      StartExecd
      AddQueue
      GiveHints
fi
 
if [ $QMASTER = "install" -a $UPDATE != "true" ]; then

 if [ $AUTO = true ]; then
  {
    $INFOTEXT -log "Starting qmaster installation!"
    GetConfigFromFile
    CheckPath
  }
 fi

 WelcomeTheUser
 CheckForLocalHostResolving
 CheckWhoInstallsSGE
 ProcessSGERoot 
 GetQmasterPort
 GetExecdPort
 GetCell
 GetQmasterSpoolDir $euid
 SetCellDependentVariables
 SetPermissions
 SelectHostNameResolving
 SetProductMode
 MakeDirsMaster
 SetSpoolingOptions
 AddBootstrap
 InitSpoolingDatabase
 AddConfiguration
 AddLocalConfiguration
 AddActQmaster
 AddDefaultComplexes
 AddPEFiles
 AddDefaultDepartement
 AddCommonFiles
 CreateSGEStartUpScripts $euid true master
 CreateSGEStartUpScripts $euid true execd 
 CreateSettingsFile
 InitCA
 AddSGEStartUpScript $euid master
 StartQmaster
 AddHosts
 SetScheddConfig
 GiveHints

 if [ $AUTO = true ]; then
    MoveLog
 fi

fi


if [ $EXECD = "install" -a $UPDATE != "true" ]; then
 if [ $AUTO = true ]; then
    GetConfigFromFile
    CheckPath

    for h in $EXEC_HOST_LIST; do
   
      if [ $h = `hostname` ]; then
  
         WelcomeTheUserExecHost
         CheckForLocalHostResolving
         ProcessSGERoot
         CheckQmasterInstallation
         CheckCellDirectory
         CheckCSP
         CheckHostNameResolving
         GetLocalExecdSpoolDir
         AddLocalConfiguration_With_Qconf
         AddSGEStartUpScript $euid execd 
         StartExecd
         AddQueue
         GiveHints
         CheckRunningDaemon sge_execd
         
         if [ $? = 0 ]; then
            $INFOTEXT -log "Execd on host %s is running!\n" $h
         else
            $INFOTEXT -log "Execd on host %s is not started!\n" $h
         fi

         if [ $AUTO = "true" ]; then
            MoveLog
         fi

      else
         if [ $QMASTER = "install" -o $NOREMOTE = "false" ]; then
            $INFOTEXT -log "remote installation on host %s" $h
            echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; cd $SGE_ROOT && ./inst_sge -x -auto $FILE -noremote" | $SHELL_NAME $h /bin/sh &
         fi

      fi

    done
 
 else
      WelcomeTheUserExecHost
      CheckForLocalHostResolving
      ProcessSGERoot
      CheckQmasterInstallation
      CheckCellDirectory
      . $SGE_ROOT/$SGE_CELL/common/settings.sh
      CheckCSP
      CheckHostNameResolving
      GetLocalExecdSpoolDir
      AddLocalConfiguration_With_Qconf
      AddSGEStartUpScript $euid execd
      StartExecd
      AddQueue
      GiveHints
      
      if [ $AUTO = "true" ]; then
         MoveLog
      fi

      if [ -f $EXEC_HOST_LIST ]; then
         for h in `cat $EXEC_HOST_LIST`; do
           if [ $QMASTER = "install" -o $NOREMOTE = "false" ]; then
               $INFOTEXT "Starting remote installation on host %s" $h
               $INFOTEXT "This part runs in automatic mode," \
                         "be sure to have a valid configuration file"
               if [ $CONFIG_FILE = "undef" ]; then
                  $INFOTEXT -n "Please enter the path to your autoinstall configuration file >> " 
                  CONFIG_FILE=`Enter`
                  export CONFIG_FILE
               else               
                  $INFOTEXT -n "Please enter the path to your autoinstall configuration file or\n hit <RETURN> to use [%s] >> " $CONFIG_FILE
                 CONFIG_FILE=`Enter $CONFIG_FILE`
               fi

               $INFOTEXT -n "Please enter which shell you want to use for remote login (rsh/ssh) or\n hit <RETURN> to use [%s] >> " $SHELL_NAME
               SHELL_NAME=`Enter $SHELL_NAME` 
               $INFOTEXT -wait -auto $AUTO -n "Hit <RETURN> to continue >> "
               echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; cd $SGE_ROOT && ./inst_sge -x -auto $CONFIG_FILE -noremote" | $SHELL_NAME $h /bin/sh &
           fi
         done
      else
         if [ $EXEC_HOST_LIST != "undef" ]; then 
            for h in $EXEC_HOST_LIST; do
              if [ $QMASTER = "install" -o $NOREMOTE = "false" ]; then
                  $INFOTEXT "Starting remote installation on host %s" $h
                  $INFOTEXT "This part runs in automatic mode," \
                            "be sure to have a valid configuration file"
                  if [ $CONFIG_FILE = "undef" ]; then
                     $INFOTEXT -n "Please enter the path to your autoinstall configuration file >> " 
                     CONFIG_FILE=`Enter`
                     export CONFIG_FILE
                  else               
                     $INFOTEXT -n "Please enter the path to your autoinstall configuration file or\n hit <RETURN> to use [%s] >> " $CONFIG_FILE
                     CONFIG_FILE=`Enter $CONFIG_FILE`
                  fi
                  $INFOTEXT -n "Please enter which shell you want to use for remote login (rsh/ssh) or\n hit <RETURN> to use [%s] >> " $SHELL_NAME
                  SHELL_NAME=`Enter $SHELL_NAME`
                  $INFOTEXT -wait -auto $AUTO -n "Hit <RETURN> to continue >> "
                  echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; cd $SGE_ROOT && ./inst_sge -x -auto $CONFIG -noremote" | $SHELL_NAME $h /bin/sh &
              fi
         done
         fi
      fi

  fi

 
fi

if [ $BERKELEY = "install" ]; then
 
   BasicSettings
   CheckWhoInstallsSGE
   ProcessSGERoot
   GetCell
   COMMONDIR=$SGE_CELL/common
   Makedir $SGE_CELL
   Makedir $COMMONDIR 
   SetSpoolingOptions
   InstallServerScript $euid
   GiveBerkelyHints

fi


if [ $EXECD = "uninstall" ]; then

   WelcomeUninstall 

   if [ $AUTO = "true" ]; then
      GetConfigFromFile
   fi

   if [ "$SGE_ROOT" = "" -o "$SGE_CELL" = "" ]; then
      $INFOTEXT -wait "Your SGE_ROOT or SGE_CELL variable is not set!\n" \
                "Please, enter SGE_ROOT and SGE_CELL in the following\n" \
                "screens!\n\nHit, <ENTER> to continue!"
      $CLEAR
      ProcessSGERoot

      $INFOTEXT -n "Please enter your SGE_CELL directory or use the default [default] >> "

      SGE_CELL=`Enter default`
      export SGE_CELL
   fi

    . $SGE_ROOT/$SGE_CELL/common/settings.sh

   FetchHostname
   
   if [ $AUTO = "true" ]; then
      MoveLog
   fi
   
   $INFOTEXT "If you have local execution host spool directories,\n" \
             "please remove them by hand!\n\n"

   if [ $QMASTER != "uninstall" ]; then 
      exit 0
   fi

fi


if [ $QMASTER = "uninstall" ]; then
   . $SGE_ROOT/$SGE_CELL/common/settings.sh
   RemoveQmaster
fi


if [ $SHADOW = "install" ]; then

   if [ $AUTO = "false" ]; then
      $INFOTEXT -u "\nShadow Master Host Setup"   
      $INFOTEXT -wait -n "\nMake sure, that the host, you wish to configure as a " \
                      "shadow host,\n has read/write permissions to the qmaster spool " \
                      "and SGE_ROOT/<cell>/common \ndirectory! For using a shadow master it " \
                      "is recommended to set up a \nBerkeley DB Spooling Server\n\n Hit <RETURN> to continue >> "
      ProcessSGERoot

      $INFOTEXT -n "Please enter your SGE_CELL directory or use the default [default] >> "

      SGE_CELL=`Enter default`
      export SGE_CELL
 
      if [ $HOST != "" ]; then
         $SGE_UTILBIN/gethostname -all | grep $HOST > /dev/null
         if [ "$?" = 0 ]; then 
            SHADOW_HOST=`$SGE_UTILBIN/gethostname -aname`
         else
            SHADOW_HOST=$HOST
         fi
      else
         SHADOW_HOST=`$SGE_UTILBIN/gethostname -aname`
      fi

      #creating the local_conf now!
      AddLocalConfiguration

      $INFOTEXT "Creating shadow_masters file for host %s\n" $SHADOW_HOST
      $INFOTEXT "Starting sge_shadowd on host %s\n" $SHADOW_HOST    
      mod_file="echo $SHADOW_HOST"
      ExecuteAsAdmin $mod_file >> $SGE_ROOT/$SGE_CELL/common/shadow_masters
 
      if [ $SHADOW_HOST = `$SGE_UTILBIN/gethostname -aname` ]; then
         . $SGE_ROOT/$SGE_CELL/common/settings.sh
         SGE_ARCH=`$SGE_ROOT/util/arch`
         $SGE_ROOT/bin/$SGE_ARCH/sge_shadowd &
      else
        for shs in `cat $SGE_ROOT/$SGE_CELL/common/shadow_masters`; do
           echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; SGE_ARCH=`$SGE_ROOT/util/arch`; $SGE_ROOT/bin/$SGE_ARCH/sge_shadowd &" | $SHELL_NAME $shs /bin/sh &
        done
      fi

      $INFOTEXT "Shadowhost installation completed!"
   else
      GetConfigFromFile
      $INFOTEXT -log "Starting sge_shadowd on host %s" $SHADOW_HOST    
      mod_file="echo $SHADOW_HOST"
      ExecuteAsAdmin $mod_file >> $SGE_ROOT/$SGE_CELL/common/shadow_masters
      for shs in `cat $SGE_ROOT/$SGE_CELL/common/shadow_masters`; do
         echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; SGE_ARCH=`$SGE_ROOT/util/arch`; $SGE_ROOT/bin/$SGE_ARCH/sge_shadowd &" | $SHELL_NAME $shs /bin/sh &
      done
   fi
fi

if [ $SHADOW = "uninstall" ]; then

   s_host=`cat $SGE_ROOT/$SGE_CELL/common/shadow_masters`
   s_pid=`cat $SGE_ROOT/$SGE_CELL/spool/qmaster/shadowd_$s_host.pid`

   if [ $AUTO = "true" ]; then
      ret=$SGE_UTILBIN/checkprog $s_pid shadowd
      if [ $ret = 0 ]; then
         $INFOTEXT -log "Stopping shadowd!" 
         $SGE_ROOT/$SGE_CELL/common/sgemaster -shadowd stop
         ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters
         exit 0
      else
         $INFOTEXT -log "Shadow daemon is not running on this host!" 
         exit 1
      fi
   else
      echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; $SGE_ROOT/$SGE_CELL/common/sgemaster -shadowd stop &; ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters" | $SHELL_NAME $s_host /bin/sh &   

   fi

   if [ $s_host = `$SGE_UTILBIN/gethostname -aname` ]; then
      ret=$SGE_UTILBIN/checkprog $s_pid shadowd
      if [ $ret = 0 ]; then
         $SGE_ROOT/$SGE_CELL/common/sgemaster -shadowd stop
         ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters
      else
         $INFOTEXT "Shadow daemon is not running on this host!" 
      fi
   else
      echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; $SGE_ROOT/$SGE_CELL/common/sgemaster -shadowd stop &; ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters" | $SHELL_NAME $s_host /bin/sh &
   fi
      

fi

if [ $BACKUP = "true" ]; then
   if [ $AUTO = "true" ]; then
      GetConfigFromFile
   fi
   BackupConfig
fi

if [ $RESTORE = "true" ]; then
   RestoreConfig
fi

if [ $MAKE_RC = "true" ]; then

   DATE=`date '+%Y-%m-%d_%H:%M:%S'`

   $INFOTEXT -u "\nRC Startup Script Generation"   
   $INFOTEXT -wait -n "\nThis option allows you to create new startup scripts for\n" \
                         "qmaster/scheduler/shadow daemon and the execution daemon.\n\n" \
                         "The new startup scripts will be saved in <sge_root>/<cell>/common.\n" \
                         "A copy of your old rc-scripts will be saved.\n\n" \
                         "Hit <RETURN> to continue >> "
   ProcessSGERoot

   $INFOTEXT -n "\nPlease enter your SGE_CELL directory or use the default [default] >> "

   SGE_CELL=`Enter default`
   SGE_CELL_VAL=$SGE_CELL

   export SGE_CELL
   export SGE_CELL_VAL

   if [ -f $SGE_ROOT/$SGE_CELL/common/bootstrap ]; then
      ADMINUSER=`cat $SGE_ROOT/$SGE_CELL/common/bootstrap | grep admin_user | awk '{ print $2 }'`
   else
      $INFOTEXT "\nObviously there was no qmaster installation for this cell yet. The file\n\n" \
                "   %s\n\n" \
                "does not exist. Exit." \$SGE_ROOT/$SGE_CELL/common/bootstrap
      exit 1
   fi

   if [ -f $SGE_ROOT/$SGE_CELL/common/settings.sh ]; then
      . $SGE_ROOT/$SGE_CELL/common/settings.sh
   else
      $INFOTEXT "\nThe file\n\n" \
                "   %s\n\n" \
                "which is required to set the environment variables does not exist. Exit." \
                \$SGE_ROOT/$SGE_CELL/common/settings.sh
       exit 1
   fi

   if [ -f $SGE_ROOT/$SGE_CELL/common/sgemaster ]; then
      ExecuteAsAdmin mv $SGE_ROOT/$SGE_CELL/common/sgemaster $SGE_ROOT/$SGE_CELL/common/sgemaster_$DATE
   fi

   if [ -f $SGE_ROOT/$SGE_CELL/common/sgeexecd ]; then
      ExecuteAsAdmin mv $SGE_ROOT/$SGE_CELL/common/sgeexecd $SGE_ROOT/$SGE_CELL/common/sgeexecd_$DATE
   fi   

   COMMONDIR=$SGE_CELL/common

   CreateSGEStartUpScripts 0 true master
   CreateSGEStartUpScripts 0 true execd

   $INFOTEXT "\nYour new startup scripts are created. You will find them in the\n" \
             "directory:\n\n" \
             "   %s\n\n" \
             "Your old startup scripts are saved in this directory as\n\n" \
             "   %s\n" \
             "   %s\n\n" \
             \$SGE_ROOT/$COMMONDIR sgemaster_$DATE sgeexecd_$DATE

   $INFOTEXT "Please now copy the new startup scripts to the system wide rc\n" \
             "file location on all qmaster, shadowd and execution hosts."

fi

if [ $UPDATEDB = "true" ]; then

   WelcomeTheUserUpgrade
   ProcessSGERoot

   $INFOTEXT -n "\nPlease enter your SGE_CELL directory or use the default [default] >> "

   SGE_CELL=`Enter default`
   SGE_CELL_VAL=$SGE_CELL

   export SGE_CELL
   export SGE_CELL_VAL
   
   $CLEAR
   UpgradeDB
fi

exit 0
