#!/bin/bash
#
#	/etc/rc.d/init.d/btnx
#
# Starts the mouse button rerouter daemon.
#
# chkconfig: 2345 49 49
# description: Mouse button rerouter daemon.
# processname: btnx

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

[ -f /usr/sbin/btnx ] || exit 0

prog=btnx

RETVAL=0

#
# See how we were called.
#

start() {
        echo -n $"Starting $prog: "
        
        # Check if evnet handler is existing.
        if ! ls /dev/input/event* > /dev/null ;then
            echo
            echo "No input event handlers found. Start blocked."
            false
        elif [ ! -f /etc/btnx/btnx_manager ]; then
            echo
            echo "No configuration file found. Start btnx-config first."
            false
        else
            /usr/sbin/btnx -b > /dev/null 2>&1
        fi
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            touch /var/lock/subsys/btnx
            success
        else
            failure
        fi
        echo
        return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	/usr/sbin/btnx -k > /dev/null 2>&1
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
            rm -f /var/lock/subsys/btnx
            success
        else
            failure
        fi
	echo
        return $RETVAL
}


restart() {
	stop
	start
}	

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

exit $RETVAL
