#!/bin/bash

# Répertoire absolu du fichier
RELPAT="$(dirname $0)"

function menup {
	select_menup=$(zenity --list --title="Main Menu" \
				--text="Please choose an item" \
				--column="select" --column=" " \
					"quit" "Quit" \
					"cas" "TI-nSpire CAS" \
					"noncas" "TI-nSpire" \
					"binrun" "Bin File" \
					"aide" "Help" \
				--hide-column="1" \
				--height="300" \
				--width="500");
}


function cas {
	select_cas=$(zenity --list --title="TI-nSpire CAS Menu" \
				--text="Please choose an item" \
				--column="select" --column=" " \
					"return" "Return" \
					"extboot2" "Extract boot2" \
					"romcas" "Create a ROM file" \
					"runcasb1" "Launch a ROM file with boot1" \
					"runcasb2" "Launch a ROM file with boot2" \
				--hide-column="1" \
				--height="300" \
				--width="500");
}


function noncas {
	select_noncas=$(zenity --list --title="TI-nSpire Menu" \
				--text="Please choose an item" \
				--column="select" --column=" " \
					"return" "Return" \
					"extboot2" "Extract boot2" \
					"romnoncas" "Create a ROM file" \
					"runnoncasb1" "Launch a ROM file with boot1" \
					"runnoncas84b1" "Launch a ROM file in 84+ mode with boot1" \
					"runnoncasb2" "Launch a ROM file with boot2" \
					"runnoncas84b2" "Launch a ROM file in 84+ mode with boot2" \
				--hide-column="1" \
				--height="300" \
				--width="500");
}

function binrun {
	binfile=$(zenity --file-selection --title="Select a *.bin file"  --height="30" --width="500"  --filename="$RELPAT/");
	if [ "$binfile" ]; then
		"$RELPAT"/nspire_emu /B="$binfile"
	fi
}

function extboot2 {
	clear
	echo "#####################################################"
	echo "#       Extract the Boot2 file from boot2.img       #"
	echo "#####################################################"
	wine "$RELPAT"/imgdump.exe boot2.img
}

function romnoncas {
	os=$(zenity --file-selection --title="Select a *.tno file" --filename="$RELPAT/");
	if [ "$os" ]; then
		if [ "${os##*.}" = "tno" ]; then
			rom=$(zenity --file-selection --title="Save a ROM file" --save --confirm-overwrite --filename="$RELPAT/");
			if [ "$rom" ]; then
				if [ "${rom##*.}" != "rom" ]; then
					rom="$rom.rom"
				fi
				"$RELPAT"/nspire_emu /F="$rom" /N /PB=boot2.img /PO="$os"
			fi
		else
			zenity --error --title="Error" --text="Please select a *.tno file and not any other stuff ! \n Operation canceled"
		fi
	fi
}

function romcas {
	os=$(zenity --file-selection --title="Select a *.tnc file" --filename="$RELPAT/");
	if [ "$os" ]; then
		if [ "${os##*.}" = "tnc" ]; then
			rom=$(zenity --file-selection --title="Save a ROM file" --save --confirm-overwrite --filename="$RELPAT/");
			if [ "$rom" ]; then
				if [ "${rom##*.}" != "rom" ]; then
					rom="$rom.rom"
				fi
				"$RELPAT"/nspire_emu /F="$rom" /N /PB=boot2.img /PO="$os" /C
			fi
		else
			zenity --error --title="Error" --text="Please select a *.tnc file and not any other stuff ! \n Operation canceled"
		fi
	fi
}

function runnoncasb1 {
	rom=$(zenity --file-selection --title="Select a *.rom file" --filename="$RELPAT/");
	if [ "$rom" ]; then
		if [ "${rom##*.}" = "rom" ]; then
			boot1=$(zenity --file-selection --title="Select a BOOT1 file" --filename="$RELPAT/");
			if [ "$boot1" ]; then
				"$RELPAT"/nspire_emu /F="$rom" /1="$boot1"
			fi
		else
			zenity --error --title="Error" --text="Please select a *.rom file and not any other stuff ! \n Operation canceled"
		fi
	fi
}

function runnoncas84b1 {
	rom=$(zenity --file-selection --title="Select a *.rom file" --filename="$RELPAT/");
	if [ "$rom" ]; then
		if [ "${rom##*.}" = "rom" ]; then
			boot1=$(zenity --file-selection --title="Select a BOOT1 file" --filename="$RELPAT/");
			if [ "$boot1" ]; then
				"$RELPAT"/nspire_emu /F="$rom" /1="$boot1" /K
			fi
		else
			zenity --error --title="Error" --text="Please select a *.rom file and not any other stuff ! \n Operation canceled"
		fi
	fi
}

function runcasb1 {
	rom=$(zenity --file-selection --title="Select a *.rom file" --filename="$RELPAT/");
	if [ "$rom" ]; then
		if [ "${rom##*.}" = "rom" ]; then
			boot1=$(zenity --file-selection --title="Select a BOOT1 file" --filename="$RELPAT/");
			if [ "$boot1" ]; then
				"$RELPAT"/nspire_emu /F="$rom" /1="$boot1" /C
			fi
		else
			zenity --error --title="Error" --text="Please select a *.rom file and not any other stuff ! \n Operation canceled"
		fi
	fi
}

function runnoncasb2 {
	rom=$(zenity --file-selection --title="Select a *.rom file" --filename="$RELPAT/");
	if [ "$rom" ]; then
		if [ "${rom##*.}" = "rom" ]; then
			"$RELPAT"/nspire_emu /F="$rom" /B=boot2.img.raw
		else
			zenity --error --title="Error" --text="Please select a *.rom file and not any other stuff ! \n Operation canceled"
		fi
	fi
}

function runnoncas84b2 {
	rom=$(zenity --file-selection --title="Select a *.rom file" --filename="$RELPAT/");
	if [ "$rom" ]; then
		if [ "${rom##*.}" = "rom" ]; then
			"$RELPAT"/nspire_emu /F="$rom" /B=boot2.img.raw /K
		else
			zenity --error --title="Error" --text="Please select a *.rom file and not any other stuff ! \n Operation canceled"
		fi
	fi
}

function runcasb2 {
	rom=$(zenity --file-selection --title="Select a *.rom file" --filename="$RELPAT/");
	if [ "$rom" ]; then
		if [ "${rom##*.}" = "rom" ]; then
			"$RELPAT"/nspire_emu /F="$rom" /B=boot2.img.raw /C
		else
			zenity --error --title="Error" --text="Please select a *.rom file and not any other stuff ! \n Operation canceled"
		fi
	fi
}

function aide {
	clear
	echo "#####################################################"
	echo "#                      Help                         #"
	echo "#                   ----------                     ##"
	echo "##         On the usage with boot2 start up        ##"
	echo "#####################################################"
	echo "- Get the Upgrade TI files (*.tnc/*.tno) by downloading it on"
	echo "  TI website, or on TI-bank, where you can find most of the "
	echo "  files that have existed."
	echo "- Copy the file into the emulator root folder."
	echo "- Rename it by an explicit name (eg. : 17.tnc) !"
	echo "- Change the extension into *.zip format"
	echo "- Open the archive an simply extract the 'boot2.img' file into "
	echo "  the emulator root folder"
	echo "- Restore the update TI file extension (*.tnc / *.tno)."
	echo "- Thanks to the batch file, decrypt the 'boot2.img' file."
	echo "- Thanks to the batch file, launch the emulator."
	echo "### Look up the below video tutorial : http://vimeo.com/8884626"
	echo ""	
	echo "Appuyer la touche <Entrée> pour continuer..."
	read touche
}

while [  "$select_menup" != "quit"  ]; do
	menup
	if [ "$select_menup" = "cas" ]; then
		while [ "$select_cas" != "return" ]; do		
			cas		
			if [ "$select_cas" = "extboot2" ]; then
				extboot2
			elif [ "$select_cas" = "romcas" ]; then
				romcas
			elif [ "$select_cas" = "runcasb1" ]; then
				runcasb1
			elif [ "$select_cas" = "runcasb2" ]; then
				runcasb2
			else break;
			fi
		done
	select_cas=$"none"
	elif [ "$select_menup" = "noncas" ]; then
		while [ "$select_noncas" != "return" ]; do			
			noncas
			if [ "$select_noncas" = "extboot2" ]; then
				extboot2
			elif [ "$select_noncas" = "romnoncas" ]; then
				romnoncas
			elif [ "$select_noncas" = "runnoncasb1" ]; then
				runnoncasb1
			elif [ "$select_noncas" = "runnoncas84b1" ]; then
				runnoncas84b1
			elif [ "$select_noncas" = "runnoncasb2" ]; then
				runnoncasb2
			elif [ "$select_noncas" = "runnoncas84b2" ]; then
				runnoncas84b2
			else break;
			fi
		done
	select_noncas=$"none"
	elif [ "$select_menup" = "binrun" ]; then
		binrun	
	elif [ "$select_menup" = "aide" ]; then
		aide	
	else break;
	fi
done

# Credits
clear
zenity 	--info --title="Credits" \
		--text=\
"Thanks for using this Bash shell made by Levak. 
A BIG Thanks to Goplat for its emulator and to xspire for its skin !"
clear
