#!/usr/bin/env bash

cd /tmp
sudo apt-get update
sudo apt-get install --yes libsqlite3-dev zlib1g zlib1g-dev libsdl2-dev libsdl2-image-dev libguichan-dev
wget -N -q -O- "http://kat5200.jillybunch.com/downloads/kat5200-0.8.1.tar.gz" | tar -xvz --strip-components=1
# remove F1-key invoking debugger 
sed -i -e '250d' /tmp/src/interface/ui.c
#Since combo button support in v0.8.0 this is no longer needed
#sed -i '1727 a\
#        if ((SDL_JoystickGetButton(g_pc_joy[0].joy_ptr,8) == SDL_PRESSED) && (SDL_JoystickGetButton(g_pc_joy[0].joy_ptr,9) == SDL_PRESSED)) return -1;' /tmp/src/interface/sdl_if.c
./configure
make
sudo make install

if [ ! -d "/home/pi/RetroPie/roms/atari5200" ]; then
 	mkdir /home/pi/RetroPie/roms/atari5200
fi

if [ -f "/home/pi/RetroPie/BIOS/5200.ROM" ]
then
	echo "Bios has been found."
else
	clear
	echo "Bios file not found."
	echo "Please download '5200.ROM' (google is your friend) and place it in the bios directory."
	# is wget bios allowed ?
	read -n 1 -s -p "Press any key to continue"
	clear
fi

if [ ! -d "/opt/retropie/configs/atari5200" ]; then
	mkdir /opt/retropie/configs/atari5200
fi

if [ ! -d "/home/pi/.kat5200" ]; then
	mkdir /home/pi/.kat5200
fi

if [ -f "/etc/kat5200/kat5200.db3" ]; then
	sudo rm /etc/kat5200/kat5200.db3
fi

if [ -f "/home/pi/kat5200.db3" ]; then
	sudo rm /home/pi/kat5200.db3
fi

if [ -f "/home/pi/.kat5200/kat5200.db3" ]
then
	echo "A configuration file is already present."
else
	echo "Default configuration file not found, downloading it."
  	cd /home/pi/.kat5200
	wget -N -O kat5200.db3 https://github.com/futurechild/Kat5200-Retropie/raw/master/default_config.db3
	cd /tmp
fi

if [ -f "/opt/retropie/configs/atari5200/emulators.cfg" ]
then
	echo "Emulators file already present, it will be renamed."
	# maybe use sed to alter this file ?
	mv /opt/retropie/configs/atari5200/emulators.cfg /opt/retropie/configs/atari5200/emulators.cfg_org
	cat <<- EOF > /opt/retropie/configs/atari5200/emulators.cfg
	kat5200 = "/usr/local/bin/kat5200 %ROM%"
	default = "kat5200"
	EOF
else
	echo "Emulator file not found, creating one"
	cat <<- EOF > /opt/retropie/configs/atari5200/emulators.cfg
	kat5200 = "/usr/local/bin/kat5200 %ROM%"
	default = "kat5200"
EOF
fi

# Re-worked to make dryer and avoid duplicate system entries
clear

if [ ! -f "/opt/retropie/configs/all/emulationstation/es_systems.cfg" ]
then
	echo "Emulationstation systems file not found, creating it."
	sudo cp /etc/emulationstation/es_systems.cfg /opt/retropie/configs/all/emulationstation/es_systems.cfg
else
	echo "Emulationstation systems file is already present."
fi
sudo chown pi:pi /opt/retropie/configs/all/emulationstation/es_systems.cfg

fgrep -q 'atari5200' /opt/retropie/configs/all/emulationstation/es_systems.cfg

if [ $? -eq 0 ]
then
	echo "atari5200 system already configured in es_systems.cfg"
else
	sed -i '/<systemList>/a\
  <system>\
    <name>atari5200</name>\
    <fullname>Atari 5200</fullname>\
    <path>/home/pi/RetroPie/roms/atari5200</path>\
    <extension>.7z .a52 .bin .zip .7Z .A52 .BIN .ZIP</extension>\
    <command>/opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ atari5200 %ROM%</command>\
    <platform>atari5200</platform>\
    <theme>atari5200</theme>\
  </system>' /opt/retropie/configs/all/emulationstation/es_systems.cfg
	echo "Emulationstation systems file was missing entry for Atari 5200"
	echo "The following has been added to it :"
	echo " <system>"
	echo "   <name>atari5200</name>"
	echo "   <fullname>Atari 5200</fullname>"
	echo "   <path>/home/pi/RetroPie/roms/atari5200</path>"
	echo "   <extension>.7z .a52 .bin .zip .7Z .A52 .BIN .ZIP</extension>"
	echo "   <command>/opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ atari5200 %ROM%</command>"
	echo "   <platform>atari5200</platform>"
	echo "   <theme>atari5200</theme>"
	echo " </system>"
fi

echo " "
echo "kat5200 should now have been successfully installed"
echo "please reboot your system"