#!/bin/bash
#
#	/etc/rc.d/init.d/wdaemon
#
# Starts the wdaemon
#
# chkconfig: - 95 5
# description: helps X.org to support Wacom tablets hotplugging by creating \
#    permanent uinput devices that may be feeded by real tablets events while \
#    the hardware is present.
# processname: wdaemon

# Source function library.
. /etc/init.d/functions

test -x /usr/bin/wdaemon || exit 0

RETVAL=0

#
#	See how we were called.
#

prog="wdaemon"

start() {
	# Check if there's already a configuration present
	if [ ! -f /etc/wdaemon.conf ]; then
	    echo "wdaemon configuration file not present yet";
	    return 0;
	fi
	# Check if wdaemon is already running
	if [ ! -f /var/lock/subsys/wdaemon ]; then
	    if [ -z "$(lsmod | grep uinput)" ]; then
	       echo -n $"Loading uinput module: "
	       modprobe uinput;
	       RETVAL=$?
	       [ $RETVAL -eq 0 ] && success || (failure; return $RETVAL);
	       echo;
	       for count in $(seq 1 3); do
	          if [ -e /dev/uinput ]; then
	             break;
	          fi
                  sleep 1;
               done
	       if [ ! -e /dev/uinput ]; then
	          logger -p daemon.err -t wdaemon "/dev/uinput not available";
	          failure;
	          return 1;
               fi
            fi
	    echo -n $"Starting $prog: "
	    daemon /usr/bin/wdaemon -c /etc/wdaemon.conf -f >/dev/null;
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && (success; touch /var/lock/subsys/wdaemon) || failure;
	    echo
	fi
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killproc /usr/bin/wdaemon
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/wdaemon
	echo
        return $RETVAL
}


restart() {
	stop
	start
}

reload() {
	restart
}

status_at() {
 	status /usr/bin/wdaemon
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
reload|restart)
	restart
	;;
condrestart)
	if [ -f /var/lock/subsys/wdaemon ]; then
	    restart
	fi
	;;
status)
	status_at
	;;
*)
	echo $"Usage: $0 {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $?
exit $RETVAL
