#!/bin/sh
#
# (c) 2004 Sun Microsystems, Inc. Use is subject to license terms.
#
#+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#+-#+-#-+-#
#
# $SGE_ROOT/dbwriter/bin/sgedbwriter_template
#
# DO NOT EDIT THIS FILE - this file is used as an template
# Don't change the markers #+-#+-#+-# and "#-#-#-#" , they will be removed
#
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
#
# DB Writer Startup script template
#
# This script can be called with the following arguments:
#
#       start       start the dbWriter as background process
#       run         run the dbWriter
#       stop        terminates the dbWriter 
#                   
#---------------------------------------------------------------------------
# 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:       sgedbwriter 
# Required-Start: $network 
# Required-Stop:
# Default-Start:  3 5
# Default-Stop: 0 1 2 6
# Description:  start Grid Engine dbWriter
### END INIT INFO
#---------------------------------------------------------------------------

# --------------------------------------------------------------------------
# Setup a clean path
# --------------------------------------------------------------------------
PATH=/bin:/usr/bin:/sbin:/usr/sbin

# --------------------------------------------------------------------------
# setup the base environment
# --------------------------------------------------------------------------
SGE_ROOT=@@SGE_ROOT@@; export SGE_ROOT
SGE_CELL=@@SGE_CELL@@; export SGE_CELL
JAVA_HOME=@@JAVA_HOME@@; export JAVA_HOME

ARCH=`$SGE_ROOT/util/arch`

UTILBIN_DIR=$SGE_ROOT/utilbin/$ARCH

if [ ! -d $UTILBIN_DIR ]; then
   echo "utilbin directory $UTILBIN_DIR not found"
   exit 1
fi


# --------------------------------------------------------------------------
# Setup the classpath
# --------------------------------------------------------------------------
LIBDIR=$SGE_ROOT/dbwriter/lib
for i in $LIBDIR/*.jar
do
  if [ "$CP" = "" ]; then
     CP="$i"
  else
     CP="$CP:$i"
  fi
done



# --------------------------------------------------------------------------
# Setup the SPOOL directory
# -------------------------------------------------------------------------- 
SPOOL_DIR=$SGE_ROOT/$SGE_CELL/spool/`hostname`


LOG_FILE=$SPOOL_DIR/dbwriter.log
PID_FILE=$SPOOL_DIR/dbwriter.pid

# --------------------------------------------------------------------------
# source the configuration of the dbWriter
# --------------------------------------------------------------------------
. $SGE_ROOT/$SGE_CELL/common/dbwriter.conf

# --------------------------------------------------------------------------
#  echo all options for the dbWriter
# --------------------------------------------------------------------------
echoOptions()
{
    echo "DBWRITER_CONTINOUS=$DBWRITER_CONTINOUS"              
    echo "DBWRITER_INTERVAL=$DBWRITER_INTERVAL"                
    echo "DBWRITER_DRIVER=$DBWRITER_DRIVER"                    
    echo "DBWRITER_URL=$DBWRITER_URL"                          
    echo "DBWRITER_USER=$DBWRITER_USER"                        
    echo "DBWRITER_USER_PW=$DBWRITER_USER_PW"                  
    echo "DBWRITER_REPORTING_FILE=$DBWRITER_REPORTING_FILE"    
    echo "DBWRITER_SHARE_LOG_FILE=$DBWRITER_SHARE_LOG_FILE"    
    echo "DBWRITER_STATISTIC_FILE=$DBWRITER_STATISTIC_FILE"    
    echo "DBWRITER_CALCULATION_FILE=$DBWRITER_CALCULATION_FILE"
    echo "DBWRITER_DELETION_FILE=$DBWRITER_DELETION_FILE"      
    echo "DBWRITER_DEBUG=$DBWRITER_DEBUG"                      
}

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


# ------------------------------------------------------------------------
# Print a usage message and exit
# ------------------------------------------------------------------------
usage()
{
   echo "usage: $0 [-debug] [-debug_port <port>] (start|stop)"
   echo "    start   start the dbwriter as background process (default)"
   echo "    stop    stop the dbwriter"   
   echo ""
   echo "    -debug              start the dbwriter in in debug mode"
   echo "    -debug_port <port>  port for debugging (default 8000)"
   exit 1
}

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

checkSpoolDir() {

       if [ ! -d "$1" ]; then
          echo "Spool directory $1 does not exist."
          printf "Should it be created? (y/n) >> "
          read dummy
          if [ $dummy = "y" ]; then
             $UTILBIN_DIR/adminrun $ADMIN_USER mkdir -p $1
             if [ $? -ne 0 ]; then
                echo "Can't create spool directory $1"
                exit 1
             fi
          else
             echo "Can't start sgedbwriter without a spool directory"
             exit 1
          fi
       fi
}

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

DEBUG=false
DEBUG_PORT=8000
PARAM=start

while [ $# -gt 0 ]; do
   case "$1" in
     "-debug")
	     DEBUG=true; shift;;
     "-debug_port")	     
	     shift 
		 if [ $# -lt 1 ]; then
		    usage
	     fi
		 DEBUG_PORT=$1
		 shift;;
     "start")
	     PARAM=start; shift;;
     "stop")
	     PARAM=stop; shift;;
     *)
	   echo "Unknown option $1"
	   usage;;
   esac
done

case $PARAM in
   "start")

       ADMIN_USER=`GetAdminUser`

       checkSpoolDir $SPOOL_DIR

       if [ ! -f "$LOG_FILE" ]; then
          $UTILBIN_DIR/adminrun $ADMIN_USER touch $LOG_FILE
       fi
	   
	   if [ "$DEBUG" = "true" ]; then
          echo "starting dbWriter in debug mode (port=$DEBUG_PORT)"
	      JVMARGS="-server -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=$DEBUG_PORT"
       else
          echo "starting dbWriter"
	      JVMARGS="-server"
	   fi
	   

       echoOptions | nohup $UTILBIN_DIR/adminrun $ADMIN_USER  \
                        ${JAVA_HOME}/bin/java -cp "$CP" $JVMARGS \
                          com/sun/grid/reporting/dbwriter/ReportingDBWriter \
                          -logfile $LOG_FILE >> $LOG_FILE 2>&1 &

       JAVA_PID=$!
       echo $JAVA_PID > $PID_FILE;;
   "stop")
      Shutdown java $PID_FILE;;
   *)
      echo "invalid parameter $PARAM"   
      usage;;
esac

