#!/bin/bash

# File root directory
RELPAT="$(dirname $0)"

# Pause function same on Windows, Ouuaaaa !
function pause {
	echo "Press <Enter> key to continue..."
	read touche
}

function menup {
	clear
	PS3="Chose (1-5)"
	echo "#####################################################"
	echo "#                      Main Menu                    #"
	echo "#####################################################"
	select select_menup in "Quit" "TI-nSpire CAS" "TI-nSpire" "Bin file" "Help"
	do
		break
	done
}

function cas {
	clear	
	PS3="Chose (1-5)"
	echo "#####################################################"
	echo "#                TI-nSpire CAS Menu                 #"
	echo "#####################################################"
	select select_cas in "Return" "Extract boot2 file" "Create a ROM file" "Launch a ROM file with boot1" "Launch a ROM file with boot2"
	do
		break
	done
}

function noncas {
	clear	
	PS3="Chose (1-7)"
	echo "#####################################################"
	echo "#                  TI-nSpire Menu                   #"
	echo "#####################################################"
	select select_noncas in "Return" "Extract boot2 file" "Create a ROM file" "Launch a ROM file with boot1" "Launch a ROM file in 84+ mode with boot1" "Launch a ROM file with boot2" "Launch a ROM file in 84+ mode with boot2"
	do
		break
	done
}

function binrun {
	clear
	echo "#####################################################"
	echo "#              Lancer un fichier *.bin              #"
	echo "#####################################################"
	read -p "Type the *.bin file name to use (without extension) : " -e binfile
	"$RELPAT"/nspire_emu /B="$binfile".bin
	pause
}

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

function romnoncas {
	clear
	echo "#####################################################"
	echo "#          Create a ROM file for TI-nSpire          #"
	echo "#####################################################"
	read -p "Type the *.rom file name to create (without extension) : " -e rom
	read -p "Type the *.tno file name to use (without extension) : " -e os
	"$RELPAT"/nspire_emu /F="$rom".rom /N /PB=boot2.img /PO="$os".tno
	pause
}

function romcas {
	clear
	echo "#####################################################"
	echo "#            Create une ROM TI-nSpire CAS           #"
	echo "#####################################################"
	read -p "Type the *.rom file name to create (without extension) : " -e rom
	read -p "Type the *.tnc file name to use (without extension) : " -e os
	"$RELPAT"/nspire_emu /F="$rom".rom /N /PB=boot2.img /PO="$os".tnc /C
	pause
}

function runnoncasb1 {
	clear
	echo "#####################################################"
	echo "#    Launch a ROM file for TI-nSpire with boot1     #"
	echo "#####################################################"
	read -p "Type the *.rom file name to use (without extension) : " -e rom
	read -p "Type the entire boot1 file name to use : " -e boot1
	"$RELPAT"/nspire_emu /F="$rom".rom /1="$boot1"
	pause
}

function runnoncas84b1 {
	clear
	echo "#####################################################"
	echo "#     Launch a ROM file in 84+ mode with boot1      #"
	echo "#####################################################"
	read -p "Type the *.rom file name to use (without extension) : " -e rom
	read -p "Type the entire boot1 file name to use : " -e boot1
	"$RELPAT"/nspire_emu /F="$rom".rom /1="$boot1" /K
	pause
}

function runcasb1 {
	clear
	echo "#####################################################"
	echo "#     Launch a ROM file TI-nSpire CAS with boot1    #"
	echo "#####################################################"
	read -p "Type the *.rom file name to use (without extension) : " -e rom
	read -p "Type the entire boot1 file name to use : " -e boot1
	"$RELPAT"/nspire_emu /F="$rom".rom /1="$boot1" /C
	pause
}

function runnoncasb2 {
	clear
	echo "#####################################################"
	echo "#     Launch a ROM file for TI-nSpire with boot2    #"
	echo "#####################################################"
	read -p "Type the *.rom file name to use (without extension) : " -e rom
	"$RELPAT"/nspire_emu /F="$rom".rom /B=boot2.img.raw
	pause
}

function runnoncas84b2 {
	clear
	echo "#####################################################"
	echo "#      Launch a ROM file in 84+ mode with boot2     #"
	echo "#####################################################"
	read -p "Type the *.rom file name to use (without extension) : " -e rom
	"$RELPAT"/nspire_emu /F="$rom".rom /B=boot2.img.raw /K
	pause
}

function runcasb2 {
	clear
	echo "#####################################################"
	echo "#   Launch a ROM file for TI-nSpire CAS with boot2  #"
	echo "#####################################################"
	read -p "Type the *.rom file name to use (without extension) : " -e rom
	"$RELPAT"/nspire_emu /F="$rom".rom /B=boot2.img.raw /C
	pause
}

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"
	pause
}

while [  "$select_menup" != "Quit"  ]; do
	menup
	if [ "$select_menup" = "TI-nSpire CAS" ]; then
		while [ "$select_cas" != "Return" ]; do		
			cas		
			if [ "$select_cas" = "Extract boot2 file" ]; then
				extboot2
			elif [ "$select_cas" = "Create a ROM file" ]; then
				romcas
			elif [ "$select_cas" = "Launch a ROM file with boot1" ]; then
				runcasb1
			elif [ "$select_cas" = "Launch a ROM file with boot2" ]; then
				runcasb2
			fi
		done
	select_cas=$"none"
	elif [ "$select_menup" = "TI-nSpire" ]; then
		while [ "$select_noncas" != "Return" ]; do			
			noncas
			if [ "$select_noncas" = "Extract boot2 file" ]; then
				extboot2
			elif [ "$select_noncas" = "Create a ROM file" ]; then
				romnoncas
			elif [ "$select_noncas" = "Launch a ROM file with boot1" ]; then
				runnoncasb1
			elif [ "$select_noncas" = "Launch a ROM file in 84+ mode with boot1" ]; then
				runnoncas84b1
			elif [ "$select_noncas" = "Launch a ROM file with boot2" ]; then
				runnoncasb2
			elif [ "$select_noncas" = "Launch a ROM file in 84+ mode with boot2" ]; then
				runnoncas84b2
			fi
		done
	select_noncas=$"none"
	elif [ "$select_menup" = "Bin file" ]; then
		binrun	
	elif [ "$select_menup" = "Help" ]; then
		aide	
	fi
done

# Credits
clear
echo "Thanks for using this Bash shell made by Levak"
echo "A BIG Thanks to Goplat for its emulator and to xspire for its skin !"
echo ""
pause
clear
