#!/bin/bash
#
# init.d script for smad
#

# 
# chkconfig: 345 70 20
# description: agent daemon
#

### BEGIN INIT INFO
# Provides:          smad
# Required-Start:    $network $syslog $local_fs $remote_fs
# Required-Stop:     $network $syslog $local_fs $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: smad: agent daemon 
# Description:	     smad: agent daemon 
### END INIT INFO

# Redhat or SuSE?
if [ -f /etc/redhat-release ]; then
	REDHAT=1
	. /etc/init.d/functions
	RETVAL=0
elif [ -f /etc/SuSE-release ]; then
	SUSE=1
	. /etc/rc.status
	rc_reset
else
	echo "Unsupported distribution"
	exit 0
fi

# Our program.
INSTALLDIR=
MODE=SMA
LOGLEVEL=0
OPTIONS="$MODE $LOGLEVEL -d"
PROG=smad
EXEPATH=$INSTALLDIR/$PROG

# The IMB module.
IMBDIR=$INSTALLDIR/drivers
IMBMODULE=imb
IMBPATH=$IMBDIR/$IMBMODULE
IMBDEV=/dev/imb

# Check for existance.
test -x $EXEPATH || { echo "$EXEPATH not installed";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Function to handle unsupported operation.
smad_usage() {
	if [ $REDHAT ]; then
		echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
		RETVAL=1
	else
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	fi
}

# Function to check and load the IMB driver if needed.
smad_loadimb() {

	if [ ! -e $IMBPATH ]; then
		return 1
	fi

	lsmod | grep $IMBMODULE >& /dev/null
	if [ $? -ne 0 ]; then
		insmod $IMBPATH >& /dev/null
 		if [ $? -ne 0 ]; then
			return 2
		fi
	fi

	MAJ=`cat /proc/devices | awk '/imb/ {print $1}'`
	if [ ! -c $IMBDEV ]; then
		/bin/mknod $IMBDEV c $MAJ 0
		if [ $? -ne 0 ]; then
			return 3
		fi
	fi

	return 0
}

# Function to unload the IMB driver.
smad_unloadimb() {

	rmmod $IMBMODULE >&/dev/null
	if [ -e $IMBDEV ]; then
		rm -f $IMBDEV
	fi
}

# Perform called operation.
case "$1" in
  start)
	echo -n $"Starting $PROG: "
	smad_loadimb
	# echo "smad_loadimb: $?"
	export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${INSTALLDIR}"
	if [ $REDHAT ]; then
		if [ $UID -ne 0 ]; then
			RETVAL=1
			failure
        	else
			daemon $EXEPATH $OPTIONS
			RETVAL=$?
			[ $RETVAL -eq 0 ] && touch /var/lock/subsys/smad
		fi;
		echo 
	else
		startproc $EXEPATH $OPTIONS
		rc_status -v
	fi
	;;
  stop)
	echo -n $"Stopping $PROG: "
	if [ $REDHAT ]; then
		if [ $UID -ne 0 ]; then
			RETVAL=1
			failure
		else
			killproc $EXEPATH
			RETVAL=$?
			[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/smad
		fi;
		echo
	else
		killproc -TERM $EXEPATH
		rc_status -v
	fi
	;;
  restart)
	$0 stop
	$0 start
	if [ $SUSE ]; then
		rc_status
	fi
	;;
  reload)
	echo -n $"Reloading $PROG: "
	if [ $REDHAT ]; then
		killproc $EXEPATH -HUP
		RETVAL=$?
		echo
	fi
	#else
		# If daemon does not support, do nothing.
		#killproc -HUP $EXEPATH
		#rc_status -v
	#fi
	;;
  condrestart)
	if [ $REDHAT ]; then
		if [ -e /var/lock/subsys/smad ]; then
			$0 restart
		fi
	else
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
		$0 status
		if test $? = 0; then
			$0 restart
		else
			rc_reset        # Not running is not a failure.
		fi
		rc_status
	fi
	;;
  status)
	if [ $REDHAT ]; then
		status $EXEPATH
		RETVAL=$?
	else
		checkproc $EXEPATH 
		rc_status -v
	fi
	;;
	#
	# smad-only
	#
  unloadimb)
	smad_unloadimb
	if [ $REDHAT ]; then
		RETVAL=$?
	else
		rc_status -v
	fi
	;;
  uninstall)
	$0 stop
	smad_unloadimb
	rm -f $INSTALLDIR/*.log* 
	if [ $REDHAT ]; then
		RETVAL=$?
	else
		rc_status -v
	fi
	;;
	#
	# LSB-only commands below.
	#
  try-restart)
	if [ $REDHAT ]; then
		sma_usage
	else
		$0 status
		if test $? = 0; then
			$0 restart
		else
			rc_reset        # Not running is not a failure.
		fi
		rc_status
	fi
	;;
    force-reload)
	if [ $REDHAT ]; then
		sma_usage
	else
		## Signal the daemon to reload its config. Most daemons
		## do this on signal 1 (SIGHUP).
		## If it does not support it, restart.
		echo -n "Reload service $PROG"
		$0 try-restart
		rc_status
	fi
	;;
  *)
	smad_usage
	;;
esac

if [ $REDHAT ]; then
	exit $RETVAL
else
	rc_exit
fi

