#!/bin/sh
# Author: Timo Hoenig <thoenig@suse.de>
#
# /etc/init.d/ial
#
### BEGIN INIT INFO
# Provides:          ial 
# Required-Start:    dbus
# Should-Start:      powersaved
# X-UnitedLinux-Should-Start:
# Required-Stop:     
# X-UnitedLinux-Should-Stop:
# Default-Start:              2 3 5
# Default-Stop:               0 1 6
# Short-Description: Input Abstraction Layer (IAL) makes special keys "just work".
# Description:       Input Abstraction Layer (IAL) makes special keys (e.g. multimedia keys,
#                    function keys found on notebooks) "just work". IAL provides a daemon
#                    which can be enhanced by modules. These modules are sending events via
#                    D-Bus to the clients.
### END INIT INFO


# Short-Description: Listen and dispatch ACPI events from the kernel
# Description:       acpid reads events from /proc/acpi/event and dispatches
#       them (by SuSE default) to /usr/sbin/acpid_proxy. This script collects
#       additional information if necessary and executes required actions (e.g.
#       shutting down when the power button was pressed.) You may select these
#       actions in /etc/sysconfig/powermanagement.
#       Additionally this start script loads all needed modules.


# Check for binary
IALDAEMON_BIN=/usr/sbin/iald
test -x $IALDAEMON_BIN || exit 5

# Parameters (startup)
IALDAEMON_PARA=""
IALDAEMON_PIDDIR="/var/run/ial"
IALDAEMON_PID=$IALDAEMON_PIDDIR/iald.pid
DBUSDAEMON_PIDDIR="/var/run/dbus"
DBUSDAEMON_PID=$DBUSDAEMON_PIDDIR/pid

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
        if [ ! -d $IALDAEMON_PIDDIR ]; then
                mkdir -p $IALDAEMON_PIDDIR
        fi
        if [ -e $IALDAEMON_PID ]; then
		if checkproc -p $IALDAEMON_PID $IALDAEMON_BIN &> /dev/null ; then
                        echo -n "IAL already started. Not starting."
        		rc_status -v
			rc_exit
                else
                        echo "Removing stale PID file $IALDAEMON_PID."
                        rm -f $IALDAEMON_PID
                fi
        fi
	if [ ! -e $DBUSDAEMON_PID ]; then
		echo -n "D-BUS is not running. Please start D-BUS (or try 'rcial start-with-dbus')."
		rc_failed 1
        	rc_status -v
		rc_exit
	fi
        echo -n "Starting IAL daemon"
        startproc -q -s -p $IALDAEMON_PID $IALDAEMON_BIN $IALDAEMON_PARA
        rc_status -v
        ;;
    start-with-dbus)
	if [ ! -e $DBUSDAEMON_PID ]; then
		echo -n "D-BUS is not running. Starting D-BUS daemon"
		rcdbus start
        fi
	$0 start
	;;
    stop)
        echo -n "Shutting down IAL daemon"
        killproc -p $IALDAEMON_PID -TERM $IALDAEMON_BIN
        rm -f $IALDAEMON_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 IAL daemon"
        $0 stop  &&  $0 start
        rc_status
        ;;
    reload)
        rc_failed 3
        rc_status -v
        ;;
    status)
        echo -n "Checking for service IAL daemon"
        checkproc $IALDAEMON_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
