#!/bin/bash
#
# Startup script for SM
#
# description: SCMS-SM
# processname: SM
# The script was originally written in sh

pcubehome=`echo ~pcube`

if . $pcubehome/sm.sh; then
   BIN_DIR={$UMROOT}bin/
else
   BIN_DIR=`dirname "$0"` 
   case $BIN_DIR in
    */) ;;
    *)  BIN_DIR=$BIN_DIR/ ;;
   esac
   BIN_FULL_DIR=`pwd`
   case $BIN_FULL_DIR in
    */) ;;
    *)  BIN_FULL_DIR=$BIN_FULL_DIR/ ;;
   esac
   BIN_FULL_DIR=$BIN_FULL_DIR$BIN_DIR
   # UMROOT must be the full path and not relative so ...
   UMROOT=$BIN_FULL_DIR../
fi

if [ -f "$pcubehome/.clurc" ] ; then 
  . "$pcubehome/.clurc"
fi

# verify UMROOT has a slash at the end
case $UMROOT in
 */) ;;
 *)  UMROOT=$UMROOT/ ;;
esac
 
. ${UMROOT}bin/clu-common

case "`uname`" in
  SunOS) PSARG="-eaf"; ;;
  Linux) PSARG="uaxww"; ;;
      *) echo unknown architecture
         PSARG="uaxwww" ;;
esac

# create temporary file, along the way redirect all relevant mesages to this file, 
# while the script ends write the file to CLU log in order to improve loging messages.
# will be used only in --start, --stop, --restart, --abort and --quit operations
create_tmp_file
echo "p3sm $*" | redirect_to_tmp_file

# process memory in MB
if [ -z $PCUBE_SM_MEM_SIZE ] ;
  then PCUBE_SM_MEM_SIZE=256
fi

if [ 256 -gt $PCUBE_SM_MEM_SIZE ] ;
  then echo "invalid memory size $PCUBE_SM_MEM_SIZE. minimum size is 256MB" | append_to_tmp_file
  write_and_delete_tmp_file_to_clu_log_and_exit 1
fi

MEGA=M
PCUBE_SM_MEM_SIZE=$PCUBE_SM_MEM_SIZE$MEGA

UMLIB=${UMROOT}lib
UMSERVERROOT=${UMROOT}root
export UMROOT
export UMLIB
export UMSERVERROOT
export HTTP_SERVER_JARS

TT_CLASSES=$pcubehome/classes/classes15.jar
SOAP_JARS=$UMLIB/saaj.jar:$UMLIB/commons-logging.jar:$UMLIB/commons-discovery.jar:$UMLIB/axis.jar:$UMLIB/jaxrpc.jar:$UMLIB/wsdl4j.jar:$UMLIB/wss4j.jar:$UMLIB/xalan.jar:$UMLIB/activation.jar:$UMLIB/mail.jar:$UMLIB/xml-apis.jar:$UMLIB/xmlsec.jar
CLASSPATH=$UMLIB/vlm.jar:$UMLIB/um_core.jar:$UMLIB/jce-jdk13-133.jar:$UMLIB/jdmkrt.jar:$UMLIB/httpclnt.jar:$UMLIB/regexp.jar:$UMLIB/xerces.jar:$UMLIB/antlr2.7.1.jar:$UMLIB/log4j.jar:$UMLIB/asn1rt.jar:$UMLIB/xmlpapi.jar:$UMLIB/SNMP4J.jar:$UMLIB/collector.jar:$UMLIB/jdom.jar:$UMLIB/cmdsvc.jar:$SOAP_JARS:$UMLIB:$TT_CLASSES:$PCUBE_SM_CP export CLASSPATH

# The following is required for so of TimesTen:
LD_LIBRARY_PATH=$pcubehome/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

# start the SM
start_um() {   
   SPECIAL_START_ARG=
   env > $UMSERVERROOT/env.txt
   RUN_UM=1

   write_and_delete_tmp_file_to_clu_log

   while [ $RUN_UM -eq 1 ] ;
   do
      if [ -f "$UMSERVERROOT/flsuhtodefaults.tmp" ] ;
	 then rm $UMSERVERROOT/flsuhtodefaults.tmp ;
      fi

      if [ -f "$UMSERVERROOT/agent_restart.tmp" ] ;
	 then rm $UMSERVERROOT/agent_restart.tmp ;
      fi

      RUN_UM=0

      # First check if the SM is running - might happen if agent_restart.tmp exists
      case "`uname`" in
	SunOS) PSARG="-eaf"; ;;
	Linux) PSARG="uaxww"; ;;
	    *) echo unknown architecture
	       PSARG="uaxwww" ;;
      esac
      if [ `ps $PSARG | grep "$PROCNAME" | grep -v grep | awk '{print $2}' | wc -l` -gt 0 ]; then
	 # SM probably restarted manually  - exit here with an error code
	 exit 1
      else 
	  "$PQBJAVA" -DAPP=$PROCNAME "-Dcom.pcube.filemanager.defaultPath=$UMSERVERROOT" "-Djmx.serial.form=1.0" \
	  -server -Xmx$PCUBE_SM_MEM_SIZE -Xms$PCUBE_SM_MEM_SIZE -XX:+UseParallelGC $SM_JAVA_BIT_OPTION $SM_JAVA_FLAGS \
	  com.pcube.management.StartServer $SPECIAL_START_ARG $2 $3 $4 $5 $6 2>&1 |logger -t SM -p daemon.notice
      fi      
      	  
      if [ -f "$UMSERVERROOT/flsuhtodefaults.tmp" ] ;
	 then  SPECIAL_START_ARG="-deleteStates"
	 RUN_UM=1	;
      fi

      if [ -f "$UMSERVERROOT/agent_restart.tmp" ] ;
	 then  SPECIAL_START_ARG=
	 RUN_UM=1	;
      fi

   done
}

# stop the SM server gracefully with a SIGINT
stop_um() {
   if [ `ps $PSARG | grep "$PROCNAME" | \
    grep -v grep | awk '{print $2}' | wc -l` -gt 0 ]; then

      PID_FILE="$UMSERVERROOT/sm.pid"
      PID=`ps $PSARG | grep "$PROCNAME" | grep -v grep | awk '{print $2}'`
   
      # signal to the SM to stop     
      ps $PSARG | grep "$PROCNAME" | \
      grep -v grep | awk '{print $2}'| xargs kill >/dev/null 2>&1 | append_to_tmp_file
	   
      # counter
      CNT=0
      # timeout = TM * DELAY seconds
      TM=10
      DELAY=6
      
      # wait till the process ends or the timeout expires
      while [ `ps -p "$PID" | wc -l` -gt 1 ] && [ $CNT -lt $TM ]; 
	 do
	    echo "waiting for SM to stop ..." | append_to_tmp_file
	    sleep $DELAY
	    CNT=`expr $CNT + 1`
	 done
      
      # check whether we ended due to a timeout
      if [ $CNT -lt $TM ]; then
	 echo "SM is down" | append_to_tmp_file
      else
	 echo "timeout expired - killing the SM" | append_to_tmp_file
	 # kill the process	
	 abort_um	   
      fi 
      # remove the reboot log      
      if [ -f "$UMSERVERROOT/reboot_log" ] && [ $AUTOMATED_CALL -eq 0 ] ;
	 then rm $UMSERVERROOT/reboot_log ;
      fi
   fi        
}

# stop the um server forcefully
abort_um() {
   ps $PSARG | grep "$PROCNAME" | \
   grep -v grep | awk '{print $2}'| xargs kill -9 >/dev/null 2>&1 | append_to_tmp_file
}

# quit the um server forcefully
quit_um() {
   ps $PSARG | grep "$PROCNAME" | \
   grep -v grep | awk '{print $2}'| xargs kill -quit >/dev/null 2>&1 | append_to_tmp_file
}

case "$1" in
  --start)
	start_um
	;;

  --stop)
	echo "Stopping SM " | append_to_tmp_file
	stop_um
	;;

  --abort)
        echo "Aborting SM " | append_to_tmp_file
	abort_um
	;;

  --quit)
        echo "Quiting SM " | append_to_tmp_file
	quit_um
	;;
  *)
        echo "Usage: $0 {--start|--stop|--abort|--restart}";
        write_and_delete_tmp_file_to_clu_log_and_exit 1;
esac

# write and delete temporary file to CLU log
write_and_delete_tmp_file_to_clu_log_and_exit 0;

