#! /bin/sh
### BEGIN INIT INFO
# Provides:          wiimote
# Required-Start:    bluetooth
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: WiiMote startup script
# Description:       Starts two instances of wminput for two different wiimotes
### END INIT INFO

# Author: Sprite_tm

#Bluetooth addresses of the wiimotes. Find this out by using the lswm program.
#You can put these two lines in /etc/default/wminput too if you want.
WM_BDADDR1="00:1F:12:34:56:78"
WM_BDADDR2="00:1F:87:65:43:21"

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="WiiMote userspace driver"
NAME=wminput
DAEMON=/usr/bin/wminput
DAEMON_ARGS="--options args"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

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

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	$DAEMON -d -c /etc/cwiid/wminput/default $WM_BDADDR1 >/dev/null 2>/dev/null &
	#Try not to race between the two controllers
	sleep 1
	$DAEMON -d -c /etc/cwiid/wminput/default.2 $WM_BDADDR2 >/dev/null 2>/dev/null &
}

#
# Function that stops the daemon/service
#
do_stop()
{
	#Evil but works....
	killall wminput
}


case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  restart|force-reload)
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	do_start
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
	exit 3
	;;
esac

