#! /bin/sh
### BEGIN INIT INFO
# Provides:          btnx
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Mouse button rerouter daemon.
# Description:       Captures events from the mouse and
#                    reroutes them through uinput as other user-defined events.
### END INIT INFO

## BEGIN CHKCONFIG
# chkconfig: 2345 49 49
# description: Mouse button rerouter daemon.
## END CHKCONFIG

# Author: Olli Salonen <oasalonen@gmail.com>

# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script

# Note that some variables are replaced by Makefile (see sed command, data/Makefile.am).
# If variables are not filled in, then this is a template file.
PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
DESC="Button Extension - mouse button rerouter daemon"
# Name of this program:
NAME=<PACKAGE>
# Path of the executable:
DAEMON=<EXEC_PATH>
# Name of this script (full path):
SCRIPTNAME=<SCRIPT_NAME>
FAIL_STATUS=6
# Set to -l if btnx output is directed to syslog
ARG_SYSLOG=<SYSLOG>
ARG_CONFIG=

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 5

# Check arguments (configuration file name)
if [ -z $2 ]; then # empty string
	ARG_CONFIG=""
else
	ARG_CONFIG="-c \"$2\""
fi

#
# Function that checks event handlers. Prevents
# udev loading hang.
#
check_handlers()
{
	ls /dev/event* > /dev/null 2> /dev/null
	[ $? = 0 ] && return 0
	ls /dev/input/event* > /dev/null 2> /dev/null
	[ $? = 0 ] && return 0
	ls /dev/misc/event* > /dev/null 2> /dev/null
	[ $? = 0 ] && return 0

	echo "No input event handlers found. Start blocked."

	return 1
}

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	
	$DAEMON -b $ARG_SYSLOG $ARG_CONFIG
	return $?
}

#
# Function that stops the daemon/service
#
do_stop()
{
	$DAEMON -k
	return $?
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	
	return 0
}

case "$1" in
  start)
	echo "Starting $NAME :" "$DESC" >&2
	check_handlers
	[ $? -ne 0 ] && exit 1
	do_start
	RET=$?
	case "$RET" in
		0) 
			echo "btnx successfully started" 
			exit 0
			;;
		*) 
			echo "btnx failed to start (error code $RET)"
			exit $RET
			;;
	esac
	;;
  stop)
	echo "Stopping $NAME :" "$DESC" >&2
	do_stop
	case "$?" in
		0|1) 
			echo "btnx successfully stopped" 
			exit 0
			;;
		2) 
			echo "btnx failed to stop" 
			exit $FAIL_STATUS
			;;
	esac
	;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	echo "Restarting $NAME :" "$DESC" >&2
	do_stop
	case "$?" in
	  0|1)
		echo "btnx successfully stopped"
		do_start
		RET=$?
		case "$RET" in
			0) 
				echo "btnx successfully started"
				exit 0 ;;
			*) 
				echo "btnx failed to start during restart" 
				exit $RET ;;
		esac
		;;
	  *)
	  	# Failed to stop
		echo "btnx failed to stop during restart"
		exit $FAIL_STATUS ;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

:
