#!/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

SCRIPT_VERSION="5"

#Reset PATH to a safe value
#
SAVED_PATH=$PATH
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"
COPY_COMMAND="scp"
EXEC_HOST_LIST="undef"
CONFIG_FILE="undef"
STDOUT2LOG="0"

SGE_NOMSG="undef"

WINDOWS_SUPPORT=false
WIN_DOMAIN_ACCESS=false
WIN_ADMIN_NAME=Administrator

CSP_RECREATE="false"
CSP_COPY_CERTS="false"
CSP_COUNTRY_CODE="undef"
CSP_STATE="undef"
CSP_LOCATION="undef"
CSP_ORGA="undef"
CSP_ORGA_UNIT="undef"
CSP_MAIL_ADDRESS="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`
     #Stdout2Log
     GetConfigFromFile
     CheckPath
     ;;
   -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.sh
     . ./util/install_modules/inst_execd_uninst.sh
     . ./util/install_modules/inst_qmaster.sh
     ;;
   -sm)
     SHADOW="install"
     . ./util/install_modules/inst_qmaster.sh
     . ./util/install_modules/inst_execd.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
     ;;
   -udb)
     BERKELEY="uninstall"
     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" = "" ]; 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" != "" -a "$EXECD" != "install" ]; 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
   CopyCA
   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 [ "$BERKELEY" = "install" ]; then

   if [ "$AUTO" = "true" ]; then
      Stdout2Log
      $INFOTEXT -log "Starting Berkeley DB installation!"
   fi
   
   if [ "$DB_SPOOLING_SERVER" = "" -o "$HOST" = "$DB_SPOOLING_SERVER" -o "$NOREMOTE" = "true" ]; then
      CheckWhoInstallsSGE
      ProcessSGERoot
      GetCell
      COMMONDIR=$SGE_CELL/common
      Makedir $SGE_CELL
      Makedir $COMMONDIR 
      SetSpoolingOptions
      AddSGEStartUpScript $euid bdb
      GiveBerkelyHints
   else
      $INFOTEXT -log "remote berkeley rpc server installation on host %s" $DB_SPOOLING_SERVER
      echo "cd $SGE_ROOT && ./inst_sge -db -auto $FILE -noremote" | $SHELL_NAME $DB_SPOOLING_SERVER /bin/sh &
      sleep 5
   fi

   BERKELEY="undef"
fi

if [ "$QMASTER" = "install" -a "$UPDATE" != "true" ]; then

 if [ "$AUTO" = "true" ]; then
    Stdout2Log
    $INFOTEXT -log "Starting qmaster installation!"
 fi

 WelcomeTheUser
 CheckForLocalHostResolving
 CheckWhoInstallsSGE
 ProcessSGERoot 
 GetQmasterPort
 GetExecdPort
 GetCell
 GetQmasterSpoolDir $euid
 SetCellDependentVariables
 WindowsSupport
 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
 AddWindowsAdmin
 AddHosts
 CopyCA
 SetScheddConfig
 GiveHints
 CheckRunningDaemon sge_qmaster

 if [ $? = 0 ]; then
    $INFOTEXT "sge_qmaster successfully installed!\n"
    $INFOTEXT -log "sge_qmaster successfully installed!\n"
    MoveLog
    $CLEAR
 else
    $INFOTEXT "sge_qmaster not successfully installed!\n" 
    $INFOTEXT -log "sge_qmaster not successfully installed!\n" 
    MoveLog
    exit 1 
 fi

fi


if [ "$EXECD" = "install" -a "$UPDATE" != "true" ]; then
 if [ "$AUTO" = "true" ]; then
    inst_counter=0
    Stdout2Log
    . $SGE_ROOT/$SGE_CELL/common/settings.sh

    if [ "$NOREMOTE" = false ]; then

      LOCALINST=false

      for h in $EXEC_HOST_LIST; do

         ExecdAlreadyInstalled $h
         installed=$?
   
         if [ "$installed" = 0 ]; then
	         if [ $h = `$SGE_UTILBIN/gethostname -name` ]; then
		      LOCALINST=true
            else
               CheckRSHConnection $h
               if [ "$?" = 0 ]; then 
                  $INFOTEXT -log "remote installation on host %s" $h
                  qconf -ah $h
                  echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; cd $SGE_ROOT && ./inst_sge -x -auto $FILE -noremote" | $SHELL_NAME $h /bin/sh &
               else
                  $INFOTEXT -log "rsh/ssh connection to host %s is not working" $h
               fi
            fi
         else 
            $INFOTEXT -log "Host %s already installed" $h
         fi

         inst_counter=`expr $inst_counter + 1`
         if [ "$inst_counter" = "25" ]; then
            sleep 5
            inst_counter=0
         fi
      done

   else
      LOCALINST=true
   fi

   if [ "$LOCALINST" = true ]; 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 "Execd on host %s is running!\n" $h
         $INFOTEXT -log "Execd on host %s is running!\n" $h
         MoveLog
         exit 0
      else
          $INFOTEXT "Execd on host %s is not started!\n" $h
          $INFOTEXT -log "Execd on host %s is not started!\n" $h
          MoveLog
          exit 1
      fi

   fi
 
 else
      WelcomeTheUserExecHost
      CheckForLocalHostResolving
      ProcessSGERoot
      CheckQmasterInstallation
      CheckCellDirectory
      . $SGE_ROOT/$SGE_CELL/common/settings.sh
      CheckWinAdminUser
      CheckCSP
      CheckHostNameResolving
      GetLocalExecdSpoolDir
      AddLocalConfiguration_With_Qconf
      AddSGEStartUpScript $euid execd
      StartExecd
      AddQueue
      GiveHints
      
      # This code will be executed in case inst_sge -x -host <remote host> 
      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
 MoveLog
 exit 0 
fi

if [ "$EXECD" = "uninstall" ]; then

   WelcomeUninstall 

   if [ "$SGE_ROOT" = "" -o "$SGE_CELL" = "" ]; then
      $INFOTEXT -wait -auto $AUTO "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, <RETURN> 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
   CheckHostNameResolving 1
   if [ "$?" = "1" ]; then
      $INFOTEXT "This host is not an admin host. Uninstallation is not allowed\nfrom this host!"
      $INFOTEXT -log "This host is not an admin host. Uninstallation is not allowed\nfrom this host!"
      MoveLog
      exit 1
   fi
   FetchHostname
   
   MoveLog
   
   if [ $QMASTER != "uninstall" ]; then 
      exit 0
   fi

fi

if [ $BERKELEY = "uninstall" ]; then

   if [ "$AUTO" = "true" ]; then
      Stdout2Log
      $INFOTEXT -log "Starting berkeley rpc uninstallation!"
   fi
   if [ "$DB_SPOOLING_SERVER" = "" -o "$HOST" = "$DB_SPOOLING_SERVER" -o "$NOREMOTE" = "true" ]; then
      euid=`$SGE_UTILBIN/uidgid -euid`
      ProcessSGERoot
      GetCell
      ADMINUSER=`$SGE_UTILBIN/filestat -owner . 2> /dev/null`
      ExecuteRPCServerScript stop
      RemoveRcScript $HOST bdb $euid
      DeleteSpoolingDir
   else
      $INFOTEXT -log "remote berkeley rpc server uninstallation on host %s" $DB_SPOOLING_SERVER
      echo "cd $SGE_ROOT && ./inst_sge -udb -auto $FILE -noremote" | $SHELL_NAME $DB_SPOOLING_SERVER /bin/sh
   fi

   BERKELEY="undef"
   
fi

if [ $QMASTER = "uninstall" ]; then

   if [ "$AUTO" = "true" ]; then
      Stdout2Log
      $INFOTEXT -log "Starting qmaster uninstallation!"
   fi
   
   if [ "$SGE_ROOT" = "" -o "$SGE_CELL" = "" ]; then
      $INFOTEXT -wait -auto $AUTO "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
   RemoveQmaster
   MoveLog
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 >> "
      CheckWhoInstallsSGE
      CheckForLocalHostResolving
      ProcessSGERoot

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

      SGE_CELL=`Enter default`
      export SGE_CELL
      . $SGE_ROOT/$SGE_CELL/common/settings.sh
   
      CheckHostNameResolving
 
      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

      #check if spooling is supported!
      spooling_method=`cat $SGE_ROOT/$SGE_CELL/common/bootstrap 2>/dev/null | grep "spooling_method" | awk '{ print $2 }'`
      #if spooling_params contains no "/" then we have rpc server spooling
      `cat $SGE_ROOT/$SGE_CELL/common/bootstrap | grep spooling_params | awk '{ print $2 }' | grep "/"`
      if [ $? = 0 ]; then
         db_home=`cat $SGE_ROOT/$SGE_CELL/common/bootstrap 2>/dev/null | grep "spooling_params" | awk '{ print $2 }' | cut -d";" -f1`
         fstype=`$SGE_UTILBIN/fstype $db_home`
         if [ $? != 0 ]; then
            $INFOTEXT "\n Shadow master can not access spool directory at %s! \n" $db_home 
            $INFOTEXT "Check if the spool directory is mounted and has read/write permissions.\n"
            exit 1
         elif [ `echo $db_home | cut -d":" -f2` = "$db_home" ]; then
            if [ "$spooling_method" = "berkeleydb" ]; then
               if [ `echo $fstype | grep "nfs" | wc -l` -gt 0 ]; then
                  if [ "$fstype" != "nfs4" ]; then
                     $INFOTEXT "Spooling directory exported as $s is not supported!\n" $fstype
                     $INFOTEXT "\nPlease install the database directory on a NFSv4 fileserver or use the RPC Client/Server mechanism"
                     exit 1;
                  else
                     $INFOTEXT "We detected local Berkeley DB Spooling without a RPC Server. Local spooling\n is not working under " \
                               "mixed architectures.\n\n "
                     $INFOTEXT -auto $AUTO -ask "y" "n" -def "y" -n "Do you want to continue (y/n) ('n' will abort) [y] >> "
                     if [ $? != 0 ]; then
                        $INFOTEXT "Installation aborted"
                        exit 1;
                     fi
                  fi
               fi
            fi
         fi
       fi

      #creating the local_conf now!
      AddLocalConfiguration_With_Qconf

      $INFOTEXT "Creating shadow_masters file for host %s\n" $SHADOW_HOST
      $INFOTEXT "Starting sge_shadowd on host %s\n" $SHADOW_HOST
      ExecuteAsAdmin touch $SGE_ROOT/$SGE_CELL/common/shadow_masters
      ALREADY_SHADOWS=`cat $SGE_ROOT/$SGE_CELL/common/shadow_masters | grep -v $SHADOW_HOST`
      ExecuteAsAdmin mv $SGE_ROOT/$SGE_CELL/common/shadow_masters $SGE_ROOT/$SGE_CELL/common/shadow_masters.1
      ExecuteAsAdmin touch $SGE_ROOT/$SGE_CELL/common/shadow_masters
      ExecuteAsAdmin chmod a+rw $SGE_ROOT/$SGE_CELL/common/shadow_masters
      ExecuteAsAdmin echo $SHADOW_HOST >> $SGE_ROOT/$SGE_CELL/common/shadow_masters
      for s in `echo $ALREADY_SHADOWS`; do
         ExecuteAsAdmin echo $s >> $SGE_ROOT/$SGE_CELL/common/shadow_masters
      done
      ExecuteAsAdmin chmod go-w $SGE_ROOT/$SGE_CELL/common/shadow_masters
      ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters.1
 
      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 &
         AddSGEStartUpScript $euid master 
      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 &
           #TODO: install rc script
        done
      fi

      $INFOTEXT "Shadowhost installation completed!"
   else
      euid=`$SGE_UTILBIN/uidgid -euid`
      Stdout2Log
      for SHADOW in $SHADOW_HOST; do
         $INFOTEXT -log "Starting sge_shadowd on host %s" $SHADOW   
         ExecuteAsAdmin touch $SGE_ROOT/$SGE_CELL/common/shadow_masters
         ALREADY_SHADOWS=`cat $SGE_ROOT/$SGE_CELL/common/shadow_masters | grep -v "$SHADOW"`
         ExecuteAsAdmin mv $SGE_ROOT/$SGE_CELL/common/shadow_masters $SGE_ROOT/$SGE_CELL/common/shadow_masters.1
         ExecuteAsAdmin touch $SGE_ROOT/$SGE_CELL/common/shadow_masters
         ExecuteAsAdmin chmod a+rw $SGE_ROOT/$SGE_CELL/common/shadow_masters
         ExecuteAsAdmin echo $SHADOW >> $SGE_ROOT/$SGE_CELL/common/shadow_masters
         for s in `echo $ALREADY_SHADOWS`; do
            ExecuteAsAdmin echo $s >> $SGE_ROOT/$SGE_CELL/common/shadow_masters
         done
         ExecuteAsAdmin chmod go-w $SGE_ROOT/$SGE_CELL/common/shadow_masters
         ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters.1
         if [ $SHADOW = `$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 &
            AddSGEStartUpScript $euid master 
         else
              echo ". $SGE_ROOT/$SGE_CELL/common/settings.sh; SGE_ARCH=`$SGE_ROOT/util/arch`; $SGE_ROOT/bin/$SGE_ARCH/sge_shadowd &" | $SHELL_NAME $SHADOW /bin/sh &
              #TODO: install rc script
         fi
      done
   fi
   MoveLog
fi

if [ $SHADOW = "uninstall" ]; then

   GetAdminUser

   if [ "$HOST" = "undef" -o "$HOST" = `$SGE_UTILBIN/gethostname -aname`  ]; then
      s_host=`$SGE_UTILBIN/gethostname -aname`
      is_local="true"
   else
      s_host="$HOST"
      is_local="false"
   fi

   if [ "$s_host" = "`cat $SGE_ROOT/$SGE_CELL/common/shadow_masters | grep "$s_host"`" ]; then
      is_shadow="true"
      m_spool=`cat $SGE_ROOT/$SGE_CELL/common/bootstrap | grep qmaster_spool_dir | awk '{ print $2}'`
      s_pid=`cat $m_spool/shadowd_$s_host.pid`
   else
      is_shadow="false"
   fi

   if [ $AUTO = "true" ]; then
      if [ "$is_local" = "true" ]; then
         Stdout2Log
         if [ "$is_shadow" = "false" ]; then
            $INFOTEXT -log "This host is no shadow host!"
            MoveLog
            exit 1
         fi
         $INFOTEXT -log "Stopping shadowd!" 
         $SGE_ROOT/$SGE_CELL/common/sgemaster -shadowd stop
         ExecuteAsAdmin mv $SGE_ROOT/$SGE_CELL/common/shadow_masters $SGE_ROOT/$SGE_CELL/common/shadow_masters_tmp
         ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters
         ExecuteAsAdmin touch $SGE_ROOT/$SGE_CELL/common/shadow_masters
         cat $SGE_ROOT/$SGE_CELL/common/shadow_masters_tmp | grep -v $s_host >> $SGE_ROOT/$SGE_CELL/common/shadow_masters
         ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters_tmp
         ExecuteAsAdmin chown $ADMINUSER $SGE_ROOT/$SGE_CELL/common/shadow_masters
         MoveLog
         exit 0
      else
         echo "cd $SGE_ROOT; . $SGE_ROOT/$SGE_CELL/common/settings.sh; inst_sge -usm" | $SHELL_NAME $s_host /bin/sh &   
      fi
   else
      if [ "$is_local" = "true" ]; then
         if [ "$is_shadow" = "false" ]; then
            $INFOTEXT "This host is no shadow host!"
            exit 1
         fi
         $INFOTEXT "Stopping shadowd!" 
         $SGE_ROOT/$SGE_CELL/common/sgemaster -shadowd stop
         ExecuteAsAdmin mv $SGE_ROOT/$SGE_CELL/common/shadow_masters $SGE_ROOT/$SGE_CELL/common/shadow_masters_tmp
         ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters
         ExecuteAsAdmin touch $SGE_ROOT/$SGE_CELL/common/shadow_masters
         cat $SGE_ROOT/$SGE_CELL/common/shadow_masters_tmp | grep -v $s_host >> $SGE_ROOT/$SGE_CELL/common/shadow_masters
         ExecuteAsAdmin rm -f $SGE_ROOT/$SGE_CELL/common/shadow_masters_tmp
         ExecuteAsAdmin chown $ADMINUSER $SGE_ROOT/$SGE_CELL/common/shadow_masters
         exit 0
      else
         $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`
         which $SHELL_NAME
         if [ "$?" = 1 ]; then
            $INFOTEXT ">>%s<< is not a valid shell command, please use rsh or ssh!"
            exit 1
         fi 
         echo "cd $SGE_ROOT; . $SGE_ROOT/$SGE_CELL/common/settings.sh; inst_sge -usm" | $SHELL_NAME $s_host /bin/sh &   
      fi     
   fi

fi

if [ $BACKUP = "true" ]; then
   BackupConfig
   MoveLog
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
      GetAdminUser
   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
