#!/bin/sh

############################
## WII REMOTE INIT SCRIPT ##
############################

## Callum Dickinson <gcfreak_ag20@hotmail.com>
## Sonic Team Junior <http://www.srb2.org>
## http://mb.srb2.org/showthread.php?t=31137
## svn://code.srb2.org/SRB2/branches/WII

## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.


## Any lines in this file preceeded by a hash ('#') indicates a comment, and is not read by the interpreter.

#--------------------#
. /lib/lsb/init-functions
. /etc/profile
LOGFILE="$LOGDIR/$ME.log"
#--------------------#

if [ "$1" = "start" ]
then
	case "$2" in
		1|2) echo -n;;
		*) print "Usage: /etc/init.d/wiimote {start|stop|restart|reload|force-reload} {1|2}" && exit 1;;
	esac
fi

case "$1" in
	"start") [ -n $2 ] && wiimote=$2 || wiimote=1
		log_action_begin_msg "Starting Wii Remote number $wiimote for launcher"
		killall wminput > /dev/null 2>&1
		wiigpio +sensorbar > /dev/null 2>&1
		wminput -p -r -n $wiimote -c /etc/cwiid/wminput/wminput.conf > /dev/null 2>&1 &
		while [ "$WAIT" != "yes" ]
		do
			ps | grep "wminput -p -r -n $wiimote" > /tmp/tmpfile1
			if [ -s /tmp/tmpfile1 ]
			then
				WAIT="yes"
			else
				if [ "$COUNT" ]
				then
					COUNT="`expr $COUNT + 1`"
				else
					COUNT="1"
				fi
				if [ "$COUNT" = "10" ]
				then
					log_failure_msg "Wii Remote connection interface failed to start properly"
				else
					sleep 1
				fi
			fi
		done
		log_action_end_msg 0
   		;;
	"stop") log_action_begin_msg "Stopping Wii Remote"
		wiigpio -sensorbar > /dev/null 2>&1
		killall wminput > /dev/null 2>&1
		log_action_end_msg 0
		;;
	restart|reload|force-reload) [ -n $2 ] && wiimote=$2 || wiimote=1
		log_action_begin_msg "Starting Wii Remote number $wiimote for game"
## This is for when wminput will work with more than one remote properly
#		wmpid="`ps | grep "wminput -p -r -n $wiimote" | awk '{ print $1 }'`"
#		kill $wmpid > /dev/null 2>&1
		killall wminput > /dev/null 2>&1
		wiigpio +sensorbar > /dev/null 2>&1
		wminput -p -r -n $wiimote -c $HOME/wminput.conf > /dev/null 2>&1 &
		while [ "$WAIT" != "yes" ]
		do
			ps | grep "wminput -p -r -n $wiimote" > /tmp/tmpfile1
			if [ -s /tmp/tmpfile1 ]
			then
				WAIT="yes"
			else
				if [ "$COUNT" ]
				then
					COUNT="`expr $COUNT + 1`"
				else
					COUNT="1"
				fi
				if [ "$COUNT" = "10" ]
				then
					log_failure_msg "Wii Remote connection interface failed to start properly"
				else
					sleep 1
				fi
			fi
		done
		log_action_end_msg 0
   		;;
	*) print "Usage: /etc/init.d/wiimote {start|stop|restart|reload|force-reload} {1|2}" && exit 1;;
esac
