#!/bin/sh
#
#  SMmonitor
#
#  %version:           18 %
#  %date_created:      Thu Jul 12 03:53:09 2012 %
#
#  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_DS
CLIENT_DIR=$BASEDIR/client
TEMP_DIR=/tmp
DATA_DIR=/var/opt/SM
FFEAT=FULL_SA
STORAGE_MANAGER_TYPE=5

script=SMmonitor
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 -DstorageManager=$STORAGE_MANAGER_TYPE -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
	    echo "$PID" > /var/run/$script.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

		if [ $retval -eq 0 ]; then
			echo "$script has been stopped."
			logger -t $script -p daemon.info $script has been stopped.
			if [ -f /var/run/$script.pid ]; then
				rm -f /var/run/$script.pid
			fi
		fi
	done

	;;

'restart')
	# 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

		if [ $retval -eq 0 ]; then
			echo "$script has been stopped."
			logger -t $script -p daemon.info $script has been stopped.
			if [ -f /var/run/$script.pid ]; then
				rm -f /var/run/$script.pid
			fi
		fi
	done

	#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 -DstorageManager=$STORAGE_MANAGER_TYPE -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
	    	echo "$PID" > /var/run/$script.pid
		retval=0
	fi
;;

'status')
    if [ -f /var/run/$script.pid ]; then
        ps `cat /var/run/$script.pid` >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "$script (pid `cat /var/run/$script.pid`) is running..."
            rval=0
   		else
			echo "$script is stopped"
        fi
	fi

	if [ ! -f /var/run/$script.pid ]; then
		echo "$script is stopped"
	fi

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

esac

exit $retval

