#!/bin/bash
#
# chkconfig: - 99 01
# description: bnasd SNMP agentX sub-agent daemon
#
### BEGIN INIT INFO
# Provides:		bnasd
# Required-Start:	snmpd
# Required-Stop:
# Default-Start:	3 5
# Default-Stop:		0 1 2 6
# Short-Description:	Brocade 10G Network Adapter SNMP sub-agent
# Description:		Processes requests for MIB in BROCADE-CNA.mib
#		
### END INIT INFO

# Name of the Brocadde SNMP sub-agent
prog=bnasd

# Path of the Brocade SNMP sub-agent
prog_bin=/usr/bin/$prog
pidfile=/var/run/$prog.pid

if [ -f /etc/redhat-release ]; then
	# Source function library
	. /etc/rc.d/init.d/functions

	version="`rpm --queryformat="%{version}" -qf /etc/redhat-release`"

	retval=0

	start()
	{
		echo -n $"Starting $prog: "
		daemon $prog_bin
		retval=$?
		echo
		[ $retval = 0 ] && touch $pidfile
		return $retval
	}

	stop()
	{
		echo -n $"Shutting down $prog: "
		if [[ "$version" =~ "^4" ]]; then
			killproc $prog_bin
		else
			killproc -p $pidfile
		fi
		retval=$?
		echo
		rm -f $pidfile
		return $retval
	}

	case "$1" in
		start)
			start
			;;
		stop)
			stop
			;;
		restart)
			stop
			start
			;;
		status)
			status $prog
			retval=$?
			;;
		*)
			echo $"Usage: $0 {start|stop|restart|status}"
			retval=3
	esac

	exit $retval
elif [ -f /etc/SuSE-release ]; then
	test -x $prog_bin || { echo "$prog_bin not installed";
				if [ "$1" = "stop" ]; then exit 0;
        			else exit 5; fi; }

	# source functions from /etc/rc.status
	. /etc/rc.status

	# Reset status of this service
	rc_reset

	case "$1" in
	    start)
		echo -n "Starting $prog "
		## Start daemon with startproc(8). If this fails
		## the return value is set appropriately by startproc.
		/sbin/startproc $prog_bin

		# Remember status and be verbose
		rc_status -v
		;;
	    stop)
		echo -n "Shutting down $prog "
		## Stop daemon with killproc(8) and if this fails
		## killproc sets the return value according to LSB.

		/sbin/killproc -TERM $prog_bin

		# Remember status and be verbose
		rc_status -v
		;;
	    restart)
		## Stop the service and regardless of whether it was
		## running or not, start it again.
		$0 stop
		$0 start

		# Remember status and be quiet
		rc_status
		;;
	    status)
		echo -n "Checking for service $prog "
		## Check status with checkproc(8), if process is running
		## checkproc will return with exit status 0.

		# Return value is slightly different for the status command:
		# 0 - service up and running
		# 1 - service dead, but /var/run/  pid  file exists
		# 2 - service dead, but /var/lock/ lock file exists
		# 3 - service not running (unused)
		# 4 - service status unknown :-(
		# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
		
		# NOTE: checkproc returns LSB compliant status values.
		/sbin/checkproc $prog_bin
		# NOTE: rc_status knows that we called this init script with
		# "status" option and adapts its messages accordingly.
		rc_status -v
		;;
	    *)
		echo "Usage: $0 {start|stop|status|restart}"
		exit 1
		;;
	esac
	rc_exit
else
	# should never come here
	echo "unsupported operating system"
fi
