@echo off
REM rlite Windows launcher.
REM 1. If the SelfUpdater downloaded a new build last run, a *.jar.new file
REM    sits next to the JAR. Swap it in before the JVM opens the JAR for read.
REM 2. Launch the JAR with java -jar.
REM
REM Place this .bat next to the JAR. Double-click to run, or:
REM     launch.bat --debug --safe-mode    (extra args pass through)
REM
REM Requires: Java 11+ on PATH (`brew install openjdk@11` is mac equivalent).

setlocal enabledelayedexpansion

cd /d "%~dp0"

REM --- Apply any staged update (every *.jar.new -> *.jar) ---
for %%F in (*.jar.new) do (
    set "TARGET=%%~nF"
    if exist "!TARGET!" (
        echo Applying update from %%F to !TARGET! ...
        move /Y "%%F" "!TARGET!" >nul
    )
)

REM --- Find the JAR to launch (first *.jar in folder) ---
set "JAR="
for %%F in (*.jar) do (
    if "!JAR!"=="" set "JAR=%%F"
)

if "%JAR%"=="" (
    echo No .jar file found in %CD%
    pause
    exit /b 1
)

REM --- Verify Java is available ---
where java >nul 2>nul
if errorlevel 1 (
    echo ERROR: java not found on PATH. Install Java 11 or later.
    echo Suggested: https://adoptium.net/temurin/releases/?version=11
    pause
    exit /b 1
)

echo Launching %JAR% ...
java -jar "%JAR%" %*

if errorlevel 1 pause
