#! /bin/sh
# Copyright (c) Fujitsu Technology Solutions 2009-2013
#
# /etc/init.d/aurad
#
### BEGIN INIT INFO
# Provides:          aurad
# Required-Start:    $network $syslog +snmp +xinetd +iomemory-vsl
# Required-Stop:     $network +iomemory-vsl
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Description:       aurad - Daemon for ServerView RAID Manager
### END INIT INFO

# Our working directory
WORKDIR=/opt/fujitsu/ServerViewSuite/RAIDManager

# Our executable directory
EXECDIR=/usr/sbin

# Force execution if not called by a runlevel directory.
test -x $EXECDIR/amDaemon || exit 0

. /etc/rc.status

case "$1" in
	start)
		echo -n "Starting ServerView RAID Manager: "
		pid=`pidof amDaemon`
		if [ -n "$pid" ]
		then
			# amDaemon is running
			rc_failed 1
			rc_status -v
			rc_exit
			exit 0
		fi

		export LD_LIBRARY_PATH=$WORKDIR/bin
		export MIBDIRS=/usr/share/snmp/mibs
		export MIBS=ALL

		# Need mptctl module and character special /dev/mptctl
		mpt1ctlpath=/dev/mptctl
		mpt2ctlpath=/dev/mpt2ctl
		major=10
		minor=220

		modprobe mptctl > /dev/null 2>&1

		if [ ! -e $mpt1ctlpath ]
		then
			mknod $mpt1ctlpath c $major $minor > /dev/null 2>&1
		fi
		minor=`/usr/bin/expr $minor + 1`
		if [ ! -e $mpt2ctlpath ]
		then
			mknod $mpt2ctlpath c $major $minor > /dev/null 2>&1
		fi

		export SNMPCONFPATH=$WORKDIR/bin
		export OPENSSL_CONF=/etc/pki/tls/openssl.cnf

		######################################################################################################
		## The next two lines are responsible to write a core under / if a segmentation fault happens
		######################################################################################################
		export DAEMON_COREFILE_LIMIT=unlimited
		ulimit -c unlimited
		######################################################################################################

		###########################
		## start daemon
		###########################
		startproc $EXECDIR/amDaemon
		######################################################################################################
		## For additional console output use following code line but set the
		## line "startproc $EXECDIR/amDaemon" (two lines above) in comment
		######################################################################################################
		##$EXECDIR/amDaemon -k 2>&1 | tee -a $EXECDIR/amDaemon.log

		if [ ! -e /var/lock/subsys/aurad ]
		then
			touch /var/lock/subsys/aurad
		fi
		rc_status -v
		;;
	stop)
		echo -n "Shutting down ServerView RAID Manager: "
		pid=`pidof amDaemon`
		if [ ! -n "$pid" ]
		then
			# amDaemon is not running
			rc_failed 1
			rc_status -v
			rc_exit
			exit 0
		fi

		###################################################################################
		## The following lines with kill -TERM make sometimes problems on RH with snmpd and
		## slowly plugins by shutdown. Then if unlimited set by start the kill -TERM force
		## a segmentation fault. If so set the following lines in comment and stop only
		## with kill -KILL.
		###################################################################################
		killproc -t 35 $EXECDIR/amDaemon -TERM

		###################################################################################

		pid=`pidof amDaemon`
		if [ -n "$pid" ]
		then
			killproc -t 15 -KILL $EXECDIR/amDaemon > /dev/null 2>&1
		fi
		if [ -e /var/lock/subsys/aurad ]
		then
			/bin/rm -f /var/lock/subsys/aurad > /dev/null 2>&1
		fi
		if [ -e /var/run/aurad.pid ]
		then
			/bin/rm -f /var/run/aurad.pid > /dev/null 2>&1
		fi
		if [ -e /RAID.LOG ]
		then
			/bin/rm -f /RAID.LOG > /dev/null 2>&1
		fi
		rc_status -v
		;;
	restart)
		$0 stop
		$0 start
		rc_status
		;;
	try-restart)
		$0 status
		if test $? = 0; then
		$0 restart
		else
		rc_reset
		fi
		rc_status
		;;
	force-reload)
		$0 stop; sleep 1 && $0 start
		rc_status
		;;
	reload)
		$0 stop; sleep 1 && $0 start
		rc_status -v
		;;
	status)
		echo -n "Checking for ServerView RAID Manager: "
		checkproc $EXECDIR/amDaemon
		rc_status -v
		;;
	*)
		echo "Usage: $0 {start|stop|try-restart|restart|force-reload|reload|status}"
		exit 1
esac
rc_exit
