#!/bin/bash

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#   Purpose: Ensures that a pid is killed upon exit of the function.
#   Input: pid number of process
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function killPid () {
    typeset _killPid=$1
    typeset _pid
    typeset _cnt
    kill -15 $_killPid >/dev/null 2>&1
    # make sure process exits
    for _cnt in 1 2; do
	_pid=$(ps -eo pid | grep "$_killPid" | while read _proc; do
		test "$_proc" -eq "$_killPid" && return $_proc
	    done
	)
	test -z "$_pid" && return
	sleep 2
    done
    test -n "$_pid" && kill -9 $_killPid >/dev/null 2>&1
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#   Purpose: Kills all running Java instances of the Command Controller.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function killAllJavaProcs () {
    # ensures that all java instances of command controller are stopped
    typeset _pids
    _pids=$(ps -eo pid,cmd -www | grep java | grep $CONTROLLER_JAVA_CLASS | awk '{print $1}')
    test -z "$_pids" && return

    #  kill java pids
    typeset _kill
    for _kill in $_pids; do
	killPid $_kill
    done
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#   Purpose: Determines if an instance of this script is running
#   Return: 0 = not running  1 = running
#   Variables Modified:
#       CMD_PID: set to pid of Command Controller instance
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function isControllerRunning () {
    # check pid file for running instance
    if [[ -f $PID_FILE ]]; then
	typeset _pid
	CMD_PID=$(cat $PID_FILE)
	test -z "$CMD_PID" && { rm -f $_PID_FILE; return 0; }

        # is process still running
	typeset _psData
	ps -eo pid,cmd -www | grep $CMD_PID | grep $BASENAME | while read _psData; do
	    set -- $_psData
	    # this returns from this sub process (not the parent process)
	    test "$1" -eq "$CMD_PID" && return 1
    	done
	test $? -ne 0 && return 1
	# process is not running
	rm -f $PID_FILE
    else
	_pids=$(ps -eo pid,cmd -www | grep java | grep $CONTROLLER_JAVA_CLASS | awk '{print $1}')
	test -n "$_pids" && return 1
    fi

    return 0
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#   Purpose: Stops the current instance of the Command Controller
#            shell script and all Java instances of the Command
#            Controller.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function stopController () {
    # ignore signals now
    trap "" 1 2 3 15

    isControllerRunning
    test "$?" -eq 0 && return

    # remove pid file before killing Java processes
    # new command controller will create new instances and create
    # new pid file before all java pids are killed
    rm -f $PID_FILE
    test -n "$CMD_PID" && killPid $CMD_PID
    killAllJavaProcs
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#   Purpose: Starts a new instance of the Command Controller shell
#            and creates a new instance of the Java instance. If any
#            Java instances of the Command Controller are running,
#            these processes will be killed.
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function startController () {
    isControllerRunning
    test $? -ne 0 && return

    killAllJavaProcs
    cd $HSC_BIN_DIR

    typeset -r WEBSM_DIR="/usr/websm"
    typeset -r WEBSM_CODEBASE_DIR=$WEBSM_DIR/codebase
    typeset -r WEBSM_PLUGINJARS_DIR=$WEBSM_CODEBASE_DIR/pluginjars
    typeset -r I18N_DIR=/opt/hsc/jars/i18n
    typeset -r RMC_CODEBASE_DIR=/usr/sbin/rsct/codebase

    if [ ! -d $HSC_VAR_DIR ]
    then
       mkdir $HSC_VAR_DIR
    fi
    for i in ${I18N_DIR}/hsc_*.jar
    do
      hsclocalejar=$hsclocalejar:$i:
    done

    JAVA_CLASSPATH=$DEBUG_HMC_CMD:${debug_jars}:${hsclocalejar}:${WEBSM_PLUGINJARS_DIR}/hsc.jar:${WEBSM_PLUGINJARS_DIR}/hsc_bundles.jar:${WEBSM_PLUGINJARS_DIR}/sniacimom.jar:${WEBSM_PLUGINJARS_DIR}/xerces.jar:${WEBSM_PLUGINJARS_DIR}/aca.jar:${WEBSM_PLUGINJARS_DIR}/ccfw.jar:${WEBSM_PLUGINJARS_DIR}/ccfw_sslite.jar:${WEBSM_PLUGINJARS_DIR}/auifw.jar:${WEBSM_CODEBASE_DIR}/wsm.jar:${RMC_CODEBASE_DIR}:${RMC_CODEBASE_DIR}/rmcapi.jar:${WEBSM_PLUGINJARS_DIR}/bundles.jar:${WEBSM_PLUGINJARS_DIR}/ldw_csdmlBeans.jar:${WEBSM_PLUGINJARS_DIR}/ior_csdmlBeans.jar:${WEBSM_PLUGINJARS_DIR}/SlotTypeLib.jar:${WEBSM_PLUGINJARS_DIR}/csdml.jar

    typeset _pid

    # set java flags
    # set maximum heap size
    typeset -r _max_heap="-Xmx150m"
    typeset -r _prop_file=${HSC_DATA_DIR}/CommandController.properties
    typeset -r _std_flags="-Djavax.net.ssl.keyStore=/opt/ccfw/data/SM.pubkr -Djavax.net.ssl.keyStorePassword=defp -Dorg.snia.wbem.cimom.properties=/opt/hsc/data/cim.properties -Dcontroller.properties=${_prop_file}"

    if [[ -f $HSC_LOG_DIR/.JIT_DISABLE ]]; then
	typeset -r _jit_compiler="-Djava.compiler=NONE"
    fi

    if [ -f ${HSC_LOG_DIR}/.DEBUG_COMMAND ]; then
	typeset -r _debug_flags="-verbosegc"
    fi

    trap "stopController" 1 2 3 15

    #+++++++++++++++++++++++++++++++
    # save last log file
    if [[ -f ${CONTROLLER_LOG} ]]; then
	mv -f ${CONTROLLER_LOG} ${CONTROLLER_LOG}.1
    fi

    java $_debug_flags $_jit_compiler $_max_heap $_std_flags -cp ${JAVA_CLASSPATH} ${CONTROLLER_JAVA_CLASS} >/tmp/$BASENAME.log 2>&1 &

    _pid=$!
    echo $_pid > $PID_FILE
    /bin/chmod 0440 $PID_FILE

    # wait for controller to exit
    wait $_pid
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  main

# define constants
export PATH=$PATH:/opt/IBMJava/jre/bin
typeset -r HSC_BASE_DIR=/opt/hsc
typeset -r HSC_VAR_DIR=/var/hsc
typeset -r HSC_BIN_DIR=${HSC_BASE_DIR}/bin
typeset -r HSC_LIB_DIR=${HSC_BASE_DIR}/lib
typeset -r HSC_DATA_DIR=$HSC_BASE_DIR/data
typeset -r HSC_LOG_DIR=$HSC_VAR_DIR/log

typeset -r PID_FILE=${HSC_VAR_DIR}/controller.pid

typeset -r BASENAME=$(basename $0)
typeset -r CONTROLLER=${HSC_BIN_DIR}/${BASENAME}
typeset -r CONTROLLER_JAVA_CLASS=com.ibm.hsc.command.CommandController
typeset -r CONTROLLER_LOG=${HSC_LOG_DIR}/controller.log

JAVAPATH="/opt/IBMJava/jre/bin/"
x=`type -p java 2>/dev/null`
if [ "$x" != "" ]
then
  JAVAPATH=`/usr/bin/dirname $x`
fi
export LD_LIBRARY_PATH=${HSC_LIB_DIR}:/usr/lib/:$LD_LIBRARY_PATH
if [ "${DEBUG_JARS_DIRECTORY}" != "" ] ; then
  if [ -d ${DEBUG_JARS_DIRECTORY} ] ; then
    for i in ${DEBUG_JARS_DIRECTORY}/*.jar
    do
       debug_jars=${debug_jars}:$i
    done
  fi
fi

# set path to include JAVA and HSC_BIN
PATH=$JAVAPATH:$HSC_BIN_DIR:$PATH
export PATH

case $1 in
    start )
	startController
	;;

    stop )
	stopController
	;;
esac
