#!/bin/sh
#
#+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#
#
# $SGE_ROOT/util/execd_template
#
# DO NOT EDIT THIS FILE - this file is used as an template
# Don't change the markers #+-#+-#+-# and "#-#-#-#" , they will be removed
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
#
# SGE/SGEEE startup script
#
# (c) 2004 Sun Microsystems, Inc. Use is subject to license terms.  

#
# This script can be called with the following arguments:
#
#       start       start execution daemon
#       stop        Terminates the execution daemon
#                   and the shepherd. This only works if the execution daemon 
#                   spool directory is in the default location.
#       softstop    do not kill the shepherd process
#
# Unix commands which may be used in this script:
#    cat cut tr ls grep awk sed basename
#
# This script requires the script $SGE_ROOT/util/arch
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin

#---------------------------------------------------------------------------
# The following lines provide the necessary info for adding a startup script
# according to the Linux Standard Base Specification (LSB) 1.0.0 which can
# be found at:
#
#    http://www.linuxbase.org/spec/gLSB/gLSB/sysinit.html
#
# See also
#
#    http://www.suse.de/~grimmer/Package-Conventions/index.html
#
### BEGIN INIT INFO
# Provides:       sgeexecd 
# Required-Start: $network
# Required-Stop:
# Default-Start:  3 5
# Default-Stop: 0 1 2 6
# Description:  start Grid Engine execd 
### END INIT INFO
#---------------------------------------------------------------------------

SGE_ROOT=GENROOT; export SGE_ROOT
SGE_CELL=GENCELL; export SGE_CELL
SGE_QMASTER_PORT=GENSGE_QMASTER_PORT; export SGE_QMASTER_PORT 
SGE_EXECD_PORT=GENSGE_EXECD_PORT; export SGE_EXECD_PORT

unset CODINE_ROOT GRD_ROOT COD_CELL GRD_CELL

count=0
while [ ! -d "$SGE_ROOT" -a $count -le 5 ]; do
   count=`expr $count + 1`
   sleep 5
done

ARCH=`$SGE_ROOT/util/arch`
shlib_path_name=`$SGE_ROOT/util/arch -lib`
old_value=`eval echo '$'$shlib_path_name`
if [ x$old_value = x ]; then
   eval $shlib_path_name=$SGE_ROOT/lib/$ARCH
else
   eval $shlib_path_name=$old_value:$SGE_ROOT/lib/$ARCH
fi
export $shlib_path_name

#---------------------------------------------------------------------------
# Shutdown
# Send SIGTERM to process name $1 with pid in file $2
#
Shutdown()
{
   name=$1
   pidfile=$2
   if [ -f $pidfile ]; then
      pid=`cat $pidfile`
      $utilbin_dir/checkprog $pid $name > /dev/null
      if [ "$?" = 0 ]; then
         kill -TERM $pid
         return $?
      fi
   fi
}


#---------------------------------------------------------------------------
# GetPathToBinaries
#    echo the name of the bin_dir on this system
#    The check is fullfilled if we can access the qstat binary
#    echo "none" if we can't determine the binary path
GetPathToBinaries()
{
   cfgname=$SGE_ROOT/$SGE_CELL/common/bootstrap

   base=none

   if [ -f $cfgname ]; then
      base=`grep binary_path $cfgname | awk '{ print $2 }'`
      if [ -f $base/qstat ]; then
         :
      elif [ -f $SGE_ROOT/util/arch ]; then
         arch=`$SGE_ROOT/util/arch`
         if [ -f $base/$arch/qstat ]; then
               base=$base/$arch
         fi
      fi
   fi

   echo $base
}


#---------------------------------------------------------------------------
# GetAdminUser
#    echo the name of the admin user on this system
#    echo "root" if admin user retrieval fails
GetAdminUser()
{
   cfgname=$SGE_ROOT/$SGE_CELL/common/bootstrap
   user=none

   if [ -f $cfgname ]; then
      user=`grep admin_user $cfgname | awk '{ print $2 }'`
   fi

   if [ `echo $user|tr "A-Z" "a-z"` = "none" ]; then
      user=root
   fi
   echo $user
}

#---------------------------------------------------------------------------
# GetPathToUtilbin
#    echo the path to the binaires in utilbin
#    The check is fullfilled if we can access the "gethostname" binary
#    echo "none" if we can't determine the binary path
#
GetPathToUtilbin()
{
   base=none

   if [ -f $SGE_ROOT/util/arch ]; then
      utilbindir=$SGE_ROOT/utilbin

      arch=`$SGE_ROOT/util/arch`
      if [ -f $utilbindir/$arch/gethostname ]; then
         base=$utilbindir/$arch
      fi
   fi

   echo $base
}


#---------------------------------------------------------------------------
# GetExecdSpoolDir
# get the execution daemon spooling dir from configuration
GetExecdSpoolDir()
{
   EXECD_SPOOL_DIR=`$bin_dir/qconf -sconf $UQHOST | grep execd_spool_dir | awk '{ print $2 }'`
   if [ "$EXECD_SPOOL_DIR" = "" ]; then
      EXECD_SPOOL_DIR=`$bin_dir/qconf -sconf | grep execd_spool_dir | awk '{ print $2 }'`
   fi


}

#---------------------------------------------------------------------------
usage()
{
   echo "Grid Engine start/stop script. Valid parameters are:"
   echo ""
   echo "   (no parameters): start qmaster and execution daemon if applicable"
   echo "   \"start\"        dto."
   echo "   \"stop\"         shutdown local Grid Engine processes and jobs"
   echo "   \"softstop\"     shutdown local Grid Engine processes (no jobs)"
   echo "                    host and restart it on this host"
   echo "                    Migration only works if this host is an admin host"
   echo ""
   echo "Only one of the parameters \"start\", \"stop\" or \"softstop\" is allowed."
   echo "Only one of the parameters beginning  with \"-\" is allowed." 
   echo
   echo "Default argument is \"start\" for all components."
   echo "Default for \"stop\" is shutting down all components."
   echo
   exit 1
}


#---------------------------------------------------------------------------
# MAIN Procedure
#

if [ "$#" -gt 2 -o "$1" = "-h" -o "$1" = "help" ]; then
   usage
fi

startup=true
execd=true
qstd=false
softstop=false

for i in $*; do
   if [ "$i" = start ]; then
      startup=true
   elif [ "$i" = stop ]; then
      startup=false
   elif [ "$i" = softstop ]; then
      startup=false
      softstop=true
   else
      usage
   fi
done

bin_dir=`GetPathToBinaries`
if [ "$bin_dir" = "none" ]; then
   echo "can't determine path to Grid Engine binaries"
   exit 1
fi

utilbin_dir=`GetPathToUtilbin`
if [ "$utilbin_dir" = "none" ]; then
   echo "can't determine path to Grid Engine utility binaries"
   exit 1
fi

# HOST is the aliased name (SGE name)
# UQHOST is the local host name (unqualified name)
HOST=`$utilbin_dir/gethostname -aname`
UQHOST=`$utilbin_dir/gethostname -name | cut -f1 -d.`

if [ "$startup" = true ]; then

   # execution daemon is started on this host!

      echo "   starting sge_execd"
      exec 1>/dev/null 2>&1
      $bin_dir/sge_execd
      [ $? -eq 0 -a -d /var/lock/subsys ] && touch /var/lock/subsys/sgeexecd
      exit 0
else
   if [ $execd = true ]; then
      # Shutdown execution daemon
      GetExecdSpoolDir
      execd_spool_dir=$EXECD_SPOOL_DIR/$UQHOST

      # Send SIGTERM to execd
      echo "   Shutting down Grid Engine execution daemon"
      Shutdown sge_execd $execd_spool_dir/execd.pid
      ret=$?
      if [ -f /var/lock/subsys/sgeexecd ]; then
          uid=`$utilbin_dir/uidgid -uid` 
          if [ "$uid" = "0" -a "$ret" = "0" ]; then
             rm -f /var/lock/subsys/sgeexecd >/dev/null 2>&1
          else
             echo "Can't shut down execd!"
             exit 1
          fi
      fi
      if [ $softstop = false ]; then
         # Send SIGTERM to all shepherds
         for jobid in `ls $execd_spool_dir/active_jobs`; do
            echo "   Shutting down Grid Engine shepherd of job $jobid"
            Shutdown sge_shepherd $execd_spool_dir/active_jobs/$jobid/pid
         done
      fi
   fi
fi
