#!/bin/sh

###################
## CONFIG 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.

#--------------------#
. /etc/profile
sleepcount=3
LOGFILE="$LOGDIR/$ME.log"
conf="$HOME/$OSNAME.conf"
baseconf="`basename $conf`"
#--------------------#

# [ -f /dev/input/mice ] || export SDL_NOMOUSE=1 - set by the system

cp $HOME/base_config.cfg $HOME/.srb2/config.cfg

## If -c is set as a parameter, use the specified set config.
case "$1" in
	"-c") if [ -n "$2" ]; then
			print "Using set configuration $2."
			players="`cat $HOME/.srb2/conf$2/$baseconf | grep Multiplayer | sed -ne  's/.*Multiplayer=\([^ ]*\).*/\1/p'`"
			cont1="`cat $HOME/.srb2/conf$2/$baseconf | grep Control1 | sed -ne 's/.*Control1=\([^ ]*\).*/\1/p'`"
			cont2="`cat $HOME/.srb2/conf$2/$baseconf | grep Control2 | sed -ne 's/.*Control2=\([^ ]*\).*/\1/p'`"
		fi
		;;
	*) players="`cat $conf | grep Multiplayer | sed -ne  's/.*Multiplayer=\([^ ]*\).*/\1/p'`"
		cont1="`cat $conf | grep Control1 | sed -ne 's/.*Control1=\([^ ]*\).*/\1/p'`"
		cont2="`cat $conf | grep Control2 | sed -ne 's/.*Control2=\([^ ]*\).*/\1/p'`"
		;;
esac

#############################################
## Start configuration and initialisation. ##
#############################################

## Kill original wminput configuration and restart it with game configuration
/etc/init.d/wiimote restart 1
sleep $sleepcount > /dev/null 2>&1
while [ "$jsloop" != "no" ]
do
	if [ -n "$jscount" ]
	then
		jscount="`expr $jscount + 1`"
	else
		jscount="0"
	fi
	if ! [ -f /sys/class/input/js$jscount/device/name ]
	then
		jsloop="no"
	fi
done
## Configure first joystick
case "$cont1" in
	"Wiimote") count="0"
		while [ "$js" != "yes" ]; do
			jsdevice="`cat /sys/class/input/js$count/device/name 2> /dev/null`"
			if [ "$jsdevice" = "Nintendo Wiimote #1" ]; then
				print "Wii Remote set as player 1."
				echo "use_joystick `expr $count + 1`" >> $HOME/.srb2/config.cfg
				js="yes"
			else
				if [ "$count" = "$jscount" ] && [ "$js" != "yes" ]; then
					print "Wii Remote joystick device could not be found. Try increasing the timeout for the Wii Remote device node detection."
					sleep 5
					setexit
				fi
				if [ "$count" ]; then
					count="`expr $count + 1`"
				else
					count="1"
				fi
			fi
		done
		;;
	"GCNControllerCoop") count="0"
		while [ "$js" != "yes" ]; do
			jsdevice="`cat /sys/class/input/js$count/device/name 2> /dev/null`"
			if [ "$jsdevice" = "standard pad" ] || [ "$jsdevice" = "wireless pad" ]; then
				wire="`[ "$jsdevice" = "standard pad" ] && echo "Wired" || echo "WaveBird"`"
				if [ "`cat /tmp/gcnport1`" = "$wire" ]; then
					port="1"
				elif [ "`cat /tmp/gcnport2`" = "$wire" ]; then
					port="2"
				elif [ "`cat /tmp/gcnport3`" = "$wire" ]; then
					port="3"
				elif [ "`cat /tmp/gcnport4`" = "$wire" ]; then
					port="4"
				fi
				print "$wire GameCube Controller in port $port set as player 1, in co-op configuration."
				defjscount="$count"
				defport="$port"
				echo "use_joystick `expr $count + 1`" >> $HOME/.srb2/config.cfg
				echo 'joyaxis_firenormal "None"' >> $HOME/.srb2/config.cfg # Joystick Ring Toss Normal
				echo 'joyaxis_fire "None"' >> $HOME/.srb2/config.cfg # Joystick Ring Toss
				echo 'joyaxis_turn "LStick.X"' >> $HOME/.srb2/config.cfg # Look Left/Right
				echo 'joyaxis_side "RStick.X"' >> $HOME/.srb2/config.cfg # Strafe Left/Right
				echo 'setcontrol "fire" "JOYRIGHT"' >> $HOME/.srb2/config.cfg # Ring Toss
				echo 'setcontrol "firenormal" "KEY0"' >> $HOME/.srb2/config.cfg # Ring Toss Normal
				echo 'setcontrol "tossflag" "JOYLEFT"' >> $HOME/.srb2/config.cfg # Toss Flag
				echo 'setcontrol "use" "JOYA"' >> $HOME/.srb2/config.cfg # Spin
				echo 'setcontrol "pause" "JOYBB"' >> $HOME/.srb2/config.cfg # Pause
				echo 'setcontrol "jump" "JOYB"' >> $HOME/.srb2/config.cfg # Jump
				echo 'setcontrol "centerview" "JOYLEFT"' >> $HOME/.srb2/config.cfg # Center Camera
				echo 'setcontrol "weaponnext" "JOYDOWN"' >> $HOME/.srb2/config.cfg # Weapon Next
				echo 'setcontrol "weaponprev" "JOYUP"' >> $HOME/.srb2/config.cfg # Weapon Previous
				echo 'setcontrol "forward" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
				echo 'setcontrol "backward" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
				echo 'setcontrol "turnleft" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
				echo 'setcontrol "turnright" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
				js="yes"
			else
				if [ "$count" = "$jscount" ] && [ "$js" != "yes" ]; then
					print "GameCube Controller could not be found."
					sleep 5
					setexit
				fi
				if [ "$count" ]; then
					count="`expr $count + 1`"
				else
					count="1"
				fi
			fi
		done
		;;
	"GCNControllerMatch") count="0"
		while [ "$js" != "yes" ]; do
			jsdevice="`cat /sys/class/input/js$count/device/name 2> /dev/null`"
			if [ "$jsdevice" = "standard pad" ] || [ "$jsdevice" = "wireless pad" ]; then
				wire="`[ "$jsdevice" = "standard pad" ] && echo "Wired" || echo "WaveBird"`"
				if [ "`cat /tmp/gcnport1`" = "$wire" ]; then
					port="1"
				elif [ "`cat /tmp/gcnport2`" = "$wire" ]; then
					port="2"
				elif [ "`cat /tmp/gcnport3`" = "$wire" ]; then
					port="3"
				elif [ "`cat /tmp/gcnport4`" = "$wire" ]; then
					port="4"
				fi
				print "$wire GameCube Controller in port $port set as player 1, in match configuration."
				defjscount="$count"
				defport="$port"
				echo "use_joystick `expr $count + 1`" >> $HOME/.srb2/config.cfg
				echo 'joyaxis_firenormal "None"' >> $HOME/.srb2/config.cfg # Joystick Ring Toss Normal
				echo 'joyaxis_fire "None"' >> $HOME/.srb2/config.cfg # Joystick Ring Toss
				echo 'joyaxis_turn "RStick.X"' >> $HOME/.srb2/config.cfg # Look Left/Right
				echo 'joyaxis_side "LStick.X"' >> $HOME/.srb2/config.cfg # Strafe Left/Right
				echo 'setcontrol "fire" "JOYLEFT"' >> $HOME/.srb2/config.cfg # Ring Toss
				echo 'setcontrol "firenormal" "KEY0"' >> $HOME/.srb2/config.cfg # Ring Toss Normal (unused)
				echo 'setcontrol "tossflag" "JOYUP"' >> $HOME/.srb2/config.cfg # Toss Flag
				echo 'setcontrol "use" "JOYAA"' >> $HOME/.srb2/config.cfg # Spin
				echo 'setcontrol "pause" "JOYBB"' >> $HOME/.srb2/config.cfg # Pause
				echo 'setcontrol "jump" "JOYRIGHT"' >> $HOME/.srb2/config.cfg # Jump
				echo 'setcontrol "centerview" "KEY0"' >> $HOME/.srb2/config.cfg # Center Camera (unused)
				echo 'setcontrol "weaponnext" "JOYB"' >> $HOME/.srb2/config.cfg # Weapon Next
				echo 'setcontrol "weaponprev" "JOYA"' >> $HOME/.srb2/config.cfg # Weapon Previous
				echo 'setcontrol "forward" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
				echo 'setcontrol "backward" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
				echo 'setcontrol "turnleft" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
				echo 'setcontrol "turnright" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
				js="yes"
			else
				if [ "$count" = "$jscount" ] && [ "$js" != "yes" ]; then
					print "GameCube Controller could not be found."
					sleep 5
					setexit
				fi
				if [ "$count" ]; then
					count="`expr $count + 1`"
				else
					count="1"
				fi
			fi
		done
		;;
esac

js=""

## Configure second controller if multiplayer is set
case "$players" in
	Enabled) case "$cont2" in
			"Wiimote")
				if [ "$cont1" = "Wiimote" ]; then
					print "The Wii Remote is set to the first controller. Multiplayer with two Wii Remotes is not currently safely supported."
					print "Not adding multiplayer support to SRB2."
					echo "use_joystick2 0" >> $HOME/.srb2/config.cfg
					js="yes"
				else
					/etc/init.d/wiimote restart 2
					sleep $sleepcount > /dev/null 2>&1
					jsloop=""
					jscount="0"
					while [ "$jsloop" != "no" ]
					do
						if [ -n "$jscount" ]
						then
							jscount="`expr $jscount + 1`"
						else
							jscount="0"
						fi
						if ! [ -f /sys/class/input/js$jscount/device/name ]
						then
							jsloop="no"
						fi
					done
					count="0"
					while [ "$js" != "yes" ]; do
						jsdevice="`cat /sys/class/input/js$count/device/name 2> /dev/null`"
						if [ "$jsdevice" = "Nintendo Wiimote #2" ]
						then
							print "Wii Remote set as player 2."
							echo "use_joystick2 `expr $count + 1`" >> $HOME/.srb2/config.cfg
							js="yes"
						else
							if [ "$count" = "$jscount" ] && [ "$js" != "yes" ]
							then
								print "Wii Remote joystick device could not be found. Try increasing the timeout for the Wii Remote device node detection."
								print "Not adding multiplayer support to SRB2."
								echo "use_joystick2 0" >> $HOME/.srb2/config.cfg
								js="yes"
							fi
							if [ "$count" ]
							then
								count="`expr $count + 1`"
							else
								count="1"
							fi
						fi
					done
				fi
				;;
			"GCNControllerCoop") count="0"
				while [ "$js" != "yes" ]; do
					jsdevice="`cat /sys/class/input/js$count/device/name 2> /dev/null`"
					if [ "$jsdevice" = "standard pad" ] || [ "$jsdevice" = "wireless pad" ]; then
						if [ "$count" != "$defjscount" ]; then
							wire="`[ "$jsdevice" = "standard pad" ] && echo "Wired" || echo "WaveBird"`"
							if [ "`cat /tmp/gcnport1`" = "$wire" ] && [ "$defport" != "1" ]; then
								port="1"
							elif [ "`cat /tmp/gcnport2`" = "$wire" ] && [ "$defport" != "2" ]; then
								port="2"
							elif [ "`cat /tmp/gcnport3`" = "$wire" ] && [ "$defport" != "3" ]; then
								port="3"
							elif [ "`cat /tmp/gcnport4`" = "$wire" ] && [ "$defport" != "4" ]; then
								port="4"
							fi
							print "$wire GameCube Controller in port $port set as player 2, in match configuration."
							echo "use_joystick2 `expr $count + 1`" >> $HOME/.srb2/config.cfg
							echo 'joyaxis2_firenormal "None"' >> $HOME/.srb2/config.cfg # Joystick Ring Toss Normal
							echo 'joyaxis2_fire "None"' >> $HOME/.srb2/config.cfg # Joystick Ring Toss
							echo 'joyaxis2_turn "LStick.X"' >> $HOME/.srb2/config.cfg # Look Left/Right
							echo 'joyaxis2_side "RStick.X"' >> $HOME/.srb2/config.cfg # Strafe Left/Right
							echo 'setcontrol2 "fire" "SEC_JOYRIGHT"' >> $HOME/.srb2/config.cfg # Ring Toss
							echo 'setcontrol2 "firenormal" "KEY0"' >> $HOME/.srb2/config.cfg # Ring Toss Normal
							echo 'setcontrol2 "tossflag" "SEC_JOYLEFT"' >> $HOME/.srb2/config.cfg # Toss Flag
							echo 'setcontrol2 "use" "SEC_JOYA"' >> $HOME/.srb2/config.cfg # Spin
							echo 'setcontrol2 "pause" "SEC_JOYBB"' >> $HOME/.srb2/config.cfg # Pause
							echo 'setcontrol2 "jump" "SEC_JOYB"' >> $HOME/.srb2/config.cfg # Jump
							echo 'setcontrol2 "centerview" "SEC_JOYLEFT"' >> $HOME/.srb2/config.cfg # Center Camera
							echo 'setcontrol2 "weaponnext" "SEC_JOYDOWN"' >> $HOME/.srb2/config.cfg # Weapon Next
							echo 'setcontrol2 "weaponprev" "SEC_JOYUP"' >> $HOME/.srb2/config.cfg # Weapon Previous
							echo 'setcontrol2 "forward" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
							echo 'setcontrol2 "backward" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
							echo 'setcontrol2 "turnleft" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
							echo 'setcontrol2 "turnright" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
							js="yes"
						else
							if [ "$count" = "$jscount" ] && [ "$js" != "yes" ]; then
								print "GameCube Controller could not be found."
								print "Not adding multiplayer support to SRB2."
								echo "use_joystick2 0" >> $HOME/.srb2/config.cfg
								js="yes"
							fi
							if [ "$count" ]; then
								count="`expr $count + 1`"
							else
								count="1"
							fi
						fi
					else
						if [ "$count" = "$jscount" ] && [ "$js" != "yes" ]; then
							print "GameCube Controller could not be found."
							print "Not adding multiplayer support to SRB2."
							echo "use_joystick2 0" >> $HOME/.srb2/config.cfg
							js="yes"
						fi
						if [ "$count" ]; then
							count="`expr $count + 1`"
						else
							count="1"
						fi
					fi
				done
				;;
			"GCNControllerMatch") count="0"
				while [ "$js" != "yes" ]; do
					jsdevice="`cat /sys/class/input/js$count/device/name 2> /dev/null`"
					if [ "$jsdevice" = "standard pad" ] || [ "$jsdevice" = "wireless pad" ]; then
						if [ "$count" != "$defjscount" ]; then
							wire="`[ "$jsdevice" = "standard pad" ] && echo "Wired" || echo "WaveBird"`"
							if [ "`cat /tmp/gcnport1`" = "$wire" ] && [ "$defport" != "1" ]; then
								port="1"
							elif [ "`cat /tmp/gcnport2`" = "$wire" ] && [ "$defport" != "2" ]; then
								port="2"
							elif [ "`cat /tmp/gcnport3`" = "$wire" ] && [ "$defport" != "3" ]; then
								port="3"
							elif [ "`cat /tmp/gcnport4`" = "$wire" ] && [ "$defport" != "4" ]; then
								port="4"
							fi
							print "$wire GameCube Controller in port $port set as player 2, in match configuration."
							echo "use_joystick2 `expr $count + 1`" >> $HOME/.srb2/config.cfg
							echo 'joyaxis2_firenormal "None"' >> $HOME/.srb2/config.cfg # Joystick Ring Toss Normal
							echo 'joyaxis2_fire "None"' >> $HOME/.srb2/config.cfg # Joystick Ring Toss
							echo 'joyaxis2_turn "RStick.X"' >> $HOME/.srb2/config.cfg # Look Left/Right
							echo 'joyaxis2_side "LStick.X"' >> $HOME/.srb2/config.cfg # Strafe Left/Right
							echo 'setcontrol2 "fire" "SEC_JOYLEFT"' >> $HOME/.srb2/config.cfg # Ring Toss
							echo 'setcontrol2 "firenormal" "KEY0"' >> $HOME/.srb2/config.cfg # Ring Toss Normal (unused)
							echo 'setcontrol2 "tossflag" "SEC_JOYUP"' >> $HOME/.srb2/config.cfg # Toss Flag
							echo 'setcontrol2 "use" "SEC_JOYAA"' >> $HOME/.srb2/config.cfg # Spin
							echo 'setcontrol2 "pause" "SEC_JOYBB"' >> $HOME/.srb2/config.cfg # Pause
							echo 'setcontrol2 "jump" "SEC_JOYRIGHT"' >> $HOME/.srb2/config.cfg # Jump
							echo 'setcontrol2 "centerview" "KEY0"' >> $HOME/.srb2/config.cfg # Center Camera (unused)
							echo 'setcontrol2 "weaponnext" "SEC_JOYB"' >> $HOME/.srb2/config.cfg # Weapon Next
							echo 'setcontrol2 "weaponprev" "SEC_JOYA"' >> $HOME/.srb2/config.cfg # Weapon Previous
							echo 'setcontrol2 "forward" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
							echo 'setcontrol2 "backward" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
							echo 'setcontrol2 "turnleft" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
							echo 'setcontrol2 "turnright" "KEY0"' >> $HOME/.srb2/config.cfg # Unused
							js="yes"
						else
							if [ "$count" = "$jscount" ] && [ "$js" != "yes" ]; then
								print "GameCube Controller could not be found."
								print "Not adding multiplayer support to SRB2."
								echo "use_joystick2 0" >> $HOME/.srb2/config.cfg
							fi
							if [ "$count" ]; then
								count="`expr $count + 1`"
							else
								count="1"
							fi
						fi
					else
						if [ "$count" = "$jscount" ] && [ "$js" != "yes" ]; then
							print "GameCube Controller could not be found."
							print "Not adding multiplayer support to SRB2."
							echo "use_joystick2 0" >> $HOME/.srb2/config.cfg
							js="yes"
						fi
						if [ "$count" ]; then
							count="`expr $count + 1`"
						else
							count="1"
						fi
					fi
				done
				;;
		esac
		;;
	*) echo "use_joystick2 0" >> $HOME/.srb2/config.cfg;;
esac

## Start SRB2 with parameters
checkexit
BLNAME="SRB2"
case $1 in
	"-a") if [ -f /tmp/wadadd ]
		then
			echo "#!/bin/sh" > /tmp/srb2
			if [ -f /tmp/exeadd ]
			then
				BLNAME=`cat /tmp/exeadd | awk '{ print $2 }'`
				BNAME=`cat /tmp/exeadd | awk '{ print $1 }'`
				echo -n "$HOME/binaries/$BNAME/$BNAME -file " >> /tmp/srb2
			else
				echo -n "$HOME/srb2 -file " >> /tmp/srb2
			fi
			while read line
			do
				echo -n "$HOME/addons/$line " >> /tmp/srb2
			done < /tmp/wadadd
			echo " >> $LOGFILE 2>&1" >> /tmp/srb2
			print "Starting $BLNAME, this will take a while, please wait..."
			. /tmp/srb2
		else
			if [ -f /tmp/exeadd ]
			then
				BLNAME=`cat /tmp/exeadd | awk '{ print $2 }'`
				BNAME=`cat /tmp/exeadd | awk '{ print $1 }'`
				echo "#!/bin/sh" > /tmp/srb2
				echo "$HOME/binaries/$BNAME/$BNAME >> $LOGFILE 2>&1" >> /tmp/srb2
				print "Starting $BLNAME, this will take a while, please wait..."
				. /tmp/srb2
			else
				print "Starting $BLNAME, this will take a while, please wait..."
				$HOME/srb2 >> $LOGFILE 2>&1
			fi
		fi
		;;
	"-c") if [ -f $HOME/.srb2/conf$2/wadadd ]
		then
			echo "#!/bin/sh" > /tmp/srb2
			if [ -f $HOME/.srb2/conf$2/exeadd ]
			then
				BLNAME=`cat $HOME/.srb2/conf$2/exeadd | awk '{ print $2 }'`
				BNAME=`cat $HOME/.srb2/conf$2/exeadd | awk '{ print $1 }'`
				echo -n "$HOME/binaries/$BNAME/$BNAME -file " >> /tmp/srb2
			else
				echo -n "$HOME/srb2 -file " >> /tmp/srb2
			fi
			while read line
			do
				echo -n "$line " >> /tmp/srb2
			done < $HOME/.srb2/conf$2/wadadd
			echo " >> $LOGFILE 2>&1" >> /tmp/srb2
			print "Starting $BLNAME, this will take a while, please wait..."
			. /tmp/srb2
		else
			if [ -f $HOME/.srb2/conf$2/exeadd ]
			then
				BLNAME=`cat $HOME/.srb2/conf$2/exeadd | awk '{ print $2 }'`
				BNAME=`cat $HOME/.srb2/conf$2/exeadd | awk '{ print $1 }'`
				echo "#!/bin/sh" > /tmp/srb2
				echo "$HOME/binaries/$BNAME/$BNAME >> $LOGFILE 2>&1" >> /tmp/srb2
				print "Starting $BLNAME, this will take a while, please wait..."
				. /tmp/srb2
			else
				print "Starting $BLNAME, this will take a while, please wait..."
				$HOME/srb2 >> $LOGFILE 2>&1
			fi
		fi
		;;
	*) print "Starting $BLNAME, this will take a while, please wait..."
		$HOME/srb2 >> $LOGFILE 2>&1;;
esac
