@echo off
setlocal enabledelayedexpansion

:: Self-elevate the script if not already running as administrator
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"

echo ====================================================
echo The World Reflected in Crimson Eyes  Patch Installer (Admin Mode)
echo ====================================================
echo.

:: Check if necessary files exist
if not exist "Shinku.hcb" (
    echo ERROR: Original file "Shinku.hcb" not found.
    echo Please place this batch file in the same directory as your original game files.
    goto :error
)

if not exist "graph.bin" (
    echo ERROR: Original file "graph.bin" not found.
    echo Please place this batch file in the same directory as your original game files.
    goto :error
)

if not exist "hitomipatch\xdelta3.exe" (
    echo ERROR: xdelta3.exe not found in the hitomipatch folder.
    goto :error
)

if not exist "hitomipatch\Shinku.hitomipatch" (
    echo ERROR: Patch file "Shinku.hitomipatch" not found in the hitomipatch folder.
    goto :error
)

if not exist "hitomipatch\graph.hitomipatch" (
    echo ERROR: Patch file "graph.hitomipatch" not found in the hitomipatch folder.
    goto :error
)

echo Creating backup of original files...
if not exist "backup" mkdir backup
copy "Shinku.hcb" "backup\Shinku.hcb.bak" > nul
copy "graph.bin" "backup\graph.bin.bak" > nul
echo Backup created in the "backup" folder.
echo.

echo Applying patches...
echo.

echo Patching Shinku.hcb...
hitomipatch\xdelta3.exe -d -s "Shinku.hcb" "hitomipatch\Shinku.hitomipatch" "Shinku.hcb.new"
if %errorlevel% neq 0 (
    echo Failed to patch Shinku.hcb!
    goto :error
)

echo Patching graph.bin...
hitomipatch\xdelta3.exe -d -s "graph.bin" "hitomipatch\graph.hitomipatch" "graph.bin.new"
if %errorlevel% neq 0 (
    echo Failed to patch graph.bin!
    goto :error
)

echo.
echo Replacing original files with patched versions...
move /y "Shinku.hcb.new" "Shinku.hcb" > nul
move /y "graph.bin.new" "graph.bin" > nul

echo.
echo ====================================================
echo Patching completed successfully!
echo.
echo The game has been successfully patched with the translation.
echo Original files were backed up to the "backup" folder.
echo ====================================================
echo.
goto :end

:error
echo.
echo ====================================================
echo Patching process failed!
echo ====================================================
echo.
echo Read the instructions again.
pause
exit /b 1

:end
echo Press any key to exit...
pause > nul
exit /b 0
