@echo off
chcp 65001 >NUL
SET MKCA_version=1.0.1
SET MKCA_type=English Translation Patch
SET MKCA_game=G-MODE Archives+ Deadly Danmaku Trial - DoDonPachi Dai-Ou-Jou Edition
SET MKCA_patch_file_name=ddt_ddp_doje
SET MKCA_data_folder=DanmakuKenteiShiken_Dodonpachi_Data
SET MKCA_game_folder=GmodeArchivesPlus_DanmakuKenteiShiken_Dodonpachi
SET MKCA_game_exe=DanmakuKenteiShiken_Dodonpachi
SET MKCA_url=Deadly%%20Danmaku%%20Trial%%20-%%20DoDonPachi%%20Dai-Ou-Jou%%20Edition


title %MKCA_game%   %MKCA_type% %MKCA_version%

echo.
echo ————————————————————
echo This is the English translation patch for the Steam version of %MKCA_game%.
echo.
echo This is %MKCA_version% version of the patch.
echo.
echo Latest version can always be found here:
echo    github.com/MKCAMK/G-MODE-Archives-translations/tree/main/patches/%MKCA_url%
echo.

choice /n /m "Do you want to apply this patch now? [Y/N]:"

if errorlevel 2 goto Abort

echo.
echo First, you need to have placed this patch into the game's folder.
echo.
choice /n /m "Has this already been done? Are you running this patch from the game's folder? [Y/N]:"

if errorlevel 2 goto PlaceFiles

:Ready
echo.
echo Everything is ready. Before starting, I need to warn you that the game's files will be OVERWRITTEN. If you want to make a backup, do it before proceeding.
echo.
choice /n /m "Having been warned, do you want to proceeded now? [Y/N]:"

if errorlevel 2 goto Abort

echo.
choice /n /m "... Or do you actually want to abort? [Y/N]:"

if errorlevel 2 goto Patch

echo.
choice /n /m "Wait, you do want to abort? [Y/N]:"

if errorlevel 2 goto Ready

goto Abort

:Patch
echo.
echo Applying the patch...
echo ————————————————————
echo.

SET MKCA_work_folder_absolute=%~dp0%MKCA_data_folder%[patched]
SET MKCA_data_folder_absolute=%~dp0%MKCA_data_folder%
SET MKCA_backup_folder=%MKCA_data_folder%[backup]
SET MKCA_backup_folder_absolute=%~dp0%MKCA_backup_folder%

if not exist "%MKCA_data_folder_absolute%" ( 
SET /A MKCA_error_code=-1
goto Error
)

if exist "%MKCA_work_folder_absolute%" rd /s /q "%MKCA_work_folder_absolute%"

"%~dp0%MKCA_patch_file_name%.%MKCA_version%.exe" "%MKCA_data_folder_absolute%" -X "%MKCA_work_folder_absolute%" >NUL
SET /A MKCA_error_code = %ERRORLEVEL%
if %MKCA_error_code% NEQ 0 goto Error

if exist "%MKCA_backup_folder_absolute%" rd /s /q "%MKCA_backup_folder_absolute%"

if exist "%MKCA_backup_folder_absolute%" ( 
SET /A MKCA_error_code=-2
goto Error
)

ren "%MKCA_data_folder_absolute%" "%MKCA_backup_folder%"

if exist "%MKCA_data_folder_absolute%" ( 
SET /A MKCA_error_code=-3
goto Error
)

ren "%MKCA_work_folder_absolute%" "%MKCA_data_folder%"

if not exist "%MKCA_data_folder_absolute%" ( 
ren "%MKCA_backup_folder_absolute%" "%MKCA_data_folder%"
SET /A MKCA_error_code=-4
goto Error
)

if exist "%MKCA_backup_folder_absolute%" rd /s /q "%MKCA_backup_folder_absolute%"

echo.
echo ————————————————————
echo All done. Thank you for using this patch!

goto Pause

:PlaceFiles
echo.
echo It this case you will need to close this window, move the two files that make up the patch (%MKCA_patch_file_name%.%MKCA_version%-PATCH.bat, %MKCA_patch_file_name%.exe) into the game's folder, and launch this patch again, this time from the game's folder.
echo.
echo The game's folder is likely to be located downstream of the Steam's folder, here:
echo    ...\Steam\steamapps\common\%MKCA_game_folder%
echo.
echo It should contain the following items:
echo    - %MKCA_data_folder%
echo    - MonoBleedingEdge
echo    - %MKCA_game_exe%.exe
echo    - UnityCrashHandler64.exe
echo    - UnityPlayer.dll
echo.
echo You can launch this patch again once the files have been placed in the game's folder.

goto Pause

:Abort
echo.
echo OK. Take care!

goto Pause

:Error
echo.
echo ————————————————————
echo An error has occurred. Its code was: %MKCA_error_code%
echo.
echo It is possible that the patch files have not been put in the game's folder.
echo    Make sure the patch files are in the game's folder.
echo.
echo It is possible that the game has already been patched. 
echo    Make sure that the game is a fresh install. Installing over the older versions of the patch is not currently supported.
echo.
echo It is possible that the game files are corrupted.
echo    Reinstall the game, and try applying the patch again.
echo.
echo Finally, it is possible that the game has been updated, and the patch is no longer working.
echo    If nothing else works, you may want to seek help from the creators of this patch.
echo    You can find them here:
echo       github.com/MKCAMK/G-MODE-Archives-translations

goto Pause

:Pause
echo.
echo ————————————————————
pause

exit /B %MKCA_error_code%