#!/bin/sh
#
# halevt         This script starts halevt deamon
#
# chkconfig: - 26 59
# description: This script starts and stops the halevt deamon
# processname: halevt
# pid /var/run/halevt/halevt.pid
# config: /etc/halevt/halevt.xml
#
### BEGIN INIT INFO
# Provides: halevt
# Required-Start: hal
# Required-Stop: hal
# Default-Stop: 0 1 6
# Default-Start: 2 3 4 5
# Short-Description: halevt daemon to handle HAL events
# Description: Halevt is a generic handler for HAL events. \
#              It can be used to automount, run arbitrary commands when \
#              events or conditions occur or properties are modified on your hardware \
#              (e.g., run a command when you close your laptop's lid, run a command when \
#              a particular device is attached or a particular CD is inserted, etc).
### END INIT INFO

processname=halevt
servicename=halevt
PIDFILE=/var/lib/halevt/halevt.pid

# Sanity checks.
[ -x /usr/bin/halevt ] || exit 0

RETVAL=0

[ -e /etc/default/halevt ] && . /etc/default/halevt

halevt_start() {
    if [ "$START_DAEMON" = "yes" ]; then

	echo -n "Starting halevt daemon: "
	start-stop-daemon --start --oknodo --pidfile $PIDFILE \
	    -c halevt -g plugdev --startas /usr/bin/halevt \
	    -- -p $PIDFILE
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
		echo "OK."
	else
		echo "Failed".
	fi
	return $RETVAL
    fi
}

halevt_stop() {
        echo -n "Stopping halevt daemon: "

	start-stop-daemon --stop --oknodo --pidfile $PIDFILE
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
		echo "OK."
	else
		echo "Failed".
	fi
	return $RETVAL
}

case "$1" in
    start)
	rm -f /var/lib/halevt/dev_tab
	halevt_start
	RETVAL=$?
	;;
    stop)
	halevt_stop
	RETVAL=$?
	;;
    restart|reload|force-reload)
	halevt_stop
	sleep 1
	halevt_start
	RETVAL=$?
	;;
    condrestart)
	if [ -f /var/lock/subsys/$servicename ]; then
	    halevt_stop
	    sleep 1
	    halevt_start
	    RETVAL=$?
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|restart|force-reload|reload|condrestart}"
	exit 1
	;;
esac

exit $RETVAL
