@echo OFF
setlocal enabledelayedexpansion


:Start

cls
echo ---------------------------------------
echo ---- AGTP^+ for CaveStory installer ----
echo ---------------------------------------
echo.


rem Check if installers and game files present

set files=doukutsu-en2-unip doukutsu-ui^+-unip PonderosaCS.fon Doukutsu.exe data

(for %%b in (%files%) do ( 
	if not exist %%b ( goto MissingFiles )
))

rem Installation choices

call run-unip.bat s doukutsu-en2-unip
call run-unip.bat s doukutsu-ui^+-unip

echo Please choose which version of the game you'd like to play:
echo.
echo 1. CaveStory classic with the AGTP^+ translation
echo 2. CaveStory classic with the AGTP^+ translation and graphics patch
set choice=
echo.
set /p choice=Type the corresponding number: 
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto SetTOnly
if '%choice%'=='2' goto SetTandG
echo "%choice%" is not valid, try again
echo.
goto Start




:SetExtract

cls
echo ---------------------------------------
echo ---- AGTP^+ for CaveStory installer ----
echo ---------------------------------------
echo.

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

echo Please choose whether to install, extract, or both:
echo.
echo 1. Install these patches to my CaveStory installation 
echo 2. Install these patches and extract them as plain files for local backup
rem echo 3. Only extract these patches as plain files for local backup
set choice=
echo.
set /p choice=Type the corresponding number: 
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto InstallOnly
if '%choice%'=='2' goto InstallExtract
rem if '%choice%'=='3' goto ExtractOnly
echo "%choice%" is not valid, try again
echo.
goto SetExtract


:FontQuery
cls
echo ---------------------------------------
echo ---- AGTP^+ for CaveStory installer ----
echo ---------------------------------------
echo.

echo 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 and the game's internal resolution is set to 640x480 (which can then be scaled to any resolution by Windows).
echo.
echo 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.
echo.
echo 1. Open Windows dialog for font installation (click "install", then close the preview)
echo 2. I already have the font installed or don't want to use it
set choice=
echo.
set /p choice=Type the corresponding number: 
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto OpenFontDiag
if '%choice%'=='2' goto StartInstall
echo "%choice%" is not valid, try again
echo.
goto FontQuery

:OpenFontDiag
explorer PonderosaCS.fon
echo.
echo Continue to installation when font is installed.
echo.
pause

:StartInstall
rem Chain all the installs now

rem AGTP+ section
if !translation!==1 (
	if !extract!==1 (
		call run-unip.bat extract doukutsu-en2-unip
		pause
	)
	if !install!==1 (
		call run-unip.bat install doukutsu-en2-unip
		pause
	)
)

rem UI patch section

if !graphics!==1 (
	if !extract!==1 (
		call run-unip.bat extract doukutsu-ui^+-unip
		pause
	)
	if !install!==1 (
		call run-unip.bat install doukutsu-ui^+-unip
	)
)


echo.
echo Finished installing selection.
goto End


:InstallOnly
set install=1
set extract=0
set iestring=install
goto FontQuery

:InstallExtract
set install=1
set extract=1
set iestring=install and extract
goto FontQuery

:ExtractOnly
set install=0
set extract=1
set iestring=extract
goto FontQuery

:SetTOnly
set graphics=0
set translation=1
goto SetExtract

:SetTandG
set graphics=1
set translation=1
goto SetExtract

:MissingFiles
echo.
echo Please 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).
goto End

:AbortInstall
echo.
echo Aborting...
goto End


:End
echo.
pause