#!/data/data/com.termux/files/usr/bin/bash
source pidejl/includes.sh
rompath=storage/external-1/Intellivision
game=$rompath/$1
ListGames(){
	cd $rompath
	ls *.bin *.int *.itv *.rom | sed 's/\(.*\)\..*/\1/'
	#for f in *.rom ; do
		#echo ${f%.rom}
	#done
	cd
}
clear
if [ -z "$1" ] ; then
	ListGames
	echo -e "${LightRed}Choose from the above list and enter the name of the game in quotes.${NoColor}"
elif [[ ! -e $game.bin && ! -e $game.rom && ! -e $game.cc3 && ! -e $game.int && ! -e $game.itv ]] ; then
	ListGames
	echo -e "${LightRed}$1 not found.  Sorry.${NoColor}"
elif [ -e "dasm/$1.asm" ] ; then
	geany "$game (Hack).cfg" "dasm/$1.asm" &
	#echo geany returned exit code $?
else
	echo -e "${White}Step 1, converting to .bin${NoColor}"
	if [ -e "$game.bin" ] ; then
		echo -e "${Yellow}$1.bin already exists.${NoColor}"
	else
		rom2bin "$game.rom"
	fi
	DrawThatLine
	echo -e "${White}Step 2, disassembling.${NoColor}"
	if [ -e "dasm/$1.asm" ] ; then
		echo -e "${Yellow}dasm/$1.asm already exists.${NoColor}"
	else
		dasm1600 "$game.bin" > "dasm/$1.asm"
	fi
	DrawThatLine
	echo -e "${White}Step 3, renaming and opening.${NoColor}"
	if [ ! -e "$game (Hack).bin" ] ; then
		mv "$game.bin" "$game (Hack).bin"
	fi
	if [ ! -e "$game (Hack).cfg" ] ; then
		mv "$game.cfg" "$game (Hack).cfg"
	fi
	geany "$game (Hack).cfg" "dasm/$1.asm" &
	#echo geany returned exit code $?
fi
