#!/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
#
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__

#
# 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) which can
# be found at:
#
#    http://www.linuxfoundation.org/spec/booksets/LSB-Core-generic/LSB-Core-generic/initscrcomconv.html
#
### BEGIN INIT INFO
# Provides:       sgebdb 
# Required-Start: $network $remote_fs $portmap
# 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

# library path setting required only for architectures where RUNPATH is not supported
arch=`$SGE_ROOT/util/arch`
case $arch in
sol*|lx*)
   ;;
*)
   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
   ;;
esac

#---------------------------------------------------------------------------
# Shutdown
# Send SIGTERM to process name $1 with pid in file $2
#
Shutdown()
{
  DetectSMFService bdb
  if [ -n "$service" ]; then
     /usr/sbin/svcadm disable -st $service
     exit $?
  fi
  #CR 6646780 - sgebdb stop does not work
  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()
{
   #TODO: Should not use hardcoded value, but read a bootstrap file!
   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

}

#---------------------------------------------------------------------------
# DetectSMFService - sets service to a mask maching the name
# $1 ... name
#
DetectSMFService()
{
   name=$1
   service=""

   if [ "$noSMF" = true ]; then
      return
   fi
   #Otherwise we try is it's available of the system
   if [ -f /lib/svc/share/smf_include.sh ]; then
      . /lib/svc/share/smf_include.sh
      smf_present
      if [ $? -ne 0 ]; then
         return
      fi
   else
      return
   fi

   #Check we have cluster_name file
   if [ ! -r "$SGE_ROOT/$SGE_CELL/common/cluster_name" ]; then
      echo "Error: could not find $SGE_ROOT/$SGE_CELL/common/cluster_name!"
      exit $SMF_EXIT_ERR_CONFIG
   fi
   #Cluster name must be unique
   SGE_CLUSTER_NAME=`cat $SGE_ROOT/$SGE_CELL/common/cluster_name 2>/dev/null`
   
   service="svc:/application/sge/$name:$SGE_CLUSTER_NAME"

   if [ -n "$SMF_FMRI" -a "$service" != "$SMF_FMRI" ]; then
      #Someone is trying to fool us
      echo "Error: $service does not match FMRI $SMF_FMRI provided by SMF!"
      echo "Check that service instance $SGE_CLUSTER_NAME exist on your system"
      exit $SMF_EXIT_ERR_CONFIG
   fi
   #Check if service exists
   /usr/bin/svcs $service > /dev/null 2>&1
   if [ $? -ne 0 ]; then
      #No such service found in the system
      service=""
   fi
}


#---------------------------------------------------------------------------
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 "   \"-nosmf\"       force no SMF"
   echo
   exit 1
}


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

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

noSMF=false

for i in $*; do
   if [ "$i" = start ]; then
      startup=true
   elif [ "$i" = stop ]; then
      startup=false
   elif [ "$i" = -nosmf ]; then
      noSMF=true
   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
   DetectSMFService bdb
   if [ -z "$SMF_FMRI" -a -n "$service" ]; then
      /usr/sbin/svcadm enable -st $service
      exit $?
   fi
   #CheckSpoolDir
   #CheckIfLocal SPOOLING_DIR
   ExecuteAsAdmin $utilbin_dir/berkeley_db_svc -L SPOOLING_DIR/bdb_messages $BDBHOMES
else
   echo Stopping Berkeley DB
   Shutdown
fi
