#!/bin/sh
#chkconfig: 2345 55 80
#description: LSI SNMP Agent startup/shutdown script
#Source function library.
. /etc/init.d/functions   

megadevice="megasnmp"
agent="/usr/sbin/lsi_mrdsnmpagent"
agent_name="lsi_mrdsnmpagent"
pidfile="mrdsnmpd"
vendor="LSI SNMP"	
RETVAL=0
SNMPDCONF=/etc/snmp/snmpd.conf


#A function to stop a program.
statuscheck() {
	local base=${1}
	local pid

	# Test syntax.
	if [ "$#" = 0 ] ; then
		echo $"Usage: status {program}"
		return 1
	fi

	# First try "pidof"
	pid=`pidof -o $$ -o $PPID -o %PPID -x $1 || \
	     pidof -o $$ -o $PPID -o %PPID -x ${base}`
	if [ -n "$pid" ]; then
	        echo $"${base} (pid $pid) is running..."
	        return 0
	fi

	# Next try "/var/run/*.pid" files
	if [ -f /var/run/${base}.pid ] ; then
	        read pid < /var/run/${base}.pid
	        if [ -n "$pid" ]; then
	                echo $"${base} dead but pid file exists"
	                return 1
	        fi
	fi
	# See if /var/lock/subsys/${base} exists
	if [ -f /var/lock/subsys/${base} ]; then
		echo $"${base} dead but subsys locked"
		return 2
	fi
	echo $"${base} is stopped"
	return 3
}

mykillproc() 
{
	RC=0
	# Test syntax.
	if [ $# = 0 ]; then
		echo "Usage: killproc {program} [signal]"
		return 1
	fi

	notset=0
	# check for second arg to be kill level
	if [ "$2" != "" ] ; then
		killlevel=$2
	else
		notset=1
		killlevel="-9"
	fi

        # Save basename.
        base=`basename $1`

        # Find pid.
	pid=`pidof $1 | cut -f1 -d" "`	

        # Kill it.
        if [ "$pid" != "" ] ; then
                [ "$BOOTUP" = "verbose" ] && echo -n "$base "
		if [ "$notset" = "1" ] ; then
		       if ps h $pid >/dev/null 2>&1; then
			   # TERM first, then KILL if not dead
			   kill -TERM $pid
			   usleep 200000
			   if ps h $pid >/dev/null  >/dev/null 2>&1 ; then
				sleep 1
				if ps h $pid >/dev/null  >/dev/null 2>&1 ; then
				        sleep 3
					if ps h $pid >/dev/null  >/dev/null 2>&1 ; then
					   kill -KILL $pid
					   usleep 200000
					fi
				fi
			   fi
		        fi
			ps h $pid >/dev/null  >/dev/null 2>&1
			RC=$?
			[ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
			RC=$((! $RC))
		# use specified level only
		else
		        if ps h $pid >/dev/null  >/dev/null 2>&1; then
	                	kill $killlevel $pid
				RC=$?
				[ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
			fi
		fi
	else
	    failure "$base shutdown"
	fi

        # Remove pid file if any.
	if [ "$notset" = "1" ]; then
            rm -f /var/run/$base.pid
	fi
	return $RC
}


start()
{
	echo -n "Starting $vendor Agent:"
	spid=`pidof snmpd`
	if [ "$spid" = "" ] ; then 
		if [ -x /etc/init.d/snmpd ] ; then 
			/etc/init.d/snmpd start
		else
			echo SNMP Daemon not started
		fi
	fi
	daemon ${agent} -c ${SNMPDCONF}
	echo
}

stop()
{
	echo -n "Stopping $vendor Agent:"
	killproc ${agent}
	echo
	return 0
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	$0 stop
	$0 start
	;;
 status)
      
        statuscheck ${agent_name}
	RETVAL=$?
        ;;
  *)
	echo "$vendor Agent is not Started/Stopped"
      echo "Usage: $0 {start|stop|restart|status}"
	exit 1
esac

exit $RETVAL
