#!/bin/sh

##############
## LAUNCHER ##
##############

## 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 shell parser.

#--------------------#
. /etc/profile
conf="$HOME/$OSNAME.conf"
case "$LOADER" in
	"ios") DESC="Return to the Homebrew Channel.";;
	"mini") DESC="Restart the Wii.";;
esac
backtitle="$MINIFULLOS"
backtune="/usr/share/launcher/backtune.mp3"
LOGFILE="$LOGDIR/$ME.log"
OLDLOGCOMPAT="yes"
#--------------------#

## Set the setconfig number to load.
setconfig ()
{
	setconfig="$1"
}

## Start SRB2.
start ()
{
	loadfile /etc/init.d/srb2 $*
}

## Simple command to start and kill the background tune.
music ()
{
	case $1 in
		"start") madplay -Q --repeat $backtune > /dev/null 2>&1 &;;
		"stop") killall madplay > /dev/null 2>&1;;
	esac
}

## Turns on and off the multiplayer mode in a custom setup.
# -c <number>: changes the multiplayer setup for a specific set config.
multoggle ()
{
	case $1 in
		"-c") players=`cat $HOME/.srb2/conf$2/$OSNAME.conf | grep Multiplayer | sed -ne  's/.*Multiplayer=\([^ ]*\).*/\1/p'`
			if [ $players = "Enabled" ]
			then
				sed "/Multiplayer/d" $HOME/.srb2/conf$2/$OSNAME.conf > $HOME/.srb2/conf$2/$OSNAME.conf.2
				echo "Multiplayer=Disabled" >> $HOME/.srb2/conf$2/$OSNAME.conf.2
				rm -rf $HOME/.srb2/conf$2/$OSNAME.conf
				mv $HOME/.srb2/conf$2/$OSNAME.conf.2 $HOME/.srb2/conf$2/$OSNAME.conf
			else
				sed "/Multiplayer/d" $HOME/.srb2/conf$2/$OSNAME.conf > $HOME/.srb2/conf$2/$OSNAME.conf.2
				echo "Multiplayer=Enabled" >> $HOME/.srb2/conf$2/$OSNAME.conf.2
				rm -rf $HOME/.srb2/conf$2/$OSNAME.conf
				mv $HOME/.srb2/conf$2/$OSNAME.conf.2 $HOME/.srb2/conf$2/$OSNAME.conf
			fi
			;;
		*) players=`cat $conf | grep Multiplayer | sed -ne  's/.*Multiplayer=\([^ ]*\).*/\1/p'`
			if [ $players = "Enabled" ]
			then
				sed "/Multiplayer/d" $conf > $conf.2
				echo "Multiplayer=Disabled" >> $conf.2
				rm -rf $conf
				mv $conf.2 $conf
			else
				sed "/Multiplayer/d" $conf > $conf.2
				echo "Multiplayer=Enabled" >> $conf.2
				rm -rf $conf
				mv $conf.2 $conf
			fi
			;;
	esac
}

## Changes the controller setup for a specific controller for an SRB2 custom setup.
# -c <number>: changes it for a specific set config.
contfig ()
{
	case $1 in
		1) case $2 in
			"-c") sconf="$HOME/.srb2/conf$3/`basename $conf`"
				sed "/Control1/d" $sconf > $sconf.2
				echo "Control1=$4" >> $sconf.2
				mv $sconf.2 $sconf
				;;
			*) sed "/Control1/d" $conf > $conf.2
				echo "Control1=$2" >> $conf.2
				mv $conf.2 $conf
				;;
		esac
			;;
		2) case $2 in
			"-c") sconf="$HOME/.srb2/conf$3/`basename $conf`"
				sed "/Control2/d" $sconf > $sconf.2
				echo "Control2=$4" >> $sconf.2
				mv $sconf.2 $sconf
				;;
			*) sed "/Control2/d" $conf > $conf.2
				echo "Control2=$2" >> $conf.2
				mv $conf.2 $conf
				;;
		esac
			;;
	esac
}	

## Turns on and off the network functionality in the system.
nettoggle ()
{
	if [ $NETWORKING = "Enabled" ]; then
		dialog --backtitle "$backtitle" --infobox "Stopping the network interface..." 3 37
		/etc/init.d/networking stop > /dev/null 2>&1
		sed "/Networking/d" $SYSCONF > $SYSCONF.2
		echo "Networking=Disabled" >> $SYSCONF.2
		rm -rf $SYSCONF
		mv $SYSCONF.2 $SYSCONF
	else
		dialog --backtitle "$backtitle" --infobox "Starting the network interface..." 3 37
		sed "/Networking/d" $SYSCONF > $SYSCONF.2
		echo "Networking=Enabled" >> $SYSCONF.2
		rm -rf $SYSCONF
		mv $SYSCONF.2 $SYSCONF
		/etc/init.d/networking start > /dev/null 2>&1
		status=$?
		if [ "$status" != "0" ]; then
			dialog --backtitle "$backtitle" --infobox "The network interface had a problem starting up. Check if the connection is working and if not, check your network configuration." 6 44
			sleep 5
		fi
		if [ -f /tmp/networking_fail ]; then
			case "`cat /tmp/networking_fail`" in
				dhcp_nolease) dialog --backtitle "$backtitle" --infobox "The system could not get a lease from the DHCP server, or the DHCP server could not be found. Check if the pool of the available IP addresses isn't empty, or that the DHCP server is online, and try restarting the network connection." 7 52
						sleep 5
						;;
			esac
		fi
	fi
}

## Turns on and off the background tune for the launcher.
tunetoggle ()
{
	if [ $TUNES = "Enabled" ]; then
		sed "/Backtune/d" $conf > $conf.2
		echo "Backtune=Disabled" >> $conf.2
		rm -rf $conf
		mv $conf.2 $conf
	else
		sed "/Backtune/d" $conf > $conf.2
		echo "Backtune=Enabled" >> $conf.2
		rm -rf $conf
		mv $conf.2 $conf
	fi
}	

## Actually deletes the launcher configuration files (used with the below command).
purge ()
{
	clear
	echo
	echo
	print "Deleting SRB2Wii information..."
	rm -rf $HOME/.srb2
	print "Restarting SRB2Wii launcher..."
	mkdir $HOME/.srb2
	cp $HOME/base_config.cfg $HOME/.srb2/config.cfg
	loadproc menu main
}

## Asks whether to delete all the launcher configuration files.
reset ()
{
	dialog --backtitle "$backtitle" \
	--yesno "Are you sure you want to DELETE ALL THE CONFIGURATION for SRB2Wii, including SRB2 save files, SRB2 settings and SRB2 parameters/options?" \
	7 55
	sel=$?
	case $sel in
		"0") purge;;
	esac
}

## Network configuration, stage 1. Configures the connection type used for mini users.
netconf1 ()
{
	if [ -d $SDCARD/linux ]
	then
		echo -n ""
	else
		mkdir $SDCARD/linux
	fi
	echo " " >> /tmp/wireless
	dialog --backtitle "$backtitle" \
	--title "Network Configuration" \
	--menu "Press 2 to select and use the directional pad to move." 11 75 4 \
	"Wireless (WPA)" "Use a Wireless connection with WPA/WPA2." \
	"Wireless (WEP-ASCII)" "Use a Wireless connection with WEP." \
	"Wireless (WEP-Binary)" "Use a Wireless connection with WEP." \
	"Wired" "Use a Wired connection." 2> /tmp/set
	menuitem=`cat /tmp/set`
	case $menuitem in
		"Wireless (WPA)") netset wpa
			menu network -s
			;;
		"Wireless (WEP-ASCII)") netset wepa
			menu network -s
			;;
		"Wireless (WEP-Binary)") netset wepb
			menu network -s
			;;
		"Wired") netset eth
			menu network -s
			;;
	esac
}

## Actually configures the network type once chosen, used with the above command.
# netset <wpa|wepa|wepb|eth>: sets the network type passed
netset ()
{
	case $1 in
		"wpa") cat $conf | sed "/Encryption/d" | sed "/NetInterface/d" > $SYSCONF.2
			echo "Encryption=WPA" >> $SYSCONF.2
			echo "NetInterface=wlan0" >> $SYSCONF.2
			rm -rf $SYSCONF
			mv $SYSCONF.2 $SYSCONF
			;;
		"wepa") cat $SYSCONF | sed "/Encryption/d" | sed "/NetInterface/d" > $SYSCONF.2
			echo "Encryption=WEP-ASCII" >> $SYSCONF.2
			echo "NetInterface=wlan0" >> $SYSCONF.2
			rm -rf $SYSCONF
			mv $SYSCONF.2 $SYSCONF
			;;
		"wepb") cat $conf | sed "/Encryption/d" | sed "/NetInterface/d" > $conf.2
			echo "Encryption=WEP-Binary" >> $SYSCONF.2
			echo "NetInterface=wlan0" >> $SYSCONF.2
			rm -rf $SYSCONF
			mv $SYSCONF.2 $SYSCONF
			;;
		"eth") 	cat $conf | sed "/Encryption/d" | sed "/NetInterface/d" > $SYSCONF.2
			echo "Encryption=Ethernet" >> $SYSCONF.2
			echo "NetInterface=eth0" >> $SYSCONF.2
			rm -rf $SYSCONF
			mv $SYSCONF.2 $SYSCONF
			;;
	esac
}

## Network configuration, stage 2. Configures wireless network details for mini users.
netconf2 ()
{
	enc=`cat $SYSCONF | grep Encryption | sed -ne 's/.*Encryption=\([^ ]*\).*/\1/p'`
	case $enc in
	"WPA") dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your wireless network's SSID." 8 60 2> /tmp/set
		set=`cat /tmp/set`
		echo "wpassid $set" > /tmp/wificonfig
		dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your WPA/WPA2 passcode." 8 60 2> /tmp/set
		set=`cat /tmp/set`
		echo "wpa-psk $set" >> /tmp/wificonfig
		echo "wpa-key-mgmt WPA-PSK" >> /tmp/wificonfig
		echo "wpa-pairwise TKIP CCMP" >> /tmp/wificonfig
		echo "wpa-group TKIP CCMP" >> /tmp/wificonfig
		echo "wpa-proto WPA RSN" >> /tmp/wificonfig
		echo "wpa-ap-scan 1" >> /tmp/wificonfig
		echo "wpa-scan-ssid 1" >> /tmp/wificonfig
		;;
	"WEP-ASCII") dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your wireless network's SSID." 8 60 2> /tmp/set
		set=`cat /tmp/set`
		echo "wireless-essid $set" > /tmp/wificonfig
		dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your WEP ASCII passcode." 8 60 2> /tmp/set
		set=`cat /tmp/set`
		echo "wireless-key $set" >> /tmp/wificonfig
		;;
	"WEP-Binary") dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your wireless network's SSID." 8 60 2> /tmp/set
		set=`cat /tmp/set`
		echo "wireless-essid $set" > /tmp/wificonfig
		dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your WEP binary passcode (format: 0123-4567-89)." 8 60 2> /tmp/set
		set=`cat /tmp/set`
		echo "wireless-key s:$set" >> /tmp/wificonfig
		;;
	*) dialog --backtitle "$backtitle" --infobox "You do not use the Wi-Fi features, so this is disabled." 3 54
		sleep 3
		;;
	esac
}

## Network configuration, stage 3. Configures network details, such as whether to use DHCP or a static IP address and the information required.
netconf3 ()
{
	if [ -d $SDCARD/linux ]
	then
		echo -n ""
	else
		mkdir $SDCARD/linux
	fi
	echo "auto lo" > /tmp/interfaces
	echo "iface lo inet loopback" >> /tmp/interfaces
	echo "" >> /tmp/interfaces
	dialog --backtitle "$backtitle" \
	--title "Network Configuration" \
	--menu "Press 2 to select and use the directional pad to move." 9 75 2 \
	"Use DHCP" "Use DHCP to automatically configure the network." \
	"Manual" "Manually set network information." 2> /tmp/set
	card=`cat $SYSCONF | grep NetInterface | sed -ne 's/.*NetInterface=\([^ ]*\).*/\1/p'`
	menuitem=`cat /tmp/set`
	case $menuitem in
		"Use DHCP") echo "auto $card" >> /tmp/interfaces && echo "iface $card inet dhcp" >> /tmp/interfaces && menu network -s;;
		"Manual") echo "auto $card" >> /tmp/interfaces && echo "iface $card inet static" >> /tmp/interfaces && statconfig && menu network -s;;
	esac
}

## Part of network configuration, stage 3. The wizard that it walks you through to get the Internet information and writes it to a temporary file on disk.
statconfig ()
{
	dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your Wii's IP address." 8 60 2> /tmp/set
	set=`cat /tmp/set`
	echo "address $set" >> /tmp/interfaces
	dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your subnet mask." 8 60 2> /tmp/set
	set=`cat /tmp/set`
	echo "netmask $set" >> /tmp/interfaces
	dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your internet gateway (router) IP address." 8 60 2> /tmp/set
	set=`cat /tmp/set`
	echo "gateway $set" >> /tmp/interfaces
	dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your network IP range bottom end (for example 10.1.1.0)." 8 70 2> /tmp/set
	set=`cat /tmp/set`
	echo "network $set" >> /tmp/interfaces
	dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your network IP range top end (for example 10.1.1.255)." 8 70 2> /tmp/set
	set=`cat /tmp/set`
	echo "broadcast $set" >> /tmp/interfaces
	dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your primary DNS server." 8 60 2> /tmp/set
	set=`cat /tmp/set`
	echo "nameserver $set" >> /tmp/resolv.conf
	dialog --backtitle "$backtitle" --title "Network Configuration" --inputbox "Enter your secondary DNS server." 8 60 2> /tmp/set
	set=`cat /tmp/set`
	echo "           $set" >> /tmp/resolv.conf
}

## Saves the network information to the SD card.
netsave ()
{
	enc=`cat $SYSCONF | grep Encryption | sed -ne 's/.*Encryption=\([^ ]*\).*/\1/p'`
	case $enc in
		"Ethernet") echo -n;;
		*) case $enc in
			"WPA") cat /tmp/wificonfig >> /tmp/interfaces;;
			"WEP-ASCII") cat /tmp/wificonfig >> /tmp/interfaces;;
			"WEP-Binary") cat /tmp/wificonfig >> /tmp/interfaces;;
			*) sed "/Encryption/d" $conf > $conf.2
				echo "Encryption=Ethernet" >> $conf.2
				rm -rf $conf
				mv $conf.2 $conf
				;;
			esac
			;;
	esac
	rm -rf /tmp/wificonfig
	mv /tmp/interfaces $SDCARD/linux/interfaces > /dev/null 2>&1
	mv /tmp/resolv.conf $SDCARD/linux/resolv.conf > /dev/null 2>&1
}

## Command to search for WADs on a USB stick.
searchwads ()
{
	clear
	echo
	echo
	[ -d $USBMOUNT ] || mkdir -p $USBMOUNT
	print -n "Mounting the USB disk..."
	umount $USBMOUNT > /dev/null 2>&1
	mount -n -t auto /dev/sda1 $USBMOUNT
	status=$?
	if [ "$status" = "0" ]; then
		echo "done."
		echo -n "Searching for WADs..."
		find $USBMOUNT -print | grep .wad > /tmp/wadlist
		echo "done."
		echo -n "Adding WADs to the system..."
		if [ -s /tmp/wadlist ]
		then
			while read line
			do
	   			cp "$line" $HOME/addons
			done < /tmp/wadlist
			echo "done."
			echo "Adding WADs finished. Returning you to the $OSLNAME launcher..."
			sleep 2
		else
			echo "failed."
			echo "No WADs found on the USB stick. Returning you to the $OSLNAME launcher..."
			sleep 2
		fi
	else
		echo "failed."	
		echo "Returning you to the $OSLNAME launcher..."
		sleep 2
	fi
}

# Command to search for EXEs on a USB stick.
searchexes ()
{
	clear
	echo
	echo
	[ -d $USBMOUNT ] || mkdir -p $USBMOUNT
	print -n "Mounting the USB disk..."
	umount $USBMOUNT > /dev/null 2>&1
	mount -n -t auto /dev/sda1 $USBMOUNT
	status=$?
	if [ "$status" = "0" ]; then
		print "done."
		print -n "Searching for binaries..."
		find $USBMOUNT -print | grep "$OSNAME-binary" > /tmp/exelist
		print "done."
		print -n "Adding binaries to the system..."
		if [ -s /tmp/exelist ]
		then
			while read line
			do
	   			cp $line $HOME/binaries/tmp.tar.bz2
				cd $HOME/binaries
				tar -jxf tmp.tar.bz2
				rm -rf tmp.tar.bz2
				mv `basename $line | sed "s/.tar.bz2//"` `basename $line | sed -e "s/.tar.bz2//" -e "s/$OSNAME-binary-//"`
			done < /tmp/exelist
			print "done."
			print "Adding binaries finished. Returning you to the $OSLNAME launcher..."
			sleep 3
		else
			print "failed."
			print "No binaries found on the USB stick. Returning you to the $OSLNAME launcher..."
			sleep 3
		fi
	else
		print "failed."
		print "Returning you to the $OSLNAME launcher..."
		sleep 3
	fi
}

###########################################################################################################
## The 'menu' command, which actually displays most of what you actually see, the launcher menus itself. ##
## Some operations are also performed here, but most of the time the launcher calls the above commands.  ##
## Take care when modifying this, as any mistakes could lead to unpredictable results!                   ##
###########################################################################################################

menu ()
{
	func="$2"
	## Main menu.
	case $1 in
		"main") dialog --backtitle "$backtitle" \
			--title "Main Menu" \
			--menu "Press 2 to select and use the directional pad to move." 12 75 5 \
			"Start SRB2" "Starts vanilla SRB2 with the defaults." \
			"Configure SRB2" "Configure SRB2 options." \
			"SRB2Wii Settings" "Configure the SRB2Wii distribution settings." \
			"Restart" "$DESC" \
			"Power Off" "Turn off the Wii." 2> /tmp/set
			menuitem=`cat /tmp/set`
			case $menuitem in
				"Start SRB2") music stop
					clear
					echo
					echo
					start
					loadfile /etc/init.d/wiimote start 1
					[ "$TUNES" = "Enabled" ] && music start
					loadfunc menu main
					;;
				"Configure SRB2") loadproc menu srb2config
						loadproc menu main
						;;
				"SRB2Wii Settings") loadproc menu settings
						loadproc menu main
						;;
				"Restart") music stop
					clear
					echo
					echo
					reboot
					;;
				"Power Off") music stop
					clear
					echo
					echo
					poweroff;;
			esac
			;;
		## SRB2 Configuration menu.
		"srb2config") players=`cat $conf | grep Multiplayer | sed -ne  's/.*Multiplayer=\([^ ]*\).*/\1/p'`
			dialog --backtitle "$backtitle" \
			--title "Configure SRB2" \
			--menu "Press 2 to select and use the directional pad to move." 15 62 7 \
			"Start SRB2" "Starts SRB2 with the set options." \
			"WAD Configuration" "Configure WADs for SRB2 to load." \
			"EXE Configuration" "Configure an SRB2 binary to load." \
			"Multiplayer" "Toggle multiplayer support ($players)." \
			"Controls" "Set up controllers to use with SRB2." \
			"Saved Configs" "Create, load and delete set configs." \
			"Back" "Return to the Main Menu." 2> /tmp/set
			menuitem=`cat /tmp/set`
			case $menuitem in
				"Start SRB2") music stop
					clear
					echo
					echo
					if [ "$setconfig" ]; then
						start -c $setconfig
						/etc/init.d/wiimote start 1
						[ "$TUNES" = "Enabled" ] && music start
						menu main
					else
						start -a
						/etc/init.d/wiimote start 1
						[ "$TUNES" = "Enabled" ] && music start
						menu main
					fi
					;;
				"WAD Configuration") loadproc menu wads
					loadproc  menu srb2config
					;;
				"EXE Configuration") loadproc menu exes
					loadproc menu srb2config
					;;
				"Multiplayer") multoggle
					menu srb2config
					;;
				"Controls") loadproc menu controls
					loadproc menu srb2config;;
				"Saved Configs") loadproc menu setconfig
					loadproc menu srb2config;;
				"Back") loadproc menu main;;
			esac
			;;
		## WAD Configuration menu.
		"wads") case $2 in
				"-c") dialog --backtitle "$backtitle" \
					--title "WAD Configuration" \
					--menu "Press 2 to select and use the directional pad to move." 12 68 4 \
					"Load WADs" "Load WADs stored on the $MOUNT into SRB2." \
					"Search for WADs" "Search for WADs on a USB Mass Storage Device." \
					"Delete Config" "Delete the current WAD configuration." \
					"Back" "Return to the Configure SRB2 menu." 2> /tmp/set
					menuitem=`cat /tmp/set`
					case $menuitem in
						"Load WADs") loadproc menu listwads -c $3
							loadproc menu wads -c $3
							;;
						"Search for WADs") loadfunc searchwads
							loadproc menu wads -c $3
							;;
						"Delete Config") rm -rf $HOME/.srb2/conf$3/lwad* $HOME/.srb2/conf$3/wadadd
							menu wads -c $3
							;;
						"Back") menu setconfigmake $3;;
					esac
					;;
				*) dialog --backtitle "$backtitle" \
					--title "WAD Configuration" \
					--menu "Press 2 to select and use the directional pad to move." 12 68 4 \
					"Load WADs" "Load WADs stored on the $MOUNT into SRB2." \
					"Search for WADs" "Search for WADs on a USB Mass Storage Device." \
					"Delete Config" "Delete the current WAD configuration." \
					"Back" "Return to the Configure SRB2 menu." 2> /tmp/set
					menuitem=`cat /tmp/set`
					case $menuitem in
						"Load WADs") loadproc menu listwads
							loadproc menu wads
							;;
						"Search for WADs") if [ "$USBDISK" = "yes" ]; then
								dialog --backtitle "$backtitle" --infobox "The $OSLNAME system is running on a USB stick; it is not necessary to look for files." 4 49
								sleep 5
							else
								loadfunc searchwads
								loadproc menu wads
							fi
							;;
						"Delete Config") rm -rf /tmp/lwad* /tmp/wadadd
							menu wads
							;;
						"Back") loadproc menu srb2config;;
					esac
					;;
			esac
			;;
		## WAD list.
		"listwads") case $2 in
				"-c") cd $HOME/addons
					find -print | grep wad > $HOME/.srb2/conf$3/lwadlist
					# WHY do I do this again?
					# while read line
					# do
					# 	replace=`echo "$line" | tr '_' '-'`
					# 	mv $line $replace
					# done < $HOME/.srb2/conf$3/lwadlist
					# cat $HOME/.srb2/conf$3/lwadlist | tr "_" "-" > $HOME/.srb2/conf$3/lwadlist2
					# mv $HOME/.srb2/conf$3/lwadlist2 $HOME/.srb2/conf$3/lwadlist
					if [ -s $HOME/.srb2/conf$3/wadadd ]
					then
						while read line
						do
							cat $HOME/.srb2/conf$3/lwadlist | sed "/$line/d" > $HOME/.srb2/conf$3/lwadlist2
							mv $HOME/.srb2/conf$3/lwadlist2 $HOME/.srb2/conf$3/lwadlist
						done < $HOME/.srb2/conf$3/wadadd
					fi
					echo "#!/bin/sh" > /tmp/lwadmenu
					echo 'dialog --backtitle "$backtitle" \' >> /tmp/lwadmenu
					echo '--title "Load WADs" \' >> /tmp/lwadmenu
					echo '--menu "Press 2 to select and use the directional pad to move." 17 75 10 \' >> /tmp/lwadmenu
					echo '"Back" "" \' >> /tmp/lwadmenu
					while read line
					do
						wad=`echo "$line" | tr -d './' | sed '$s/...$//'`
						echo -n "$wad.wad '' " >> /tmp/lwadmenu
						echo '\' >> /tmp/lwadmenu
					done < $HOME/.srb2/conf$3/lwadlist
					echo '"Restart WAD config" "" \' >> /tmp/lwadmenu
					echo '"Back" "" 2> /tmp/set' >> /tmp/lwadmenu
					echo 'menuitem=`cat /tmp/set`' >> /tmp/lwadmenu
					echo 'case $menuitem in' >> /tmp/lwadmenu
					echo "'Back') loadproc menu wads -c $3;;" >> /tmp/lwadmenu
					while read line
					do
						wad=`echo "$line" | tr -d './' | sed '$s/...$//'`
						echo "'$wad.wad') echo $wad.wad >> $HOME/.srb2/conf$3/wadadd" >> /tmp/lwadmenu
						echo "sed '/$wad.wad/d' $HOME/.srb2/conf$3/lwadlist > $HOME/.srb2/conf$3/lwadlist2" >> /tmp/lwadmenu
						echo "mv $HOME/.srb2/conf$3/lwadlist2 $HOME/.srb2/conf$3/lwadlist # $wad.wad" >> /tmp/lwadmenu
						echo "sed '/$wad.wad/d' /tmp/lwadmenu > /tmp/lwadmenu2" >> /tmp/lwadmenu
						echo "mv /tmp/lwadmenu2 /tmp/lwadmenu # $wad.wad" >> /tmp/lwadmenu
						echo ". /tmp/lwadmenu;; # $wad.wad" >> /tmp/lwadmenu
					done < $HOME/.srb2/conf$3/lwadlist
					echo "'Restart WAD config') rm -rf $HOME/.srb2/conf$3/wadadd" >> /tmp/lwadmenu
					echo "loadproc menu listwads -c $3;;" >> /tmp/lwadmenu
					echo "esac" >> /tmp/lwadmenu
					. /tmp/lwadmenu
					;;
				*) cd $HOME/addons
					find -print | grep wad > /tmp/lwadlist
					# I see no reason why I am doing this
					# while read line
					# do
					#	replace=`echo "$line" | tr '_' '-'`
					#	mv $line $replace
					# done < /tmp/lwadlist
					# cat /tmp/lwadlist | tr "_" "-" > /tmp/lwadlist2
					# mv /tmp/lwadlist2 /tmp/lwadlist
					if [ -s /tmp/wadadd ]
					then
						while read line
						do
							cat /tmp/lwadlist | sed "/$line/d" > /tmp/lwadlist2
							mv /tmp/lwadlist2 /tmp/lwadlist
						done < /tmp/wadadd
					fi
					echo "#!/bin/sh" > /tmp/lwadmenu
					echo 'dialog --backtitle "$backtitle" \' >> /tmp/lwadmenu
					echo '--title "Load WADs" \' >> /tmp/lwadmenu
					echo '--menu "Press 2 to select and use the directional pad to move." 17 75 10 \' >> /tmp/lwadmenu
					echo '"Back" "" \' >> /tmp/lwadmenu
					while read line
					do
						wad=`echo "$line" | tr -d './' | sed '$s/...$//'`
						echo -n "$wad.wad '' " >> /tmp/lwadmenu
						echo '\' >> /tmp/lwadmenu
					done < /tmp/lwadlist
					echo '"Back" "" 2> /tmp/set' >> /tmp/lwadmenu
					echo 'menuitem=`cat /tmp/set`' >> /tmp/lwadmenu
					echo 'case $menuitem in' >> /tmp/lwadmenu
					echo "'Back') loadproc menu wads;;" >> /tmp/lwadmenu
					while read line
					do
						wad=`echo "$line" | tr -d './' | sed '$s/...$//'`
						echo "'$wad.wad') echo $wad.wad >> /tmp/wadadd" >> /tmp/lwadmenu
						echo "sed '/$wad.wad/d' /tmp/lwadlist > /tmp/lwadlist2" >> /tmp/lwadmenu
						echo "mv /tmp/lwadlist2 /tmp/lwadlist # $wad.wad" >> /tmp/lwadmenu
						echo "sed '/$wad.wad/d' /tmp/lwadmenu > /tmp/lwadmenu2" >> /tmp/lwadmenu
						echo "mv /tmp/lwadmenu2 /tmp/lwadmenu # $wad.wad" >> /tmp/lwadmenu
						echo ". /tmp/lwadmenu;; # $wad.wad" >> /tmp/lwadmenu
					done < /tmp/lwadlist
					echo "esac" >> /tmp/lwadmenu
					chmod +x /tmp/lwadmenu
					. /tmp/lwadmenu
					;;
			esac
			;;
		## EXE configuration menu.
		"exes") case $2 in
				"-c") dialog --backtitle "$backtitle" \
					--title "WAD Configuration" \
					--menu "Press 2 to select and use the directional pad to move." 12 72 4 \
					"Switch EXE" "Load an SRB2 binary stored on the $MOUNT." \
					"Search for EXEs" "Search for EXEs on a USB Mass Storage Device." \
					"Delete Config" "Delete the current EXE configuration." \
					"Back" "Return to the Configure SRB2 menu." 2> /tmp/set
					menuitem=`cat /tmp/set`
					case $menuitem in
						"Switch EXE") loadproc menu exeswitch -c $3
							loadproc menu exes -c $3
							;;
						"Search for EXEs") loadfunc searchexes
							loadproc menu exes -c $3
							;;
						"Delete Config") rm -rf $HOME/.srb2/conf$3/exeadd
							menu exes -c $3
							;;
						"Back") loadproc menu setconfigmake $3;;
					esac
					;;
				*) dialog --backtitle "$backtitle" \
					--title "WAD Configuration" \
					--menu "Press 2 to select and use the directional pad to move." 12 72 4 \
					"Switch EXE" "Load an SRB2 binary stored on the $MOUNT." \
					"Search for EXEs" "Search for EXEs on a USB Mass Storage Device." \
					"Delete Config" "Delete the current EXE configuration." \
					"Back" "Return to the Configure SRB2 menu." 2> /tmp/set
					menuitem=`cat /tmp/set`
					case $menuitem in
						"Switch EXE") loadproc menu exeswitch
							loadproc menu exes
							;;
						"Search for EXEs") if [ "$USBDISK" = "yes" ]; then
								dialog --backtitle "$backtitle" --infobox "The $OSLNAME system is running on a USB stick; it is not necessary to look for files." 4 49
								sleep 5
							else
								loadfunc searchexes
								loadproc menu exes
							fi
							;;
						"Delete Config") rm -rf /tmp/exeadd
							menu exes
							;;
						"Back") loadproc menu srb2config;;
					esac
					;;
			esac
			;;
		## EXE list menu.
		"exeswitch") case $2 in
				"-c") clear
					echo ""
					echo ""
					cd $HOME/binaries
					ls -1 > /tmp/lexelist
					if [ -s $HOME/.srb2/conf$3/exeadd ]
					then
						exe=`cat $HOME/.srb2/conf$3/exeadd | awk '{ print $2 }'`
						echo -n "Creating list of binaries..."
						echo "warning."
						echo "SRB2 binary already loaded. Binary loaded is:"
						echo "$exe"
						echo "Please use the 'delete' option in the EXE loader menu to load another EXE."
						echo "Returning you to the SRB2Wii launcher..."
						sleep 3
						menu exes -c $3
					else
						echo -n "Creating list of binaries..."
						echo "#!/bin/sh" > /tmp/lexemenu
						echo 'dialog --backtitle "$backtitle" \' >> /tmp/lexemenu
						echo '--title "Load EXEs" \' >> /tmp/lexemenu
						echo '--menu "Press 2 to select and use the directional pad to move." 17 75 10 \' >> /tmp/lexemenu
						echo '"Back" "" \' >> /tmp/lexemenu
						while read line
						do
							. $HOME/binaries/$line/bin.conf
							echo -n "'$BLNAME' '' " >> /tmp/lexemenu
							echo '\' >> /tmp/lexemenu
						done < /tmp/lexelist
						echo '"Back" "" 2> /tmp/set' >> /tmp/lexemenu
						echo 'menuitem=`cat /tmp/set`' >> /tmp/lexemenu
						echo 'case $menuitem in' >> /tmp/lexemenu
						echo "'Back') loadproc menu exes -c $3;;" >> /tmp/lexemenu
						while read line
						do
							. $HOME/binaries/$line/bin.conf
							echo "'$BLNAME') loadproc menu exeinfo -c $3 $line" >> /tmp/lexemenu
							echo "loadproc menu exes -c $3;;" >> /tmp/lexemenu
						done < /tmp/lexelist
						echo "esac" >> /tmp/lexemenu
						chmod +x /tmp/lexemenu
						echo "done."
						sleep 1
						. /tmp/lexemenu
					fi
					;;
				*) clear
					echo ""
					echo ""
					cd $HOME/binaries
					ls -1 > /tmp/lexelist
					if [ -s /tmp/exeadd ]
					then
						exe=`cat /tmp/exeadd | awk '{ print $2 }'`
						echo -n "Creating list of binaries..."
						echo "warning."
						echo "SRB2 binary already loaded. Binary loaded is:"
						echo "$exe"
						echo "Please use the 'delete' option in the EXE loader menu to load another EXE."
						echo "Returning you to the SRB2Wii launcher..."
						sleep 3
						menu exes
					else
						echo -n "Creating list of binaries..."
						echo "#!/bin/sh" > /tmp/lexemenu
						echo 'dialog --backtitle "$backtitle" \' >> /tmp/lexemenu
						echo '--title "Load EXEs" \' >> /tmp/lexemenu
						echo '--menu "Press 2 to select and use the directional pad to move." 17 75 10 \' >> /tmp/lexemenu
						echo '"Back" "" \' >> /tmp/lexemenu
						while read line
						do
							. $HOME/binaries/$line/bin.conf
							echo -n "'$BLNAME' '' " >> /tmp/lexemenu
							echo '\' >> /tmp/lexemenu
						done < /tmp/lexelist
						echo '"Back" "" 2> /tmp/set' >> /tmp/lexemenu
						echo 'menuitem=`cat /tmp/set`' >> /tmp/lexemenu
						echo 'case $menuitem in' >> /tmp/lexemenu
						echo "'Back') loadproc menu exes;;" >> /tmp/lexemenu
						while read line
						do
							. $HOME/binaries/$line/bin.conf
							echo "'$BLNAME') loadproc menu exeinfo $line" >> /tmp/lexemenu
							echo "loadproc menu exes;;" >> /tmp/lexemenu
						done < /tmp/lexelist
						echo "esac" >> /tmp/lexemenu
						chmod +x /tmp/lexemenu
						echo "done."
						sleep 1
						. /tmp/lexemenu
					fi
					;;
			esac
			;;
		## EXE conform menu.
		"exeinfo") case $2 in
				"-c") . $HOME/binaries/$4/bin.conf
					dialog --backtitle "$backtitle" \
					--yesno "Name: $BLLNAME
Version: $BINVERSION
Build date: $BLDDATE

Description:
$DESC

Are you sure you want to load this binary?" \
					16 58
					sel=$?
					case $sel in
						"0") echo "$BLNAME $BLNAME" > $HOME/.srb2/conf$3/exeadd;;
					esac
					;;
				*) . $HOME/binaries/$2/bin.conf
					dialog --backtitle "$backtitle" \
					--yesno "Name: $BLLNAME
Version: $BINVERSION
Build date: $BLDDATE

Description:
$DESC

Are you sure you want to load this binary?" \
					16 58
					sel=$?
					case $sel in
						"0") echo "$BLNAME $BLNAME" > /tmp/exeadd;;
					esac
					;;
			esac
			;;
		## Control configuration menu.
		"controls") case $2 in
				"-c") cont1=`cat $HOME/.srb2/conf$3/$OSNAME.conf | grep Control1 | sed -ne 's/.*Control1=\([^ ]*\).*/\1/p'`
					cont2=`cat $HOME/.srb2/conf$3/$OSNAME.conf | grep Control2 | sed -ne 's/.*Control2=\([^ ]*\).*/\1/p'`
					port1="`cat /tmp/gcnport1`"
					port2="`cat /tmp/gcnport2`"
					port3="`cat /tmp/gcnport3`"
					port4="`cat /tmp/gcnport4`"
					dialog --backtitle "$backtitle" \
					--title "Controller Configuration" \
					--menu "Press 2 to select and use the directional pad to move." 14 58 7 \
					"GameCube Port 1" "$port1" \
					"GameCube Port 2" "$port2" \
					"GameCube Port 3" "$port3" \
					"GameCube Port 4" "$port4" \
					"Player 1" "Uses $cont1." \
					"Player 2" "Uses $cont2." \
					"Back" "Return to the Configure SRB2 menu." 2> /tmp/set
					menuitem=`cat /tmp/set`
					newcont=""
					case $cont1 in
						"Wiimote") newcont="GCNControllerCoop";;
						"GCNControllerCoop") newcont="GCNControllerMatch";;
						"GCNControllerMatch") newcont="Wiimote";;
					esac
					case $cont2 in
						"Wiimote") newcont2="GCNControllerCoop";;
						"GCNControllerCoop") newcont2="GCNControllerMatch";;
						"GCNControllerMatch") newcont2="Wiimote";;
					esac
					case $menuitem in
						"Player 1") contfig 1 $newcont
							loadproc menu controls
							;;
						"Player 2") contfig 2 $newcont2
							loadproc menu controls
							;;
						"Back") loadproc menu setconfigmake;;
					esac
					;;
				*) cont1=`cat $conf | grep Control1 | sed -ne 's/.*Control1=\([^ ]*\).*/\1/p'`
					cont2=`cat $conf | grep Control2 | sed -ne 's/.*Control2=\([^ ]*\).*/\1/p'`
					port1="`cat /tmp/gcnport1`"
					port2="`cat /tmp/gcnport2`"
					port3="`cat /tmp/gcnport3`"
					port4="`cat /tmp/gcnport4`"
					dialog --backtitle "$backtitle" \
					--title "Controller Configuration" \
					--menu "Press 2 to select and use the directional pad to move." 14 58 7 \
					"GameCube Port 1" "$port1" \
					"GameCube Port 2" "$port2" \
					"GameCube Port 3" "$port3" \
					"GameCube Port 4" "$port4" \
					"Player 1" "Uses $cont1." \
					"Player 2" "Uses $cont2." \
					"Back" "Return to the Configure SRB2 menu." 2> /tmp/set
					menuitem=`cat /tmp/set`
					newcont2=""
					case $cont1 in
						"Wiimote") newcont="GCNControllerCoop";;
						"GCNControllerCoop") newcont="GCNControllerMatch";;
						"GCNControllerMatch") newcont="Wiimote";;
					esac
					case $cont2 in
						"Wiimote") newcont2="GCNControllerCoop";;
						"GCNControllerCoop") newcont2="GCNControllerMatch";;
						"GCNControllerMatch") newcont2="Wiimote";;
					esac
					case $menuitem in
						"Player 1") contfig 1 $newcont
							loadproc menu controls
							;;
						"Player 2") contfig 2 $newcont2
							loadproc menu controls
							;;
						"Back") loadproc menu srb2config;;
					esac
					;;
			esac
			;;
		## Set config configuration menu.
		"setconfig") if [ -d $HOME/.srb2/conf1 ]
			then
				conf1="Created."
			else
				conf1="None. Create one!"
			fi
			if [ -d $HOME/.srb2/conf2 ]
			then
				conf2="Created."
			else
				conf2="None. Create one!"
			fi
			if [ -d $HOME/.srb2/conf3 ]
			then
				conf3="Created."
			else
				conf3="None. Create one!"
			fi
			if [ -d $HOME/.srb2/conf4 ]
			then
				conf4="Created."
			else
				conf4="None. Create one!"
			fi
			if [ -d $HOME/.srb2/conf5 ]
			then
				conf5="Created."
			else
				conf5="None. Create one!"
			fi
			if [ -d $HOME/.srb2/conf6 ]
			then
				conf6="Created."
			else
				conf6="None. Create one!"
			fi
			dialog --backtitle "$backtitle" \
			--title "Configure SRB2" \
			--menu "Press 2 to select and use the directional pad to move." 15 62 7 \
			"Config 1" "$conf1" \
			"Config 2" "$conf2" \
			"Config 3" "$conf3" \
			"Config 4" "$conf4" \
			"Config 5" "$conf5" \
			"Config 6" "$conf6" \
			"Back" "Return to the Main Menu." 2> /tmp/set
			menuitem=`cat /tmp/set`
			case $menuitem in
				"Config 1") loadproc menu setconfigmode 1
					loadproc menu setconfig
					;;
				"Config 2") loadproc menu setconfigmode 2
					loadproc menu setconfig
					;;
				"Config 3") loadproc menu setconfigmode 3
					loadproc menu setconfig
					;;
				"Config 4") loadproc menu setconfigmode 4
					loadproc menu setconfig
					;;
				"Config 5") loadproc menu setconfigmode 5
					loadproc menu setconfig
					;;
				"Config 6") loadproc menu setconfigmode 6
					loadproc menu setconfig
					;;
				"Back") loadproc menu srb2config;;
			esac
			;;
		## Set config modification menu.
		"setconfigmode") if [ -d $HOME/.srb2/conf$2 ]
			then
				dialog --backtitle "$backtitle" \
				--title "Configure SRB2" \
				--menu "Press 2 to select and use the directional pad to move." 11 75 4 \
				"Load" "Load this set configuration." \
				"Modify" "Modify this configuration." \
				"Delete" "Delete this configuration." \
				"Back" "Back to set configuration menu." 2> /tmp/set
				menuitem=`cat /tmp/set`
				case $menuitem in
					"Load") setconfig $2 && loadproc menu setconfig;;
					"Modify") loadproc menu setconfigmake $2 && loadproc menu setconfig;;
					"Delete") rm -rf $HOME/.srb2/config$2 && menu setconfig;;
					"Back") loadproc menu setconfig;;
				esac
			else
				dialog --backtitle "$backtitle" \
				--title "Configure SRB2" \
				--menu "Press 2 to select and use the directional pad to move." 7 75 2 \
				"Create" "Create a set configuration." \
				"Back" "Back to set configuration menu." 2> /tmp/set
				menuitem=`cat /tmp/set`
				case $menuitem in
					"Create") loadproc menu setconfigmake $2
						loadproc menu setconfig
						;;
					"Back") loadproc menu setconfig;;
				esac
			fi
			;;
		## Set config actual modification menu.
		"setconfigmake") if [ -d $HOME/.srb2/conf$2 ]
			then
				players=`cat $HOME/.srb2/conf$1/$OSNAME.conf | grep Multiplayer | sed -ne  's/.*Multiplayer=\([^ ]*\).*/\1/p'`
				dialog --backtitle "$backtitle" \
				--title "Configure SRB2" \
				--menu "Press 2 to select and use the directional pad to move." 11 62 5 \
				"WAD Configuration" "Configure WADs for SRB2 to load." \
				"EXE Configuration" "Configure an SRB2 binary to load." \
				"Multiplayer" "Toggle multiplayer support ($players)." \
				"Controls" "Set up controllers to use with SRB2." \
				"Back" "Save settings and return to the set config menu." 2> /tmp/set
				menuitem=`cat /tmp/set`
				case $menuitem in
					"WAD Configuration") loadproc menu wads -c $2
						loadproc menu srb2config
						;;
					"EXE Configuration") loadproc menu exes -c $2
						loadproc menu srb2config
						;;
					"Multiplayer") multoggle -c $2
						menu srb2config
						;;
					"Controls") loadproc menu controls -c $2
						loadproc menu srb2config
						;;
					"Back") loadproc menu main;;
				esac
			else
				mkdir $HOME/.srb2/conf$2
				echo "## SRB2Wii configuration script. Automatically generated by the SRB2Wii launcher." > $HOME/.srb2/conf$2/$OSNAME.conf
				echo "Multiplayer=Disabled" >> $HOME/.srb2/conf$2/$OSNAME.conf
				echo "Control1=Wiimote" >> $HOME/.srb2/conf$2/$OSNAME.conf
				echo "Control2=Wiimote" >> $HOME/.srb2/conf$2/$OSNAME.conf
				players=`cat $HOME/.srb2/conf$2/$OSNAME.conf | grep Multiplayer | sed -ne  's/.*Multiplayer=\([^ ]*\).*/\1/p'`
				dialog --backtitle "$backtitle" \
				--title "Configure SRB2" \
				--menu "Press 2 to select and use the directional pad to move." 11 62 5 \
				"WAD Configuration" "Configure WADs for SRB2 to load." \
				"EXE Configuration" "Configure an SRB2 binary to load." \
				"Multiplayer" "Toggle multiplayer support ($players)." \
				"Controls" "Set up controllers to use with SRB2." \
				"Back" "Save settings and return to the set config menu." 2> /tmp/set
				menuitem=`cat /tmp/set`
				case $menuitem in
					"WAD Configuration") loadproc menu wads -c $2
						loadproc menu srb2config
						;;
					"EXE Configuration") loadproc menu exes -c $2
					menu srb2config
						;;
					"Multiplayer") multoggle -c $2
					loadproc menu srb2config
						;;
					"Controls") loadproc menu controls -c $2
					loadproc menu srb2config
						;;
					"Back") loadproc menu main;;
				esac
			fi
			;;
		## Settings menu.
		"settings") TUNES=`cat $conf | grep Backtune | sed -ne  's/.*Backtune=\([^ ]*\).*/\1/p'`
			case $TUNES in
				"Disabled") music stop;;
				*) madplay -Z $backtune > /dev/null 2>&1 &;;
			esac
			[ -f $SDCARD/linux/$FILE_SIZE_SWAP_2-swap ] && EXSWAPSTAT="Delete" || EXSWAPSTAT="Create"
			dialog --backtitle "$backtitle" \
			--title "SRB2Wii Configuration" \
			--menu "Press 2 to select and use the directional pad to move." 11 80 5 \
			"$EXSWAPSTAT swap" "$EXSWAPSTAT the extended swap file." \
			"Reset Configuration" "Delete and recreate all configuration files." \
			"Network" "Configure network settings." \
			"Background Tune" "Background Tune is currently $TUNES." \
			"Back" "Return to the Main Menu." 2> /tmp/set
			menuitem=`cat /tmp/set`
			case $menuitem in
				"Delete swap") swapoff $SDCARD/linux/$FILE_SIZE_SWAP_2-swap > /dev/null 2>&1
					rm -rf $SDCARD/linux/$FILE_SIZE_SWAP_2-swap
					loadproc menu settings
					;;
				"Create swap") dialog --backtitle "$backtitle" --infobox "Creating the extended swap file..." 3 38
						dd if=/dev/zero of=$SDCARD/linux/$FILE_SIZE_SWAP_2-swap bs=$BLOCK_SIZE count=$FILE_SIZE_SWAP_2 > /dev/null 2>&1
						status=$?
						if [ "$status" != "0" ]; then
							dialog --backtitle "$backtitle" --infobox "An error occured. Check if you have at least $FILE_SIZE_SWAP_2 MB available on your $MOUNT, and if there is sufficient space, try reformatting the partition." 5 58
							rm -rf $SDCARD/linux/$FILE_SIZE_SWAP_2-swap > /dev/null 2>&1
							sleep 5
						else
							mkswap $SDCARD/linux/$FILE_SIZE_SWAP_2-swap > /dev/null 2>&1
							swapon $SDCARD/linux/$FILE_SIZE_SWAP_2-swap
						fi
						loadproc menu settings
						;;
				"Reset Configuration") loadfunc reset
					loadproc menu settings
					;;
				"Network") loadproc menu network
					loadproc menu settings
					;;
				"Background Tune") tunetoggle
					loadproc menu settings
					;;
				"Back") loadproc menu main;;
			esac
			;;
		## Network configuration menu.
		"network") NETWORKING=`cat $SYSCONF | grep Networking | sed -ne 's/.*Networking=\([^ ]*\).*/\1/p'`
			case $LOADER in
				"mini") dialog --backtitle "$backtitle" \
					--title "Network Configuration" \
					--menu "Press 2 to select and use the directional pad to move." 13 66 6 \
					"Network Type" "Choose your network type (do this first)." \
					"Wireless Info" "Set Wi-Fi SSID and passwords." \
					"Network Info" "Configure network information." \
					"Save" "Save settings to disk." \
					"Toggle Networking" "Networking is currently $NETWORKING." \
					"Back" "Return to the SRB2Wii Configuration Menu." 2> /tmp/set
					menuitem=`cat /tmp/set`
					case $menuitem in
						"Network Type") loadfunc netconf1
							menu network
							;;
						"Wireless Info") loadfunc netconf2
							menu network
							;;
						"Network Info") loadfunc netconf3
							menu network
							;;
						"Save") netsave
							menu network
							;;
						"Toggle Networking") nettoggle
							menu network
							;;
						"Back") menu settings;;
					esac
					;;
				"ios") case $2 in
						"-s") echo -n "";;
						*) dialog --backtitle "$backtitle" \
							--infobox "Because you are running under IOS (Homebrew Channel), you can only use a supported USB-to-Ethernet adapter and not the built-in Wi-Fi." 7 42
							sleep 2
							cat $SYSCONF | sed "/Encryption/d" | sed "/NetInterface/d" > $SYSCONF.2
							echo "Encryption=Ethernet" >> $SYSCONF.2
							echo "NetInterface=eth0" >> $SYSCONF.2
							rm -rf $SYSCONF
							mv $SYSCONF.2 $SYSCONF
							;;
					esac
					dialog --backtitle "$backtitle" \
					--title "Network Configuration" \
					--menu "Press 2 to select and use the directional pad to move." 11 66 4 \
					"Network Info" "Configure network information." \
					"Save" "Save settings to disk." \
					"Toggle Networking" "Networking is currently $NETWORKING." \
					"Back" "Return to the SRB2Wii Configuration Menu." 2> /tmp/set
					menuitem=`cat /tmp/set`
					case $menuitem in
						"Network Info") loadfunc netconf3
							loadproc menu network -s
							;;
						"Save") netsave
							menu network -s
							;;
						"Toggle Networking") nettoggle
							loadproc menu network -s
							;;
						"Back") loadproc menu settings;;
					esac
					;;
			esac
			;;
	esac
}

##################################################
## This is the launcher initialisation process. ##
##################################################

## This checks if the configuration is available.
## If yes, load settings from it, if not, create it with default settings.      
if ! [ -f $conf ]; then
	cat > $conf << EOF
## $OSLNAME configuration file. Automatically generated by the $OSLNAME launcher.
Control1=Wiimote
Control2=Wiimote
Multiplayer=Disabled
Backtune=Enabled
EOF
fi

## Loads the configuration for the background music as a variable and enables the launcher to check whether its enabled or not during startup.
TUNES=`cat $conf | grep Backtune | sed -ne 's/.*Backtune=\([^ ]*\).*/\1/p'`

## Display this program's help information.
displayhelp ()
{
	if [ "$displayhelp_done" != "yes" ]; then
		cat << EOF
Usage: `basename $0` [OPTION]...
$OSLLNAME launcher, to configure parameters and set up the system.

  -s, --silent		Start without Wii Remote warning and music.
  -c, --constant	Start without Wii Remote warning.
  -h, --help		Display this help screen.

Report `basename $0` bugs to gcfreak_ag20@hotmail.com
$OSLNAME launcher support page: <http://mb.srb2.org/showthread.php?t=30797>
EOF
		displayhelp_done="yes"
	fi
}

## This checks if the launcher was executed with any parameters, and configures the launcher with those parameters. ##
# -s, --silent: goes straight to menu without the Wii Remote message or loading the music in the background.
# -c, --constant: skips the Wii Remote message but still loads the music in the background if enabled in the configuration file.
# -h, --help: bring up a help screen to the user.
# Anything else is passed on to the launcher as a function.
##

## Count number of parameters passed to program.
PARAMNUM=0
while [ "$PARAMNUM" -le "$#" ]; do
	if [ "$PARAMNUM" != "0" ]; then
		echo -n 'echo $' >> /tmp/temp
		echo "$PARAMNUM >> /tmp/temp2" >> /tmp/temp
	fi
	if [ "$PARAMNUM" = "0" ] && [ "$#" = "0" ]; then
		NOPARAMS="yes"
	fi
	PARAMNUM="`expr $PARAMNUM + 1`"
done
## Any required parameters the program needs is put in this variable.
REQUIREDPARAMS=""
## Process parameters.
if [ "$NOPARAMS" != "yes" ]; then
	[ -f /tmp/temp ] && sh /tmp/temp $*
	[ -f /tmp/temp2 ] || touch /tmp/temp2
	## Process parameters and behave accordingly.
	while read PARAM; do
		case "$PARAM" in
			-s|--silent) setloadfunc menu main && setexit 0;;
			-c|--constant) if [ "$STUNES" = "Enabled" ]; then
					setloadfunc loadfunc music start
					setloadfunc loadproc menu main
					bsetexit 0
				else
					setloadfunc loadproc menu main
					bsetexit 0
				fi
				;;
			-h|--help) displayhelp && setexit 0;;  ## Display help and exit
			*) if [ ! "$*" ]; then
					setloadfunc $*
					bsetexit 0
				fi
				;; ## run whatever is in the parameters
		esac
	done < /tmp/temp2
fi

## Exit if asked to.
checkexit
# testreqparams - don't need you to run, no required parameters
checkloadfunc
## Exit if asked to.
checkexit
## Exit if asked to (for things that don't quit immediately).
bcheckexit

## If it gets this far, it will run the normal operation with the Wii Remote warning and start music.
dialog --backtitle "$backtitle" --infobox "Please hold the Wii Remote on its side." 3 44
sleep 2
[ "$TUNES" = "Enabled" ] && loadfunc music start
loadproc menu main
