@echo off
setlocal EnableExtensions

set "VERSION=2.1.2"
set "SOURCE=%~dp0payload"
set "ROOT=%LOCALAPPDATA%\MineDayZ"
set "JAVA=%ROOT%\java64\bin\javaw.exe"
set "JAR=%ROOT%\launcher\build\MDZLauncher.jar"
set "MARKER=%ROOT%\launcher-version.txt"

if not exist "%ROOT%" mkdir "%ROOT%"

set "NEED_INSTALL=0"
if not exist "%JAVA%" set "NEED_INSTALL=1"
if not exist "%JAR%" set "NEED_INSTALL=1"
if not exist "%MARKER%" set "NEED_INSTALL=1"

if exist "%MARKER%" (
    set /p INSTALLED_VERSION=<"%MARKER%"
    if /I not "%INSTALLED_VERSION%"=="%VERSION%" set "NEED_INSTALL=1"
)

if "%NEED_INSTALL%"=="1" (
    echo Installing MineDayZ Launcher %VERSION%...
    robocopy "%SOURCE%" "%ROOT%" /E /R:2 /W:1 /NFL /NDL /NJH /NJS /NP >nul
    if errorlevel 8 (
        echo Installation failed.
        echo Try extracting the ZIP again and run MineDayZ.cmd as a normal user.
        pause
        exit /b 1
    )
    >"%MARKER%" echo %VERSION%
)

if not exist "%JAVA%" (
    echo javaw.exe was not found after installation.
    pause
    exit /b 1
)

if not exist "%JAR%" (
    echo MDZLauncher.jar was not found after installation.
    pause
    exit /b 1
)

start "" "%JAVA%" "-Dhttps.protocols=TLSv1.2" "-Dmdz.home=%ROOT%" -jar "%JAR%"
exit /b 0
