#!/bin/bash

# Author : epithetic

function MissingFiles {
	echo -e "\nPlease make sure the unip folders and this script are extracted to your classic CaveStory root directory with the original AGTP patch applied (same folder as the game executable file)."
	exit
}


clear
echo "---------------------------------------"
echo "---- AGTP+ for CaveStory installer ----"
echo -e "---------------------------------------\n"

translation=0
graphics=0
extract=0
install=0

# Check if installers and game files present

if [[ ! -d "doukutsu-en2-unip" ]] || [[ ! -d "doukutsu-ui+-unip" ]] || [[ ! -f "PonderosaCS.fon" ]] || [[ ! -f "Doukutsu.exe" ]] || [[ ! -d "data" ]]; then MissingFiles; fi

# Installation choices

./run-unip.sh s doukutsu-en2-unip
./run-unip.sh s doukutsu-ui+-unip

echo -e "Please choose which version of the game you'd like to play:\n"

echo "1. CaveStory classic with the AGTP+ translation"
echo "2. CaveStory classic with the AGTP+ translation and graphics patch"
while true; do
	read choice
	case "$choice" in
		[1]* ) translation=1; break;;
		[2]* ) translation=1; graphics=1; break;;
		* ) echo "Please select 1 or 2.";;
	esac
done



clear
echo "---------------------------------------"
echo "---- AGTP+ for CaveStory installer ----"
echo -e "---------------------------------------\n"

# Comment out extract only as some combinations would fail due to dependencies 

echo -e "Please choose whether to install, extract, or both:\n"

echo "1. Install these patches to my CaveStory installation"
echo "2. Install these patches and extract them as plain files for local backup"
# echo "3. Only extract these patches as plain files for local backup"
while true; do
	read choice
	case "$choice" in
		[1]* ) install=1; break;;
		[2]* ) extract=1; install=1; break;;
		#[3]* ) extract=1; break;;
		* ) echo "Please select 1 or 2.";;
	esac
done


clear
echo "---------------------------------------"
echo "---- AGTP+ for CaveStory installer ----"
echo -e "---------------------------------------\n"

echo -e "This translation includes a Windows bitmap font to allow for crisp font rendering regardless of resolution. In order for the game to display correctly, make sure you have the font installed in your Wine environment and the game's internal resolution is set to 640x480 (which can then be scaled to any resolution by Windows).\n"

echo -e "NOTE: If you don't want to use the font, make sure you restore your Config.dat from unip-backup/ as this install will change the default font setting.\n"

read -p "Press any key to continue"


# Chain all the installs now

# AGTP+ section
if [[ "$translation" -eq 1 ]]; then
	if [[ "$extract" -eq 1 ]]; then
		./run-unip.sh extract doukutsu-en2-unip
		read -p "Press any key to continue"
	fi
	if [[ "$install" -eq 1 ]]; then
		./run-unip.sh install doukutsu-en2-unip
		read -p "Press any key to continue"
	fi
fi

# UI patch section
if [[ "$graphics" -eq 1 ]]; then
	if [[ "$extract" -eq 1 ]]; then
		./run-unip.sh extract doukutsu-ui+-unip
		read -p "Press any key to continue"
	fi
	if [[ "$install" -eq 1 ]]; then
		./run-unip.sh install doukutsu-ui+-unip
		read -p "Press any key to continue"
	fi
fi

echo -e "\nFinished installing selection."
exit