#! /bin/sh
# Copyright (c) Fujitsu Technology Solutions 2009-2013
#
# /etc/init.d/aurad
#
# chkconfig:         2345 56 41
# description:       aurad - Daemon for ServerView RAID Manager

# 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.d/init.d/functions

case "$1" in
	start)
		echo -n "Starting ServerView RAID Manager: "
		pid=`pidof amDaemon`
		if [ -n "$pid" ]
		then
			# amDaemon is running
			failure
			echo
			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
		###########################
		daemon $EXECDIR/amDaemon
		######################################################################################################
		## For additional console output use following code line but set the
		## line "daemon $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
		echo
		;;
	stop)
		echo -n "Stopping ServerView RAID Manager: "
		pid=`pidof amDaemon`
		if [ ! -n "$pid" ]
		then
			# amDaemon is not running
			failure
			echo
			exit 0
		fi

		touch /tmp/flag_svrack_invalid

		###################################################################################
		## 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 -d 15 $EXECDIR/amDaemon -TERM
		nwait=35
		while [ -n "`ps aux | fgrep amDaemon | grep -v grep`" ]
		do
			sleep 1
			nwait=$(($nwait-1))
			if [ $nwait -eq 0 ]
			then
				break
			fi
		done

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

		if [ -n "`ps aux | fgrep amDaemon | grep -v grep`" ]
		then
			killproc -d 15 $EXECDIR/amDaemon -KILL > /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
		if [ -e /tmp/flag_svrack_invalid ]
		then
			/bin/rm -f /tmp/flag_svrack_invalid > /dev/null 2>&1
		fi
		success
		echo
		;;
	restart)
		$0 stop
		$0 start
		;;
	try-restart)
		$0 status
		if test $? = 0; then
		$0 restart
		fi
		;;
	force-reload)
		$0 stop; sleep 1 && $0 start
		;;
	reload)
		$0 stop; sleep 1 && $0 start
		;;
	status)
		echo -n "Checking for ServerView RAID Manager: "
		status $EXECDIR/amDaemon
		;;
	*)
		echo "Usage: $0 {start|stop|try-restart|restart|force-reload|reload|status}"
		exit 1
esac
