#!/bin/bash
#
# chkconfig: 35 91 05
# description: Ime extract server
#

#
# RHEL5  Startup and Shutdown Script for the IME extract server
#
# 07/sep/2006 - MYSQL VERSION    MYSQL VERSION   MYSQL VERSION
# 03/nov/2006 - add 'cd $DIR' in startproc() so servers can open *.ini files
# 20/mar/2007 - ROSWELL PRODUCT LINE
# 25/may/2007 - EOS     PRODUCT LINE
# 05/sep/2007 - OHAN    PRODUCT LINE
# 04/nov/2008 - OHAN imeOrder
# 05/jan/2011 - set product line at top, render remainder of script
#               product line neutral
# 06/jun/2011 - onlt start the server if the product line is enabled
# 21/nov/2011 - add date to start/stop loggging (echo);  add finish
#               log entry (with date)
#

# Allowed exit values:
#   0 = success; causes "OK" to show up in checklist.
#   1 = failure; causes "FAIL" to show up in checklist.
#   2 = skip; causes "N/A" to show up in the checklist.
#       Use this value if execution of this script is overridden
#       by the use of a control variable, or if this script is not
#       appropriate to execute for some other reason.
#   3 = reboot; causes the system to be rebooted after execution.

# Input and output:
#   stdin is redirected from /dev/null
#
#   stdout and stderr are redirected to the /etc/rc.log file
#   during checklist mode, or to the console in raw mode.

PL=EXTRACT

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


# NOTE: If your script executes in run state 0 or state 1, then /usr might
#   not be available.  Do not attempt to access commands or files in
#   /usr unless your script executes in run state 2 or greater.  Other
#   file systems typically not mounted until run state 2 include /var
#   and /opt.

rval=0

# The set_return procedure checks the exit value of the command executed
# just prior to its call by this script.  If non-zero, the
# exit code is echoed to the log file and the return value of this script
# is set to indicate failure.

set_return() {
   x=$?

   rval=0
   if [ $x -ne 0 ]; then
      echo "EXIT CODE: $x"
      rval=1   # script FAILed
   fi
}

# The killproc procedure kills an IME server when called by
# this script.  If the kill fails, the failure is logged and the
# return value of the script is set to indicate failure.
#
# INPUT : KILL_PGM - file name of IME server to kill
# 
# NOTES : ps -e truncates process name so it doesn't display long names
#         grep -v grep  filters all processes that do not contain "grep"
#         ps -ef  2nd argument is process id
#
killproc() {
   # $1 name of program (must be passed quoted if it contains a space)
   KILL_PGM=$1
   pid=`ps -ef | grep "$KILL_PGM" | grep -v grep | awk ' {print $2}' `
   if [ "X$pid" != "X" ]; then
      if kill -s 15 "$pid"; then
         echo "$KILL_PGM Server $pid stopped" 
      else
         rval=1
         echo "Unable to stop $KILL_PGM Server process number $pid"
      fi
   fi
}

startproc() {
   # $1 base server name for the startup script
   # $2 product line mneumonic

   START_PGM=$1
   # convert product line to lower case
   lcPL=`echo $2 | tr '[A-Z]' '[a-z]'`
   ucPL=`echo $2 | tr '[a-z]' '[A-Z]'`

   # only start the server if the product line is enabled
   serviceStatus=`/sbin/chkconfig --list Ime$ucPL | grep :on`
   if [ "X$serviceStatus" != "X" ]; then

      # Execute the commands to start your subsystem
      DIR=/factory/mysql/$lcPL/bin
      cd $DIR

      echo "startproc enter : $DIR/$START_PGM"
      if [ -f $DIR/$START_PGM.startup ]; then
         $DIR/$START_PGM.startup
	 set_return
      else
	 echo "ERROR: $DIR/$START_PGM.startup file MISSING"
	 rval=1
      fi
   else
      echo -e "\n$ucPL not enabled to start (all runlevels off)"
      rval=2
   fi
}

(
case $1 in

'start')

   echo -e "\n\n" `date` " - Starting the IME $PL Product Line"

   # Execute the commands to start your subsystem
   # startup script must be of the format $START_PGM.startup

   startproc extract_server $PL
   if [ $rval = 0 ]; then
      touch /var/lock/subsys/Ime$PL
   fi

   echo `date` " - Finished Starting the IME $PL Product Line\n\n"
   ;;

'stop')
   echo -e "\n\n" `date` "Stopping the IME $PL Product Line"

   # Execute the commands to stop your subsystem
   killproc extractServer

   if [ $rval = 0 ]; then
      rm -f /var/lock/subsys/Ime$PL
   fi

   echo `date` " - Finished Stopping the IME $PL Product Line\n\n"
   ;;

'reload'| 'condrestart'| 'status'| 'restart')
   echo "$0 has no $1 action defined"
   rval=1
   ;;


*)
   echo "usage: $0 {start|stop|reload|condrestart|status|restart}"
   rval=1
   ;;
esac

) >> /var/log/Ime.log 2>&1
exit $rval
