#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          openhpid
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:
# Default-Stop:      0 1 2 3 4 5 6
# Short-Description: Start OpenHPI daemon at boot time
# Description:       Enable OpenHPI service which is provided by openhpid.
### END INIT INFO
#
# openhpid.sh    Start/Stop the openhpi daemon.
#
# chkconfig: - 90 10
# description: openhpid is standard UNIX program which uses the OpenHPI \
#              APIs and provides a standard internet server to access those \
#              APIs for client programs.
# processname: openhpid
# config: the standard openhpi conf file specified on the command line or the env.
# pidfile: /var/run/openhpid.pid
# 
# Author(s):
#	W. David Ashley <dashley@us.ibm.com>
#	Daniel de Araujo <ddearauj@us.ibm.com>

# Source function library.
PATH=/sbin:/bin:/usr/sbin:/usr/bin
prog="openhpid"

# Determine whether the lsb package is installed
# If it is, determine which lsb in installed:
# redhat, suse, or standard lsb

if test -f /etc/init.d/functions
then
   lsbtype="rh"
   . /etc/init.d/functions
elif test -f /etc/rc.status
then
   lsbtype="suse"
   . /etc/rc.status
elif test -f /lib/lsb/init-functions
then
   lsbtype="lsb"
   . /lib/lsb/init-functions
elif test -f /etc/gentoo-release
then
   lsbtype="gentoo"
   . /sbin/functions.sh
else
   lsbtype="nolsb"
fi

print_outcome()
{
   
	case "${lsbtype}" in
      
		rh)
   			echo
   			[ "$?" -eq 0 ]
   			;;

		suse)
 			rc_status -v
			;;

		lsb)
			if test "$?" -eq 0
   			then
      				log_success_msg "success"
   			else
				log_failure_msg "failed"
   			fi
  			;;
  		
  		gentoo)
  			eend $?
  			;;
   
		nolsb)
			if test "$?" -eq 0
			then
				echo " ... success"
			fi
			if test "$?" -ne 0
			then
				echo " ... failed"
			fi
			;;
   	esac
}

start() {	
	case "${lsbtype}" in
		
		rh) 
			echo -n $"Starting $prog: "
			daemon /usr/sbin/openhpid -c /etc/openhpi/openhpi.conf
			RETVAL=$?
			;;
		suse)
			echo -n $"Starting $prog: "
			startproc /usr/sbin/openhpid -c /etc/openhpi/openhpi.conf
			RETVAL=$?
			;;
		lsb)
			echo -n $"Starting $prog: "
			start_daemon /usr/sbin/openhpid -c /etc/openhpi/openhpi.conf
			RETVAL=$?
			;;
		gentoo)
			ebegin "Starting $prog: "
			start-stop-daemon --start --quiet --exec /usr/sbin/openhpid -- -c /etc/openhpi/openhpi.conf
			RETVAL=$?
			;;
		nolsb)
			echo -n $"Starting $prog: "
			/usr/sbin/openhpid -c /etc/openhpi/openhpi.conf
			RETVAL=$?
			;;
			
	esac
        
	print_outcome
        
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/openhpid

}

stop() {	
	case "${lsbtype}" in
		
		rh | lsb | suse)
			echo -n $"Stopping $prog: "
			killproc /usr/sbin/openhpid
			RETVAL=$?
			;;
		
		gentoo)
			ebegin "Stopping $prog: "
			start-stop-daemon --stop --quiet --exec /usr/sbin/openhpid
			RETVAL=$?
			;;
		
		nolsb)
			echo -n $"Stopping $prog: "
			if test -f /var/run/openhpid.pid && test "`cat /var/run/openhpid.pid`" != ""
         		then
            			kill "`cat /var/run/openhpid.pid`"
				RETVAL=$?  
			else
				RETVAL=0
			fi	          
			;;
		
	esac
	
	print_outcome
	
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/openhpid

}	

dstatus() {
	echo $"Checking for $prog daemon: "
	
	case "${lsbtype}" in
		
		rh) 
			status /usr/sbin/openhpid
			;;
		suse)
			checkproc /usr/sbin/openhpid
			rc_status -v
			;;
		lsb)
			pid="`pidofproc /usr/sbin/openhpid`"
         		if test "${pid}" != ""
			then
				log_success_msg "$prog is running"
			else
				log_success_msg "$prog is not running"	
			fi	
			;;
		gentoo | nolsb)
			if test -f /var/run/openhpid.pid && 
				test "`cat /var/run/openhpid.pid`" != "" && 
				kill -0 "`cat /var/run/openhpid.pid`"
			then
				echo "$prog is running"
			else
				echo "$prog is not running"
			fi		
			
			;;
			
	esac	
	


}	

restart() {
  	stop
	start
}	

condrestart() {
	[ -f /var/lock/subsys/openhpid ] && restart || :
}

force_reload() {
	restart
}

# See how we were called.

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  condrestart|try-restart)
  	condrestart
	;;
  status)
  	dstatus
	;;
  force-reload)
  	force_reload
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|condrestart|try-restart|status|force-reload}"
	exit 1
esac
