@echo off
setlocal
title L2 Auron - installer
cd /d "%~dp0"

rem  Todo lo que pasa aca adentro queda escrito en un archivo. Si algo falla,
rem  el jugador nos manda ese archivo y sabemos exactamente donde se rompio.
rem  Antes esto fallaba sin dejar rastro: la ventana se cerraba y listo.
rem
rem  Los carteles estan en ingles a proposito: el server habla castellano,
rem  ingles y portugues, y la consola de Windows no es lugar para traducir.

set "LOG=%TEMP%\l2_instalar_log.txt"
set "PS1=%TEMP%\l2_instalar.ps1"
set "PSEXE=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"

echo =============================================== > "%LOG%"
echo L2 Auron - installer                           >> "%LOG%"
echo date: %DATE% %TIME%                            >> "%LOG%"
echo =============================================== >> "%LOG%"

echo.
echo   ==========================================
echo         L 2   A U R O N
echo   ==========================================
echo.
echo   Starting the installer...
echo.

if not exist "%PSEXE%" goto :sinpowershell
if exist "%PS1%" del /f /q "%PS1%" >nul 2>&1
if not exist "%SystemRoot%\System32\curl.exe" goto :connet

curl.exe -s -S -f -L --connect-timeout 15 --max-time 120 -o "%PS1%" http://147.93.71.106/launcher/instalar.ps1 >> "%LOG%" 2>&1
set "CODIGO=%ERRORLEVEL%"
echo [curl] exit code %CODIGO% >> "%LOG%"
if "%CODIGO%"=="0" if exist "%PS1%" goto :correr

rem  18/10/2026: en algunas PCs Windows no deja correr curl.exe ("Acceso
rem  denegado", codigo 5). Se prueba con PowerShell antes de rendirse.
:connet
echo [powershell] downloading instalar.ps1 >> "%LOG%"
"%PSEXE%" -NoProfile -ExecutionPolicy Bypass -Command "try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch { }; (New-Object Net.WebClient).DownloadFile('http://147.93.71.106/launcher/instalar.ps1', $env:TEMP + '\l2_instalar.ps1')" >> "%LOG%" 2>&1
echo [powershell] download exit code %ERRORLEVEL% >> "%LOG%"
if not exist "%PS1%" goto :sinred

:correr

echo [powershell] starting >> "%LOG%"
"%PSEXE%" -NoProfile -ExecutionPolicy Bypass -File "%PS1%"
set "CODIGO=%ERRORLEVEL%"
echo [powershell] exit code %CODIGO% >> "%LOG%"

if not "%CODIGO%"=="0" goto :fallo
goto :fin


:sinred
echo   Could not reach the server.
echo.
echo   Check your internet connection, or your antivirus
echo   may be blocking it.
echo [error] curl could not download instalar.ps1 >> "%LOG%"
goto :ayuda

:sinpowershell
echo   PowerShell is missing from this Windows.
echo   This happens on stripped-down or pirated copies.
echo [error] not found: %PSEXE% >> "%LOG%"
goto :ayuda

:fallo
echo.
echo   The installer stopped with an error (code %CODIGO%).
echo.
echo   Most of the time it is the antivirus. Try turning it
echo   off for a moment and running this again.
goto :ayuda

:ayuda
echo.
echo   ------------------------------------------------
echo   Send THIS file to the server staff:
echo.
echo      %LOG%
echo.
echo   Copy that path, paste it into Windows Explorer
echo   and you will find the file. It says what failed.
echo   ------------------------------------------------

:fin
echo.
pause
