@echo off
setlocal enableextensions enabledelayedexpansion
title Ultima Online.AI - One-Click Installer

echo.
echo ================================================================
echo  Ultima Online.AI  ^|  One-Click Installer
echo  Britannia, restored - and remembering
echo ================================================================
echo.

REM === Locate UO data ===
set UODATA=
if exist "C:\Program Files\UO Renaissance\map0.mul" set UODATA=C:\Program Files\UO Renaissance
if exist "C:\Program Files (x86)\UO Renaissance\map0.mul" set UODATA=C:\Program Files (x86)\UO Renaissance
if exist "C:\Program Files\Electronic Arts\Ultima Online Classic\map0.mul" set UODATA=C:\Program Files\Electronic Arts\Ultima Online Classic
if exist "C:\Program Files (x86)\Electronic Arts\Ultima Online Classic\map0.mul" set UODATA=C:\Program Files (x86)\Electronic Arts\Ultima Online Classic
if exist "C:\Program Files (x86)\EA Games\Ultima Online Renaissance\map0.mul" set UODATA=C:\Program Files (x86)\EA Games\Ultima Online Renaissance

if not defined UODATA (
  echo  [!] UO data folder not found in any default location.
  echo.
  echo  You need a UO Renaissance install. Download here:
  echo    http://www.uorenaissance.com/downloads/UO_Renaissance_Client_Full.exe
  echo.
  echo  Run that installer first ^(accept default location^), then re-run this file.
  echo.
  pause
  exit /b 1
)

echo  [+] UO data found:  %UODATA%
echo.

REM === Set install dir ===
set INSTDIR=%USERPROFILE%\ClassicUO
if not exist "%INSTDIR%" mkdir "%INSTDIR%"
echo  [+] Install dir:    %INSTDIR%
echo.

REM === Download ClassicUO ===
echo  [*] Downloading ClassicUO ^(~42 MB^)...
powershell -Command "Invoke-WebRequest 'https://github.com/ClassicUO/ClassicUO/releases/download/ClassicUO-main-release/ClassicUO-win-x64-release.zip' -OutFile '%TEMP%\cuo.zip'"
if errorlevel 1 (
  echo  [!] Download failed.
  pause
  exit /b 1
)

echo  [*] Extracting...
powershell -Command "Expand-Archive -Force '%TEMP%\cuo.zip' '%INSTDIR%'"
if errorlevel 1 (
  echo  [!] Extract failed.
  pause
  exit /b 1
)

REM === Write settings.json ===
set UODATAESC=%UODATA:\=\\%
echo  [*] Writing settings.json...
(
  echo {
  echo   "ip": "192.168.1.135",
  echo   "port": 2593,
  echo   "ultimaonlinedirectory": "%UODATAESC%",
  echo   "clientversion": "2.0.7",
  echo   "ignore_relay_ip": true,
  echo   "encryption": 0,
  echo   "username": "",
  echo   "password": "",
  echo   "lang": "ENU",
  echo   "fps": 60,
  echo   "screen_scale": 1,
  echo   "is_win_maximized": true,
  echo   "saveaccount": false,
  echo   "autologin": false,
  echo   "reconnect": false,
  echo   "reconnect_time": 1,
  echo   "login_music": true,
  echo   "login_music_volume": 70,
  echo   "fixed_time_step": true,
  echo   "run_mouse_in_separate_thread": true,
  echo   "force_driver": 0,
  echo   "use_verdata": false,
  echo   "encryption_type": 0,
  echo   "plugins": []
  echo }
) > "%INSTDIR%\settings.json"

REM === Optional desktop shortcut ===
set SHORTCUT=%USERPROFILE%\Desktop\Ultima Online.AI.lnk
echo  [*] Creating desktop shortcut...
powershell -Command "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%SHORTCUT%'); $s.TargetPath='%INSTDIR%\ClassicUO.exe'; $s.WorkingDirectory='%INSTDIR%'; $s.IconLocation='%INSTDIR%\ClassicUO.exe'; $s.Save()"

echo.
echo ================================================================
echo  Done. Launching Ultima Online.AI...
echo ================================================================
echo.
echo  Server:  100.103.182.80:2593  ^(via Tailscale^)
echo  At login screen: invent any account name + password.
echo  The shard creates the account automatically on first login.
echo.

start "" "%INSTDIR%\ClassicUO.exe"
timeout /t 4 /nobreak >nul
exit /b 0
