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

#
# RHEL5  Startup and Shutdown Script for the IME servers for the OHAN
#        product line
#
# 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 - only 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=OHAN

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 $lcPL
	 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
}

execMySql( )
{
   # $1 product line mnemonic
   # $2 command   (start | stop)

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

   # extract from my.cnf all lines where the first line matches uc <PL> and
   #                                     the last line matches mysqld
   # -- this extract should be [GCMS]      line 1
   #                           [mysqld05]  line 2
   #

   echo "$2 mysql daemon for $1 "

   sed -n "/\[$ucPL/,/mysqld/p" /etc/my.cnf |
   while read pl
   do
      #echo "$pl"
      len=${#pl}
      instance=${pl:1:len-4}
      echo $instance " - instance "  $pl " - my.cnf read"
      #if [ $len -eq 10 ]; then

      if [ $instance = 'mysqld' ]; then
         # extract (zero based) chars 7,8 for instance id
         iMySql=${pl:7:2}
         echo "$pl" " - " "$iMySql"

         su --command="/usr/bin/mysqld_multi --verbose  \
                 --log=/tmp/multi_$ucPL.log             \
                 $2 $iMySql" localsql
      fi

   done
   rval=0
}

(
case $1 in

'start')

   echo -e "\n\n" `date` " - Starting the IME $PL Product Line"
   execMySql $PL start  # start mysql daemon

   sleep 2   # allow time for mysqld to start

   #ps -ef | grep mysqld

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

   startproc imeOrder $PL
   echo "start logic back from imeOrder, rval=$rval"
   if [ $rval = 0 ]; then
      startproc imeQuery $PL
      echo "start logic back from imeQuery, rval=$rval"
   fi
   if [ $rval = 0 ]; then
      startproc imeconad_rpc $PL
      echo "start logic back from imeconad, rval=$rval"
   fi
   if [ $rval = 0 ]; then
      startproc imeuabad_rpc $PL
      echo "start logic back from imeuabad, rval=$rval"
   fi
   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 "imeuabad_rpc_server $PL"
   killproc "imeconad_rpc_server $PL"
   killproc "imeQuery_server $PL"
   killproc "imeOrder_server $PL"

   execMySql $PL  stop # bring down mysql daemon

   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
