#!/bin/bash
#
#  This script launch and monitoring the Access Manager.
#  If Access Manager stops, this script will automatically re-start it.
#

osname=`uname`

stopsvc()
{
   trap "" 1 2 3 15
   if [ "$osname" != "AIX" ]; then
      ps -ewwwf | fgrep $SvcName | cut -c10-15 | xargs kill -9
   else
      ps -ef | fgrep $SvcName | cut -c10-15 | xargs kill -9
   fi
   unlink $SvcPidFile
   sleep 1
}

startsvc()
{
   if [ -f $SvcPidFile ]; then
      APid=`cat $SvcPidFile`
      if [ "$osname" != "AIX" ]; then
         ps -ewwwf | grep $SvcName | grep $APid
      else
         ps -ef | grep $SvcName | grep $APid
      fi
      if [ $? -eq 0 ]; then
         echo "$SvcName has been running. "
         return
      fi
   fi

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

   cd $SvcDir
   trap "stopsvc" 1
   trap "stopsvc; exit" 2 3 15
   java $_jit_compiler -Xoss512k -Djavax.net.ssl.keyStore=/var/websm/security/SM.privkr -Djavax.net.ssl.keyStorePassword=defp -Djava.security.auth.login.config=/opt/hsc/data/cimom.config -Djava.security.auth.policy=/opt/hsc/data/hscAccess.policy -Dorg.snia.wbem.cimom.properties=/opt/hsc/data/cim.properties com.ibm.hsc.access.server.AcmManager &
   SvcPid=$!

   echo $SvcPid > $SvcPidFile
   cd $CurrDir
}


# --- main script start here.
SvcDir=/opt/hsc/sbin
LOG_DIR=/var/hsc/log
SvcName=AcmManager
SvcPidFile=/var/hsc/${SvcName}.pid
CurrDir=`pwd`

#=== Build the ENV for running AcmManager.
x=`type -p java 2>/dev/null`
if [ "$x" != "" ]
then
  JAVAPATH=`/usr/bin/dirname $x`
fi
export LD_LIBRARY_PATH=/usr/lib:/opt/hsc/lib:$LD_LIBRARY_PATH
if [ "$osname" == "AIX" ]; then
   export LIBPATH=/usr/lib:/opt/hsc/lib:/opt/freeware/lib:$LIBPATH
fi
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

# Directory of the acm.jar.
x=""
for i in /usr/websm/codebase/*.jar
do
x=${x}/usr/websm/codebase/`basename $i`:
done

CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/opt/hsc/codebase/acm.jar:/usr/websm/codebase/pluginjars/aca.jar:/usr/websm/codebase/pluginjars/hsc_${LANG}.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/jcb.jar:/usr/websm/codebase/pluginjars/hsc.jar:/usr/websm/codebase/pluginjars/hsc_bundles.jar:/usr/websm/codebase/pluginjars/sniacimom.jar:/usr/websm/codebase/pluginjars/xerces.jar:/opt/ccfw/ccfw.jar:$x:$CLASSPATH
export CLASSPATH
export PATH=/opt/IBMJava/jre/bin:$JAVAPATH:$PATH
# === end of build ENV for running AcmManager.


let "beginTime = `date +%s`"
let "count = 0"
while (true)
do
    let "count = count + 1"
    let "t = `date +%s` - beginTime"
#    echo "$count  $t"
    if [ $count -gt 20 ]; then
       if [ $t -lt 60 ]; then
          echo  "Launch $SvcName too fast. Sleep 5 minutes ..."
          sleep 300
       else
          let "beginTime = `date +%s`"
          let "count = 0"
       fi
    fi

    startsvc
    wait $SvcPid
done

echo "$SvcName quited!"

