#!/bin/sh
#
#  SMagent
#
#  %version:            4 %
#  %date_created:       Tue May 17 10:36:34 2005 %
#
#  chkconfig: 2345 99 00
#  description: Starts and stops SMagent
#
### BEGIN INIT INFO
# Provides: SMagent
# Required-Start: $local_fs $network $syslog
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 6
# Short-Description: SMagent
# Description: Starts and stops SMagent
### END INIT INFO

BASEDIR=/opt/IBM_DS4000
AGENT_PATH=$BASEDIR/agent
JAVA_EXEC=$BASEDIR/jre/bin/java

action=$1
rval=0

#
# Make stop capability a shell function.
#
stop_agent(){
	#
	# Terminate the SMagent that may be still running
	#
	ps -A -o "pid command" --cols 256 | grep AgentLauncher | grep -v grep |
	while read line; do
		set $line x
		process=$1
		if [ $process -ne $$ ]; then
			kill -9 $process
			echo "Stopping Agent process $process."
			logger -t SMagent -p daemon.err SMagent stopped. PID=$process
		fi
	done	

}

case "$action" in
'start')
	#
	# Check to make sure there isn't another agent running already.
	# Kill and restart the agent.
	#
	stop_agent

	# Setup environment and start agent.
	LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$AGENT_PATH
	export LD_LIBRARY_PATH
	$JAVA_EXEC -classpath $AGENT_PATH/SMagent.jar devmgr.launcher.unix.AgentLauncher &
        logger -t SMagent -p daemon.err SMagent started. PID=$!
	echo "SMagent started."
;;


'stop')
	# Stop the agent.
	stop_agent
;;


*)
	# Usage message.
	echo "Usage: $0 {start|stop}"
	rval=1
;;

esac

exit $rval


