@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "REGION_URL=https://www.mauq.xyz/files/regionInfo.json"
set "TARGET_PATH="
set "TEMP_PATH=%TEMP%\MAUQ-regionInfo-download.json"
set "BACKUP_PATH=%TEMP%\MAUQ-regionInfo-backup.json"

echo Downloading custom regions from www.mauq.xyz
echo Thanks to Lotus for providing the original installer files.
echo This installer includes:
echo  --------
echo   - Modded NA
echo   - Modded EU
echo   - Modded Asia
echo   - Niko233 NA
echo   - Niko233 EU
echo   - Niko233 Asia
echo   - AOU EU
echo   - MAUL NA
echo   - MAUL EU
echo  --------
echo You can remove a region later by deleting its object from regionInfo.json.
echo.

where curl.exe >nul 2>nul
if errorlevel 1 (
    echo Curl is not installed.
    echo Download %REGION_URL% manually and replace your regionInfo.json file.
    pause
    exit /b 1
)

:menu
echo Which version of Among Us do you have?
echo   1. Steam, Epic Games, or Itch
echo   2. Microsoft Store
echo.
set /p "platformChoice=Enter 1 or 2: "
if "%platformChoice%"=="1" goto desktop
if "%platformChoice%"=="2" goto msstore
echo Invalid selection. Please try again.
echo.
goto menu

:msstore
echo.
set "PKG_ROOT="
for /d %%P in ("%LOCALAPPDATA%\Packages\Innersloth.AmongUs_*") do set "PKG_ROOT=%%P"
if not defined PKG_ROOT (
    echo Could not find the Innersloth.AmongUs package folder.
    echo Please use the manual installation instructions on the download page.
    pause
    exit /b 1
)

echo Scanning WGS save containers for regionInfo data. This may take a moment...
for /f "delims=" %%F in ('powershell.exe -NoProfile -Command "Get-ChildItem -LiteralPath '!PKG_ROOT!\SystemAppData\wgs' -Recurse -File -ErrorAction SilentlyContinue ^| Select-String -Pattern 'StaticHttpRegionInfo, Assembly-CSharp' -SimpleMatch -List ^| Select-Object -ExpandProperty Path ^| Get-Item ^| Sort-Object LastWriteTime -Descending ^| Select-Object -First 1 -ExpandProperty FullName"') do set "TARGET_PATH=%%F"
if not defined TARGET_PATH (
    echo Could not automatically locate the regionInfo container.
    echo Please use the manual installation instructions on the download page.
    pause
    exit /b 1
)
goto download

:desktop
set "TARGET_PATH=%APPDATA%\..\LocalLow\Innersloth\Among Us\regionInfo.json"

:download
echo.
echo Target file: "!TARGET_PATH!"
echo.

curl.exe --fail --location --create-dirs --output "!TEMP_PATH!" --url "%REGION_URL%"
if errorlevel 1 (
    echo Download failed. Check your internet connection and try again.
    del /q "!TEMP_PATH!" >nul 2>nul
    pause
    exit /b 1
)

powershell.exe -NoProfile -Command "Get-Content -Raw -LiteralPath '!TEMP_PATH!' | ConvertFrom-Json | Out-Null"
if errorlevel 1 (
    echo The downloaded region file is not valid JSON. Your existing file was not changed.
    del /q "!TEMP_PATH!" >nul 2>nul
    pause
    exit /b 1
)

if exist "!TARGET_PATH!" (
    copy /y "!TARGET_PATH!" "!BACKUP_PATH!" >nul
    if errorlevel 1 (
        echo Could not back up the existing region file. Installation stopped.
        del /q "!TEMP_PATH!" >nul 2>nul
        pause
        exit /b 1
    )
    echo Existing region data backed up to "!BACKUP_PATH!"
)

for %%D in ("!TARGET_PATH!") do if not exist "%%~dpD" mkdir "%%~dpD"
move /y "!TEMP_PATH!" "!TARGET_PATH!" >nul
if errorlevel 1 (
    echo Could not replace the region file.
    pause
    exit /b 1
)

echo Installation complete. Open Among Us and select a custom region.
pause
exit /b 0
