@echo off
setlocal EnableExtensions
title Titanium World - Updater Repair

echo.
echo  Titanium World updater repair
echo  ----------------------------
echo  Downloads a FRESH data.zip from the website into AppData.
echo  Keeps: bot, hotkeys, settings, minimap, characterdata
echo.
echo  1) Close Titanium World completely.
echo  2) Press any key here to start the download.
echo.
pause >nul

set "APPDIR=%APPDATA%\OTClientV8\TitaniumWorld"
set "TMPDIR=%TEMP%\tw-updater-repair"
set "TMPZIP=%TMPDIR%\TitaniumClient.zip"
set "URL=https://retrodus.net/titanium-world/downloads/TitaniumClient.zip"

if not exist "%APPDIR%" mkdir "%APPDIR%" >nul 2>&1

echo.
echo  Target: %APPDIR%
echo  Source: %URL%
echo.

if exist "%APPDIR%\data.zip" (
  for /f %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd-HHmmss"') do set "TS=%%I"
  move /y "%APPDIR%\data.zip" "%APPDIR%\data.zip.bak-repair-%TS%" >nul 2>&1
  echo  [OK] backed up old AppData data.zip
)

for %%D in (modules mods layouts) do (
  if exist "%APPDIR%\%%D\" (
    rmdir /s /q "%APPDIR%\%%D"
    echo  [OK] removed loose %%D\
  )
)
if exist "%APPDIR%\init.lua" del /f /q "%APPDIR%\init.lua" >nul 2>&1

echo  [..] downloading client zip (about 60 MB). Please wait...
if exist "%TMPDIR%" rmdir /s /q "%TMPDIR%" >nul 2>&1
mkdir "%TMPDIR%" >nul 2>&1

REM Silent download — no PowerShell progress spam
curl.exe -L --fail --retry 3 --retry-delay 2 -o "%TMPZIP%" "%URL%" 2>nul
if errorlevel 1 (
  powershell -NoProfile -ExecutionPolicy Bypass -Command ^
    "$ProgressPreference='SilentlyContinue'; try { Invoke-WebRequest -Uri '%URL%' -OutFile '%TMPZIP%' -UseBasicParsing } catch { Write-Host $_.Exception.Message; exit 1 }"
)
if errorlevel 1 (
  echo  [FAIL] download failed.
  echo  Open this URL in a browser and try again:
  echo  %URL%
  echo.
  pause
  exit /b 1
)
if not exist "%TMPZIP%" (
  echo  [FAIL] download file missing.
  pause
  exit /b 1
)
echo  [OK] download finished

echo  [..] extracting data.zip...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
  "$ProgressPreference='SilentlyContinue'; Expand-Archive -LiteralPath '%TMPZIP%' -DestinationPath '%TMPDIR%\extract' -Force; $d = Get-ChildItem -Path '%TMPDIR%\extract' -Recurse -Filter data.zip | Select-Object -First 1; if (-not $d) { Write-Host 'no data.zip inside client zip'; exit 2 }; Copy-Item -LiteralPath $d.FullName -Destination '%APPDIR%\data.zip' -Force"
if errorlevel 1 (
  echo  [FAIL] could not extract data.zip
  pause
  exit /b 1
)

if not exist "%APPDIR%\data.zip" (
  echo  [FAIL] AppData data.zip still missing.
  pause
  exit /b 1
)
echo  [OK] installed fresh data.zip

if exist "%APPDIR%\config.otml" (
  powershell -NoProfile -ExecutionPolicy Bypass -Command ^
    "$ProgressPreference='SilentlyContinue'; $p = Join-Path $env:APPDATA 'OTClientV8\TitaniumWorld\config.otml'; $c = Get-Content -LiteralPath $p -Raw; $n = [regex]::Replace($c, '(?m)^\s*tw-updater-rev:.*\r?\n', ''); $n = [regex]::Replace($n, '(?m)^\s*tw-updater-restart-rev:.*\r?\n', ''); if ($n -ne $c) { Set-Content -LiteralPath $p -Value $n -NoNewline; Write-Host '  [OK] cleared updater rev settings' } else { Write-Host '  [OK] updater rev already clear' }"
)

echo.
echo  Done. Start Titanium World again.
echo.
pause
endlocal
