#!/bin/bash
# $Id$
#

# For RedHat and cousins:
# chkconfig: 2345 85 15
# description: RTM Client Daemon
# processname: rtmclientd
#
# source function library

# For SuSE Linux:
### BEGIN INIT INFO                         
# Provides:           rtmclientd                
# Required-Start:     mysql
# Required-Stop:                            
# Default-Start:      3 5
# Default-Stop:       0 1 2 6
# Short-Description:  Client Poller for RTM
# Description:        Start the Client Poller daemon to provide disk utilization information
# 					  for IBM Spectrum LSF RTM.
### END INIT INFO                           

RTM_TOP="/opt/IBM"
PROG_BIN="rtmclientd"
PROG_PATH="$RTM_TOP/rtm/client/bin/"
LIC_PATH="$RTM_TOP/rtm/client/bin/"
OPTIONS="-c $RTM_TOP/rtm/client/etc/rtmc.conf"
export LD_LIBRARY_PATH=$prog_path:$LD_LIBRARY_PATH
START_CMD="$PROG_PATH$PROG_BIN $OPTIONS"
SYSTEMCTL_SKIP_REDIRECT=Y
HTTPD_USER=`grep Daemon_User $RTM_TOP/rtm/client/etc/rtmc.conf | awk '{print $NF}'`
HTTPD_HOME=`getent passwd $HTTPD_USER | cut -f6 -d":"`
export HOME=$HTTPD_HOME

if [ -f /etc/SuSE-release ] || `grep SUSE -q /etc/os-release`; then
	test -x $PROG_PATH$PROG_BIN || exit 5

	# Source LSB init functions, if they exist
	[ -f /etc/rc.status ] && . /etc/rc.status

	# Reset status of this service
	rc_reset

	case "$1" in
	start) 
		echo -n "Starting RTM Client Poller Daemon "
		/sbin/startproc $START_CMD
		rc_status -v
		;;
	stop) 
		echo -n "Shutting down RTM Client Poller Daemon "
		## Stop the disku collector processes first
		for clientbin in `find $LIC_PATH -type f -name "disku*" | grep -v conf`; do
			pid=`pidof $clientbin`
			if [ "$pid" != "" ]; then
				/bin/kill $pid
			fi
		done
		sleep 1
		for clientbin in `find $LIC_PATH -type f -name "disku*" | grep -v conf`; do
			pid=`pidof $clientbin`
			if [ "$pid" != "" ]; then
				sleep 3
				/bin/kill -9 $pid
			fi
		done

		## 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
		;;
	try-restart|condrestart)
		if test "$1" = "condrestart"; then
			echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
		fi
		$0 status
		if test $? = 0; then
			$0 restart
		else
			rc_reset
		fi
		rc_status
		;;
	restart)
		$0 stop
		$0 start
		rc_status
		;;
	force-reload)
		echo -n "Reload service RTM Client Poller Daemon "
		$0 try-restart
		rc_status
		;;
	reload)
		echo -n "Reload service RTM Client Poller Daemon "
		rc_failed 3
		rc_status -v
		;;
	status)
		echo -n "Checking for service RTM Client Poller Daemon "
		/sbin/checkproc $PROG_BIN
		rc_status -v
		;;
	probe)
		test $RTM_TOP/rtm/client/etc/rtmc.conf -nt /var/run/rtmclientd.pid && echo reload
		;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
		exit 1
		;;
	esac

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

	RETVAL=0
	
	case "$1" in
	start)
		status rtmclientd  >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo -n "Starting RTM Client Poller Daemon "
			daemon $PROG_PATH$PROG_BIN $OPTIONS
			RETVAL=$?
			[ $RETVAL -eq 0 ] && touch /var/lock/subsys/rtmclientd
			echo
		fi
		;;
	stop)
		echo -n "Shutting down RTM Client Poller Daemon "
		## Stop the disku collector processes first
		for clientbin in `find $LIC_PATH -type f -name "disku*" | grep -v conf`; do
			pid=`pidof $clientbin`
			if [ "$pid" != "" ]; then
				/bin/kill $pid
			fi
		done
		sleep 1
		for clientbin in `find $LIC_PATH -type f -name "disku*" | grep -v conf`; do
			pid=`pidof $clientbin`
			if [ "$pid" != "" ]; then
				sleep 3
				/bin/kill -9 $pid
			fi
		done

		killproc $PROG_BIN
		RETVAL=$?
		[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/rtmclientd
		echo
		;;
	reload)
		echo -n "Reload service RTM Client Poller Daemon "
		$0 stop
		$0 start
		;;
	report)
		echo -n $"Checking SMART devices now: "
		killproc $SMARTD_BIN -USR1
		RETVAL=$?
		echo
		;;
	restart)
		$0 stop
		$0 start
		;;
	status)
		echo -n "Checking for service RTM Client Poller Daemon "
		status $PROG_BIN
		RETVAL=$?
		;;
	*)
		echo $"Usage: $0 {start|stop|reload|report|restart|status}"
		RETVAL=1
	esac
	
	exit $RETVAL
elif [ -f /etc/lsb-release ] ; then
	[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions

	case "$1" in
	start)
		echo -n "Starting RTM Client Poller Daemon "
		start-stop-daemon  --start --quiet --exec $PROG_PATH$PROG_BIN -- \
			$OPTIONS  \
					|| RETVAL=2
		echo $RETVAL
		RETVAL=0
		;;
	stop)
		echo -n "Shutting down RTM Client Poller Daemon "
		## Stop the disku collector processes first
		for clientbin in `find $LIC_PATH -type f -name "disku*" | grep -v conf`; do
			pid=`pidof $clientbin`
			if [ "$pid" != "" ]; then
				/bin/kill $pid
			fi
		done
		sleep 1
		for clientbin in `find $LIC_PATH -type f -name "disku*" | grep -v conf`; do
			pid=`pidof $clientbin`
			if [ "$pid" != "" ]; then
				sleep 3
				/bin/kill -9 $pid
			fi
		done
		start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $PROG_PATH$PROG_BIN
		start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $PROG_PATH$PROG_BIN
		[ "$?" = 0 ] && echo success || echo failure
		;;

	restart)
		$0 stop
		$0 start
		;;
	status)
		echo -n "Checking for service RTM Client Poller Daemon "
		status_of_proc  $PROG_BIN && exit 0 || exit $?
		RETVAL=$?
		;;

	esac
	exit $RETVAL
fi
