#!/bin/sh
########################################################################
#
#  NAME:          SMmonitor
#  SUMMARY:       Startup script for the SM Event monitor
#  DESCRIPTION:   Startup script for the SM Event Monitor on Linux
#


BASEDIR=/opt/IBM_FAStT
CLIENT_DIR=$BASEDIR/client
TEMP_DIR=/tmp
DATA_DIR=/var/opt/SM
FFEAT=FULL_SA

action=$1
retval=0

case "$action" in
'start')
	#Check to see if there exists running SMmonitor
	ps -A -o "pid command" --cols 512 | grep PMUNIXLauncher | grep -v grep |
	while read line; do
		set $line x
        	process=$1
        	if [ $process -ne $$ ]; then
	  		echo "Cannot start a new Event Monitor because an Event Monitor is already running."
          		logger -t SMmonitor -p daemon.err Can\'t start a new Event Monitor daemon because it is already running. PID=$process
          		exit 1;
        	fi
	done
    
	if [ $? -gt 0 ]; then
		exit $?
	fi
    	umask 002

	# Start up the Event Monitor
      $BASEDIR/jre/bin/java -classpath $CLIENT_DIR/SMclient.jar:$CLIENT_DIR/CStor.jar -Ddevmgr.datadir=$DATA_DIR -Ddevmgr.dmv.featureOption=$FFEAT devmgr.pmlauncher.unix.PMUNIXLauncher >/dev/console 2>&1 &
	STAT=$?
	PID=$!
	if [ $STAT -ne 0 ]; then
		echo "Cannot start the Event Monitor."
		logger -t SMmonitor -p daemon.err Can\'t start the Event Monitor.
		retval=$STAT
	else
		echo "Event Monitor started."
		logger -t SMmonitor -p daemon.info Event Monitor started. PID=$PID
		retval=0
	fi
	;;

'stop')
	# Terminate the SMmonitor that may be still running
	ps -A -o "pid command" --cols 512 | grep PMUNIXLauncher | grep -v grep |
	while read line; do
		set $line x
		process=$1
		if [ $process -ne $$ ]; then
			echo "Stopping Event Monitor process $process "
			kill -9 $process
			STAT=$?
			if [ $STAT -ne 0 ]; then
				echo "Cannot stop the Event Monitor."
				logger -t SMmonitor -p daemon.err Can\'t stop the Event Monitor. PID=$process
				retval=$STAT
			fi
		fi

	done

	if [ $retval -eq 0 ]; then
		echo "The Event Monitor has been stopped."
		logger -t SMmonitor -p daemon.info The Event Monitor has been stopped.
	fi
	;;

*)
	# usage
	echo "Usage: $0 start|stop"
	retval=1
	;;

esac

exit $retval

