#!/bin/sh
#
#	/etc/rc.d/init.d
#
# Automatic start of the RAC Event server in a running Linux system
#
# chkconfig: 2345 98 02
# description: Activates the RAC Event server which \
#              allows redirection of the console output.
# processname: racsrvc
# config: /etc/sysconfig/racsrvc

# Source function library.
. /etc/rc.d/init.d/functions

# Source DRS configuration with all necessary variables.
if [ -f /etc/sysconfig/racsrvc ]; then
	. /etc/sysconfig/racsrvc
fi

# Check that drs is up.
#[ ".${DRS}" = ".no" ] && exit 0

RETVAL=0
FILE="/var/run/.racsrvc"

# See how we were called.
case "$1" in
  start)
	# Before starting RAC Event Server check whether it is already running if yes exit
	if [ -e $FILE ]; then
		#verify whether the process with the PID mentioned exists if yes exit
		read x < $FILE
		kill -s CONT $x > /dev/null 2>&1
		if [ $? == 0 ]; then
			echo "racsrvc (RAC) service daemon : Already running"
			exit 4;
		fi
	fi
	echo -n "Starting racsrvc (RAC) service daemon: "
	# determine the preferred spcmp interface
	optFile=/etc/sysconfig/racsrvc
	if [ ! -e $optFile ]; then
		spcmpif=DEFAULT
	else
		for spcmpif in I2C SOCKET AUTO DEFAULT
		{
			grep -e "^#[ \t]*USE_SPCMP=$spcmpif\$" $optFile >/dev/null 2>&1
			rc=$?
			if [ $rc -eq 0 ]; then
				break
			fi
		}
	fi
	if [ "$?" -ne 0 ]; then
		exit 4
	fi

	# start the RAC Event Server, with 5 sec delay
	daemon racsrvc -w5 -z$spcmpif
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		echo "'daemon racsrvc' command failed"
		exit 4
	fi
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/racsrvc
	;;
  stop)
	#stop only if the process is running otherwise no need to stop anything
	if [ -e $FILE ]; then
		#verify whether the process with the PID mentioned exists if yes exit
		read x < $FILE
		kill -s CONT $x > /dev/null 2>&1
		if [ $? == 0 ]; then
			echo -n "Stopping racsrvc (RAC) service daemon: "
			killproc racsrvc
			RETVAL=$?
			echo
			[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/racsrvc
			[ -e /var/run/.racsrvc ] && rm -f /var/run/.racsrvc
		fi
	fi
	;;
  status)
	status racsrvc
	RETVAL=$?
	;;
  restart|reload)
	$0 stop
	$0 start
	RETVAL=$?
	;;
  *)
	echo "Usage: racsrvc {start|stop|status|restart|reload}"
	exit 1
esac

exit $RETVAL
