#!/bin/sh
# Author: Danny Kukawka <dkukawka@suse.de>
#
# /etc/init.d/rchal
#
### BEGIN INIT INFO
# Provides:          hal
# Required-Start:    boot.localnet dbus cron
# Should-Start:      resmgr
# Required-Stop:     
# Should-Stop:
# Default-Start:     3 5
# Default-Stop:      
# Short-Description: HAL is a daemon for managning information about the hardware on the system
# Description:       HAL is a hardware abstraction layer and aims to provide a live list of devices present 
#		     in the system at any point in time. HAL tries to understand both physical devices (such 
#	   	     as PCI, USB) and the device classes (such as input, net and block) physical devices have, 
#		     and it allows merging of information from so called device info files specific to a device. 
#		     HAL provides a network API through D-BUS for querying devices and notifying when things 
#		     change. Finally, HAL provides some monitoring (in an unintrusive way) of devices, presently 
#		     ethernet link detection and volume mounts are monitored. This, and more, is all described 
#		     in the HAL specification
#                    
### END INIT INFO

# Check for binary
HALDAEMON_BIN=/usr/sbin/hald
test -x $HALDAEMON_BIN || exit 5

# Parameters (startup)
HALDAEMON_PARA="--daemon=yes";
HALDAEMON_PIDDIR="/var/run/hal";
HALDAEMON_PID=$HALDAEMON_PIDDIR/haldaemon.pid;
DBUSDAEMON_PIDDIR="/var/run/dbus";
DBUSDAEMON_PID=$DBUSDAEMON_PIDDIR/pid;

# Source LSB init functions
# providing start_daemon, killproc, pidofproc,
# log_success_msg, log_failure_msg and log_warning_msg.
# This is currently not used by UnitedLinux based distributions and
# not needed for init scripts for UnitedLinux only. If it is used,
# the functions from rc.status should not be sourced or used.
#. /lib/lsb/init-functions

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
	
	if [ ! -d $HALDAEMON_PIDDIR ]; then
                mkdir -p $HALDAEMON_PIDDIR;
                chown haldaemon:haldaemon $HALDAEMON_PIDDIR;
        fi
        if [ -e $HALDAEMON_PID ]; then
                if [ -d /proc/`cat $HALDAEMON_PID` ]; then
			echo "HAL already started. Not starting."
			exit 1;
                else
                        echo "Removing stale PID file $HALDAEMON_PID.";
                        rm -f $HALDAEMON_PID;
                fi
        fi
#	if [ ! -e $DBUSDAEMON_PID ]; then
#		echo "DBUS is not running. Please start DBUS (or try 'rchal start-with-dbus').";
#		exit 1;
#	fi
        
	echo -n "Starting HAL daemon";
        startproc -p $HALDAEMON_PID $HALDAEMON_BIN $HALDAEMON_PARA
        rc_status -v
        ;;
    start-with-dbus)
	if [ ! -e $DBUSDAEMON_PID ]; then
               echo -n "DBUS is not running. Starting D-BUS daemon";
                rcdbus start;
        fi
	$0 start
	;;
    stop)
	echo -n "Shutting down HAL daemon"
       	killproc -p $HALDAEMON_PID -TERM $HALDAEMON_BIN
       	rm -f $HALDAEMON_PID;
       	rc_status -v
        ;;
    try-restart)
        $0 status >/dev/null &&  $0 restart
        rc_status
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    force-reload)
        echo -n "Reload service HAL daemon"
        $0 stop  &&  $0 start
        rc_status
        ;;
    reload)
        rc_failed 3
        rc_status -v
        ;;
    status)
        echo -n "Checking for service HAL daemon"
        checkproc $HALDAEMON_BIN
        rc_status -v
        ;;
    probe)
        ## Optional: Probe for the necessity of a reload, print out the
        ## argument to this init script which is required for a reload.
        ## Note: probe is not (yet) part of LSB (as of 1.2)
        # test /etc/FOO/FOO.conf -nt /var/run/FOO.pid && echo reload
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|start-with-dbus|reload|probe}"
        exit 1
        ;;
esac
rc_exit


