#!/bin/sh
#
# adaptec-smbe        Starts and stops arcpd, anotifyd, aiomgrd.
#
# chkconfig: 35 99 99
# description: adaptec-smbe is used to start Adaptec Storage Manager - Browser Edition daemons
### BEGIN INIT INFO
# Provides:       adaptec-smbe
# Required-Start:
# Required-Stop:
# X-UnitedLinux-Should-Start:
# X-UnitedLinux-Should-Stop:
# Default-Start:  3 5
# Default-Stop:
# Description:    adaptec-smbe is used to start Adaptec Storage Manager - Browser Edition daemons
### END INIT INFO

scriptName=adaptec-smbe
installRoot=`ls -l /usr/sbin/arcpd | cut -f2- -d'>' | sed "s/\/sbin.*//" | sed "s/^ *//"`

#
# For now search for the daemons that are enabled since they are the only
# ones that can possibly be enabled after any scripting. This will easily
# allow some scripting in the daemonStartup script with the restriction that
# there can only be one 'on' daemon in the daemonStartup script otherwise
# this script will start that daemon for each instance that it finds on.
#
daemonStartList="`for f in \`grep ".*=.*on.*" ${installRoot}/etc/daemonStartup\`; do echo $f | cut -f1 -d'='; done`"

# Check the daemonStartup file to see which daemons get turned off
daemonStartup="${installRoot}/etc/daemonStartup"
if [ -f "${daemonStartup}" ]; then
	. "${daemonStartup}"
	for d in ${daemonStartList}; do
		eval startupType=\$${d}
		if [ "${startupType}" = "off" ]; then
			# If daemon is set to "off", remove it from our list to start/stop.
			daemonStartList="`echo ${daemonStartList} | sed \"s/${d}//\"`"
		fi
	done
fi

#
# Now reverse the order of the daemons to generate the stop list
#
daemonStopList=""
for d in  ${daemonStartList}; do
	daemonStopList="$d ${daemonStopList}"
done



adaptec_daemon_start () {
	# echo -n "	`basename $1`: "

	#
	# Ensure that the group id on the configuration files used by
	# the daemons and the group id of the process that started the
	# daemons are the same. On OpenServer and OpenUnix the default 
	# group id for root is sys, but when the daemons are started 
	# on boot up, root has group id of root (which we want).
	#
	mygid=`id|sed -e "s/^uid=[0-9]*(\([^)]*\)) gid=[0-9]*(\([^)]*\).*/\2/"`
	if [ x"$mygid" = x"root" ]; then
		eval $1
	else
		echo "exec $1" | newgrp root
	fi

	if [ $? -eq 0 ]; then
		echo -n "		[ OK ]"
	else
		echo -n "		[ FAILED ]"
	fi
	echo
}

start_daemon=""
kpAvail="no"
if [ -f /etc/rc.d/init.d/functions -a "${RUNNING_FROM_GUI}" != "1" ]; then
	. /etc/rc.d/init.d/functions
	# killproc and daemon are defined in functions
	kpAvail="yes"
	start_daemon="daemon"
else
	# adaptec_daemon_start can be above
	start_daemon="adaptec_daemon_start"
fi

#
# adaptec-smbe.functions is to provide functions that are needed
# so that adaptec-smbe can be UNIX independent.
#
if [ -f /etc/init.d/adaptec-smbe.functions ]; then
	. /etc/init.d/adaptec-smbe.functions
elif [ -f /etc/rc.d/init.d/adaptec-smbe.functions ]; then
	. /etc/rc.d/init.d/adaptec-smbe.functions
fi



start() {
	# Update the ssl.conf file in case there is a new hostname
	HOSTNAME=`hostname`; export HOSTNAME
	if [ "`grep "${HOSTNAME}" ${installRoot}/etc/ssl.conf`" != "" ]; then
		awk '{
			if (0 == match($0, "id=\"CN\"")) {
				print $0
			}
			else {
				print $0;
				getline;
				print "                    "ENVIRON["HOSTNAME"]"\\n";
			}
		}' ${installRoot}/etc/ssl.conf > /tmp/ssl.conf && mv -f /tmp/ssl.conf ${installRoot}/etc/ssl.conf && chmod 600 ${installRoot}/etc/ssl.conf
	fi

	DESKTOP_FILE="Adaptec Storage Manager - Browser Edition .desktop"
	if [ "`uname`" = "Linux" -a -f "/etc/X11/applnk/System/${DESKTOP_FILE}" ] && ! grep "${HOSTNAME}" "/etc/X11/applnk/System/${DESKTOP_FILE}" > /dev/null; then
		sed "s,\(.*\)//.*:\(.*\),\1//${HOSTNAME}:\2," "/etc/X11/applnk/System/${DESKTOP_FILE}" > /tmp/a && mv -f /tmp/a "/etc/X11/applnk/System/${DESKTOP_FILE}"
		sed "s,\(.*\)//.*:\(.*\),\1//${HOSTNAME}:\2," "/root/.gnome-desktop/${DESKTOP_FILE}" > /tmp/a && mv -f /tmp/a "/root/.gnome-desktop/${DESKTOP_FILE}"
	fi

	# Delete any lock files generated.  This might affect other applications
	# and daemons, but in practice the duration of these LCK files are very
	# short so the window of failure is very small.
	rm -rf ${installRoot}/etc/*LCK*

	# Create device nodes
	if [ -f "${installRoot}/bin/MAKEDEV.aac" ]; then
		for minor in 0 1 2 3 4 5 6 7; do
			rm -f /dev/aac${minor} && "${installRoot}/bin/MAKEDEV.aac" aac$minor > /dev/null 2>&1
		done
	fi

	echo "Starting ${scriptName} daemons:"

	# Change to the directory where all our daemons will reside
	cd "${installRoot}/sbin"

	# Start the daemons
	for daemon in $daemonStartList; do
		if [ -x "./${daemon}" ]; then
			echo -n "	${daemon}:" # tab over for each daemon
			WAIT_TIME=0
			if [ "${daemon}" = "anotifyd" ]; then
				# Allow arcpd a little time to start up
				# before starting up the notifier
				WAIT_TIME=2
				if [ -f "${installRoot}/sbin/popup.sh" ]; then
					sh "${installRoot}/sbin/popup.sh" > /dev/null 2>&1
				fi
			fi
			sleep $WAIT_TIME

			${start_daemon} "./${daemon}"
			if [ $? -ne 0 ]; then
				exit $?
			fi
			echo
		else
			# Special case for the AAC snmp Daemon. Use modified start
			# script if it exists. It should exist for OpenServer and
			# OpenUNIX.
			if  [ "${daemon}" = "aacsnmpd" -a -f ${installRoot}/etc/aacsnmp_start.sh ]; then
				${installRoot}/etc/aacsnmp_start.sh
			fi
		fi
	done

}

stop() {
	echo "Shutting down ${scriptName} daemons:"
	for daemon in ${daemonStopList}; do
		pid=`pidof ${daemon}`
		if [ "${pid}" = "" ]; then
			continue
		fi
		echo -n "	${daemon}: "
		if [ "${start_daemon}" != "" -a "${kpAvail}" = "yes" -a "${RUNNING_FROM_GUI}" != "1" ]; then
			killproc ${daemon} 2> /dev/null
		else
			#
			# Sometimes our daemons need to be told more than once to 
			# shutdown nicely on OpenUnix. Also, cannot rely on kill 
			# returning a bad status if it cannot stop the daemon. But
			# kill will return a bad status if a prevous kill finally 
			# took effect (i.e. kill/sleep/kill sequence).
			#
			for sleeptime in 1 2 2 1
			do
				kill -TERM ${pid} 2> /dev/null 
				pid=`pidof ${daemon}`
				if [ "${pid}" = "" ]; then
					break
				fi
				sleep $sleeptime 
			done

			#
			# See if we need to be forceful ....
			#
			pid=`pidof ${daemon}`
			if [ "${pid}" != "" ]; then
				sleep 1
				#
				# make sure the daemon did not go away ....
				#
				pid=`pidof ${daemon}`
				if [ "${pid}" != "" ]; then
					kill -KILL ${pid} 2> /dev/null
					#
					# Let the daemon have time to die, we don't have 
					# microsleep (usleep) like Linux
					#
					sleep 1
				fi
			fi

			#
			# Make sure it is indeed gone ...
			#
			if [ "${pid}" != "" ]; then
				pid=`pidof ${daemon}`
			fi

			if [ "${pid}" = "" ]; then
				echo -n "		[ OK ]"
			else
				echo -n "		[ FAILED ]"
			fi
		fi
		echo
	done
	if [ "`uname`" = "Linux" ]; then
		kill `pidof apopupd` > /dev/null 2>&1
	fi
	rm -rf ${installRoot}/etc/*LCK*
	echo
}

restart() {
	stop
	sleep 2
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		restart
		;;
	*)
		echo "Usage: adaptec-smbe {start|stop|restart|reload}"
		exit 1
esac

exit 0
