#!/bin/sh
# Author: Timo Hoenig <thoenig@nouse.net>
#
# /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
### END INIT INFO

# 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/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 $IALDAEMON_PIDDIR ]; then
                mkdir -p $IALDAEMON_PIDDIR;
        fi
        if [ -e $IALDAEMON_PID ]; then
                if [ -d /proc/`cat $IALDAEMON_PID` ]; then
                        echo "IAL already started. Not starting."
                        exit 1;
                else
                        echo "Removing stale PID file $IALDAEMON_PID.";
                        rm -f $IALDAEMON_PID;
                fi
        fi
	if [ ! -e $DBUSDAEMON_PID ]; then
		echo "D-BUS is not running. Please start D-BUS (or try 'rcial start-with-dbus').";
		exit 1;
	fi
        echo -n "Starting IAL daemon";
        startproc -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


