@echo off
REM Aetherbound installer for Windows (TLauncher / official launcher).
REM Double-click to run. Installs Forge 1.20.1 + a local Java 17 JRE.

setlocal enabledelayedexpansion
title Aetherbound installer

set "BASE_URL=https://pack.riegergeri.com"
set "ZIP_URL=%BASE_URL%/aetherbound-client-1.20.1.zip"
set "SERVERS_DAT_URL=%BASE_URL%/servers.dat"
set "FORGE_INSTALLER_URL=%BASE_URL%/forge-1.20.1-47.4.9-installer.jar"
set "FORGE_VERSION=1.20.1-forge-47.4.9"

cls
echo =============================================
echo          Aetherbound installer (Windows)
echo =============================================
echo.

REM 1. Pick .minecraft.
set "MC="
if exist "%APPDATA%\.minecraft" set "MC=%APPDATA%\.minecraft"
if not defined MC if exist "%APPDATA%\minecraft" set "MC=%APPDATA%\minecraft"
if not defined MC (
  set "MC=%APPDATA%\.minecraft"
  mkdir "!MC!" 2>nul
)
echo Game folder: !MC!
echo.

REM 2a. Clean any partial Forge install / locked natives dir from a previous
REM     failed launch. TLauncher errors out if a lockfile is left behind.
if exist "!MC!\versions\1.20.1-forge-47.4.9\natives" (
  echo Cleaning stale natives dir...
  rmdir /S /Q "!MC!\versions\1.20.1-forge-47.4.9\natives"
)
REM Strip read-only attribute on anything that has it.
attrib -R "!MC!\*" /S /D >nul 2>&1

REM 2. Ensure a Java 17 JRE lives inside .minecraft. TLauncher's bundled Java
REM    21 crashes Forge 1.20.1 (Sinytra Connector reflects on classes that no
REM    longer exist in newer ModLauncher).
set "JDK_HOME=!MC!\aetherbound-jdk17"
set "JAVA17=!JDK_HOME!\bin\java.exe"
if not exist "!JAVA17!" (
  echo Downloading Java 17 JRE ^(~50 MB^)...
  set "JRE_URL=https://api.adoptium.net/v3/binary/latest/17/ga/windows/x64/jre/hotspot/normal/eclipse"
  set "TMP_JRE=%TEMP%\jre17.zip"
  powershell -NoProfile -Command "$ProgressPreference='SilentlyContinue'; Invoke-WebRequest -Uri '!JRE_URL!' -OutFile '!TMP_JRE!' -UseBasicParsing"
  if errorlevel 1 ( echo Java download failed. & pause & exit /b 1 )
  if exist "!JDK_HOME!" rmdir /S /Q "!JDK_HOME!"
  mkdir "!JDK_HOME!"
  powershell -NoProfile -Command "Expand-Archive -LiteralPath '!TMP_JRE!' -DestinationPath '!JDK_HOME!\_x' -Force"
  REM Adoptium ships zip with a single top-level dir; flatten it.
  for /d %%D in ("!JDK_HOME!\_x\*") do (
    xcopy /E /I /Y "%%D\*" "!JDK_HOME!\" >nul
  )
  rmdir /S /Q "!JDK_HOME!\_x"
  del "!TMP_JRE!" >nul 2>&1
  if not exist "!JAVA17!" (
    echo Java 17 extracted but binary missing: !JAVA17!
    pause
    exit /b 1
  )
)
echo Java 17: !JAVA17!
"!JAVA17!" -version
echo.

REM 3. Mods.
if exist "!MC!\mods" (
  dir /b "!MC!\mods\*.jar" >nul 2>&1
  if not errorlevel 1 (
    for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd-HHmmss"') do set "STAMP=%%i"
    echo Existing mods\ found — moved to mods-backup-!STAMP!\
    ren "!MC!\mods" "mods-backup-!STAMP!"
  )
)
mkdir "!MC!\mods" 2>nul

set "TMP_ZIP=%TEMP%\aetherbound-pack.zip"
set "TMP_DIR=%TEMP%\aetherbound-extract"
if exist "%TMP_DIR%" rmdir /S /Q "%TMP_DIR%"
mkdir "%TMP_DIR%"

echo Downloading mods ^(about 530 MB^)... this can take a few minutes.
powershell -NoProfile -Command "$ProgressPreference='SilentlyContinue'; try { Invoke-WebRequest -Uri '%ZIP_URL%' -OutFile '%TMP_ZIP%' -UseBasicParsing } catch { Write-Host $_; exit 1 }"
if errorlevel 1 ( echo Download failed. & pause & exit /b 1 )

echo Unpacking...
powershell -NoProfile -Command "Expand-Archive -LiteralPath '%TMP_ZIP%' -DestinationPath '%TMP_DIR%' -Force"

if exist "%TMP_DIR%\mods" (
  copy /Y "%TMP_DIR%\mods\*.jar" "!MC!\mods\" >nul
) else (
  for /R "%TMP_DIR%" %%f in (*.jar) do copy /Y "%%f" "!MC!\mods\" >nul
)
for /f %%n in ('dir /b "!MC!\mods\*.jar" 2^>nul ^| find /v /c ""') do set JAR_COUNT=%%n
echo Installed !JAR_COUNT! mod files.
echo.

REM 4. Forge client install.
if not exist "!MC!\versions\%FORGE_VERSION%" (
  echo Installing Forge %FORGE_VERSION% client...
  powershell -NoProfile -Command "$ProgressPreference='SilentlyContinue'; Invoke-WebRequest -Uri '%FORGE_INSTALLER_URL%' -OutFile '%TMP_DIR%\forge.jar' -UseBasicParsing"
  if not exist "!MC!\launcher_profiles.json" (
    > "!MC!\launcher_profiles.json" echo {"profiles":{},"settings":{},"version":3}
  )
  "!JAVA17!" -jar "%TMP_DIR%\forge.jar" --installClient "!MC!"
)

REM 5. launcher_profiles.json — Aetherbound profile pinning Forge + Java 17.
set "PROFILES=!MC!\launcher_profiles.json"
if not exist "!PROFILES!" (
  > "!PROFILES!" echo {"profiles":{},"settings":{},"version":3}
)
set "PROFILES_PS=%PROFILES:\=\\%"
set "JAVA17_PS=%JAVA17:\=\\%"
set "MC_PS=%MC:\=\\%"
powershell -NoProfile -Command "$p='%PROFILES_PS%'; $d=Get-Content -Raw $p | ConvertFrom-Json; if (-not $d.profiles) { $d | Add-Member -Force profiles ([pscustomobject]@{}) }; $d.profiles | Add-Member -Force aetherbound ([pscustomobject]@{ name='Aetherbound (Forge 1.20.1)'; type='custom'; lastVersionId='%FORGE_VERSION%'; javaDir='%JAVA17_PS%'; javaArgs='-Xmx6G -Xms2G -XX:+UseG1GC'; gameDir='%MC_PS%'; icon='Furnace' }); $d | ConvertTo-Json -Depth 8 | Set-Content $p"
echo Profile 'Aetherbound (Forge 1.20.1)' written.

REM 6. Server list. Non-fatal — don't abort the install if a leftover .bak
REM    refuses to overwrite.
if exist "!MC!\servers.dat" (
  if exist "!MC!\servers.dat.bak" attrib -R "!MC!\servers.dat.bak" >nul 2>&1
  if exist "!MC!\servers.dat.bak" del /F /Q "!MC!\servers.dat.bak" >nul 2>&1
  copy /Y "!MC!\servers.dat" "!MC!\servers.dat.bak" >nul 2>&1
)
echo Adding server: mc.riegergeri.com
powershell -NoProfile -Command "$ProgressPreference='SilentlyContinue'; try { Invoke-WebRequest -Uri '%SERVERS_DAT_URL%' -OutFile '%MC%\servers.dat' -UseBasicParsing } catch { Write-Host '(could not download server list — add mc.riegergeri.com manually in-game)' }"

rmdir /S /Q "%TMP_DIR%" >nul 2>&1
del "%TMP_ZIP%" >nul 2>&1

echo.
echo =============================================
echo   Done!
echo =============================================
echo TLauncher (cracked-friendly):
echo   1. Open TLauncher. Fully quit it first if it was running.
echo   2. In the version dropdown pick:  Forge 1.20.1-47.4.9
echo      ^(TLauncher does NOT show the Aetherbound profile — it has its own
echo       profile system. The Forge version is what matters.^)
echo   3. Settings -^> Java -^> Custom path:
echo        !JAVA17!
echo   4. Click Play.
echo.
echo Official Mojang launcher:
echo   Pick the profile 'Aetherbound (Forge 1.20.1)' and hit Play.
echo.
echo Voice chat: ts.riegergeri.com  (TeamSpeak 3)
echo.
pause
