#!/bin/sh
#
#+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#
#
# $SGE_ROOT/util/rpc_startup_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 Berkeley DB RPC-Server startup script
#
# (c) 2004 Sun Microsystems, Inc. Use is subject to license terms.  

#
# This script can be called with the following arguments:
#
#       start       starts the RPC server
#       stop        Terminates the RPC server
#
# 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
#
#set -x
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:       sgebdb 
# Required-Start: $network
# Required-Stop:
# Default-Start:  3 5
# Default-Stop: 0 1 2 6
# Description:  start Grid Engine Berkeley DB RPC Server
### END INIT INFO
#---------------------------------------------------------------------------

SGE_ROOT=GENROOT; export SGE_ROOT
SGE_CELL=GENCELL; export SGE_CELL

# Attention: This information will be dynamically changed during,
# Berkeley DB installation. Do not edit!!!!
#----------------------------------------------------------------
BDBHOMES="-h SPOOLING_DIR"
#----------------------------------------------------------------

unset CODINE_ROOT GRD_ROOT COD_CELL GRD_CELL

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()
{
  if [ -f SPOOLING_DIR/bdb_messages ]; then
     db_pid=`cat SPOOLING_DIR/bdb_messages | cut -d" " -f2`
  fi
  if [ "$db_pid" != "" ]; then
     kill -TERM $db_pid 
  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()
{
   echo GENADMINUSER
}

#---------------------------------------------------------------------------
# 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
}

#-------------------------------------------------------------------------
# Execute command as user $ADMINUSER and exit if exit status != 0
# if ADMINUSER = default then execute command unchanged
#
# uses binary "adminrun" form SGE distribution
#
# USES: variables "$verbose"    (if set to "true" print arguments)
#                  $ADMINUSER   (if set to "default" do not use "adminrun)
#                 "$SGE_UTILBIN"  (path to the binary in utilbin)
#
ExecuteAsAdmin()
{
   ADMINUSER=`GetAdminUser`
   if [ "$verbose" = true ]; then
      echo $*
   fi

   if [ $ADMINUSER = default ]; then
      $*
   else
      $utilbin_dir/adminrun $ADMINUSER $* &
   fi

   return 0
}

#-------------------------------------------------------------------------
# Makedir: make directory, chown/chgrp/chmod it. Exit if failure
#
Makedir()
{
   file=$1
   if [ ! -d $file ]; then
       echo "creating directory: $file" 
       mkdir -p $1
    fi

   chmod 755 $file
}

CheckSpoolDir()
{
  if [ -d SPOOLING_DIR ]; then
     :
  else
     Makedir SPOOLING_DIR
  fi
}

CheckIfLocal()
{
   FS=`dirname $1`
   case $SGE_ARCH in
      solaris*)
         df -l $FS >/dev/null 2>&1
         if [ $? -eq 0 ]; then
            return 1
         else
            return 0
         fi
         ;;
      *linux)
         df -l $FS >/dev/null 2>&1
         if [ $? -eq 0 ]; then
            return 1
         else
            return 0
         fi
         ;;
      *)
         echo "Don't know how to test for local filesystem. Exit."
         exit 1
         ;;
   esac

}

#---------------------------------------------------------------------------
usage()
{
   echo "Grid Engine RPC server start/stop script. Valid parameters are:"
   echo ""
   echo "   \"start\"        startup Grid Engine RPC Server"
   echo "   \"stop\"         shutdown Grid Engine RPC Server"
   echo
   exit 1
}


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

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

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

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

if [ "$startup" = true ]; then

   #CheckSpoolDir
   #CheckIfLocal SPOOLING_DIR
   ExecuteAsAdmin $utilbin_dir/berkeley_db_svc -L SPOOLING_DIR/bdb_messages $BDBHOMES
else
   echo Stopping Berkeley DB
   Shutdown
fi
