#!/bin/sh
#
#  SMmonitor
#
#  %version:           5 %
#  %date_created:      Mon Jun 06 14:25:11 2005 %
#
#  chkconfig: 2345 99 00
#  description: Starts and stops SMmonitor
### BEGIN INIT INFO
#  Provides: SMmonitor
#  Required-Start: $local_fs $network $syslog
#  Should-Start:
#  Required Stop:
#  Default-Start: 2 3 4 5
#  Default-Stop: 0 6
#  Short-Description: SMmonitor
#  Description: Starts and stops SMmonitor
### END INIT INFO

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

script=`basename $0`
action=$1
retval=0
outdev=/dev/console
case "$action" in
'start')
	#Check to see if there exists running monitor
	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 $script because $script is already running."
          		logger -t $script -p daemon.err Cannot start $script 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
	# If /dev/console does not exist redirect to /dev/null
	if ! test -e /dev/console; then
        	outdev=/dev/null
   	fi
      
      $BASEDIR/jre/bin/java -classpath $CLIENT_DIR/SMclient.jar -Ddevmgr.datadir=$DATA_DIR -Ddevmgr.dmv.featureOption=$FFEAT devmgr.pmlauncher.unix.PMUNIXLauncher >$outdev 2>&1 &
	STAT=$?
	PID=$!
	if [ $STAT -ne 0 ]; then
		echo "Cannot start $script."
		logger -t $script -p daemon.err Cannot start $script. 
		retval=$STAT
	else
		echo "$script started."
		logger -t $script -p daemon.info $script started. PID=$PID
		retval=0
	fi
	;;

'stop')
	# Terminate the Event Monitor 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 $script process $process "
			kill -9 $process
			STAT=$?
			if [ $STAT -ne 0 ]; then
				echo "Cannot stop $script."
				logger -t $script -p daemon.err Cannot stop $script. PID=$process
				retval=$STAT
			fi
		fi

	done

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

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

esac

exit $retval

