@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

NET SESSION >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    ECHO.
    ECHO [ERROR] Please run this script as Administrator.
    ECHO Press Enter to exit...
    PAUSE >nul
    EXIT /B
)

REM ======= CONFIGURATION =======
SET PATH=%PATH%;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
SET ABS_WEBSERVERHOME=%1
SET ABS_WEBSERVERHOME=%ABS_WEBSERVERHOME:~0,-1%


REM ======= DETECT VERSION =======
SET "INI_FILE=%ABS_WEBSERVERHOME%\ABSSolute_version.ini"

IF NOT EXIST "%INI_FILE%" (
    ECHO ERROR: Version file not found: %INI_FILE%
    GOTO End
)

FOR /F "tokens=4" %%A IN ('findstr /C:"ABSSOLUTE VERSION IS:" "%INI_FILE%"') DO (
    SET "ABS_VERSION=%%A"
)

IF NOT DEFINED ABS_VERSION (
    ECHO ERROR: Could not detect ABSOLUTE VERSION from INI file.
    GOTO End
)

ECHO Detected ABSOLUTE VERSION: %ABS_VERSION%

FOR /F "tokens=1-3 delims=." %%A IN ("%ABS_VERSION%") DO (
    SET /A VER_MAJOR=%%A
    SET /A VER_MINOR=1%%B - 100
    SET /A VER_PATCH=1%%C - 100
)
SET /A VER_NUMBER=%VER_MAJOR%*10000 + %VER_MINOR%*100 + %VER_PATCH%

REM ======= DECIDE JAVA VERSION =======
SET "JAVA_VER=17"
IF %VER_NUMBER% LSS 90800 SET "JAVA_VER=11"
ECHO Using JAVA version: %JAVA_VER%

REM ======= JAVA CONFIGURATION =======
SET "JAVA_ZIP_URL=https://download.abssolute.net/Tools/Java/%JAVA_VER%/Zulu_%JAVA_VER%.zip"
SET "LOCAL_JAVA_ZIP=%ABS_WEBSERVERHOME%\custom\static_zips\Zulu_%JAVA_VER%.zip"
SET "TOOLS_DIR=%ABS_WEBSERVERHOME%\tools"
SET "EXTRACT_DIR=%ABS_WEBSERVERHOME%\bin"
SET "TMP_EXTRACT=%TOOLS_DIR%\tmp_extract"
SET "TEMP_ZIP=%TOOLS_DIR%\Zulu_%JAVA_VER%.zip"

REM ======= CLEAN OLD FILES =======
ECHO.
ECHO [...] Cleaning old JRE...

REM Clean bin except jdk or jre
if exist "%ABS_WEBSERVERHOME%\bin" (
    if exist "%ABS_WEBSERVERHOME%\bin\jdk" (
        rmdir /s /q "%ABS_WEBSERVERHOME%\bin\jdk"
    )
    if exist "%ABS_WEBSERVERHOME%\bin\jre" (
        rmdir /s /q "%ABS_WEBSERVERHOME%\bin\jre"
    )
)

REM ======= DOWNLOAD LATEST JAVA =======
ECHO.
ECHO [...] Downloading latest JAVA package...

if not exist "%TOOLS_DIR%" mkdir "%TOOLS_DIR%"

IF NOT EXIST "%TEMP_ZIP%" (
    REM Fallback to PowerShell BITS
    powershell -Command "Start-BitsTransfer -Source '%JAVA_ZIP_URL%' -Destination '%TEMP_ZIP%' -ErrorAction SilentlyContinue" >nul 2>&1
)

IF EXIST "%TEMP_ZIP%" (
    ECHO Download successful: %TEMP_ZIP%
) ELSE (
    ECHO Download failed, using local backup...
    if exist "%LOCAL_JAVA_ZIP%" (
        copy /y "%LOCAL_JAVA_ZIP%" "%TEMP_ZIP%" >nul
    ) ELSE (
        ECHO ERROR: No JAVA zip available. Update aborted.
        GOTO End
    )
)


REM ======= EXTRACT NEW JAVA =======
ECHO.
ECHO [...] Extracting JAVA files...
if exist "%TMP_EXTRACT%" rmdir /s /q "%TMP_EXTRACT%"
mkdir "%TMP_EXTRACT%"

powershell -NoProfile -Command "Expand-Archive -LiteralPath '%TEMP_ZIP%' -DestinationPath '%TMP_EXTRACT%' -Force" >NUL 2>&1

REM FIND THE EXTRACTED FOLDER (JDK/JRE USUALLY HAS ONE TOP-LEVEL FOLDER)
SET "SRC_DIR="
FOR /D %%D IN ("%TMP_EXTRACT%\*") DO (
    SET "SRC_DIR=%%~fD"
)

IF NOT DEFINED SRC_DIR (
    ECHO ERROR: NO DIRECTORY FOUND INSIDE ZIP.
)

REM REMOVE OLD JRE IF EXISTS
MOVE "%SRC_DIR%" "%ABS_WEBSERVERHOME%\bin\jre" >NUL

REM ======= DISPLAY VERSION =======
SET "JAVA_VERSION="
SET "IMPLEMENTOR_VERSION="

IF EXIST "%ABS_WEBSERVERHOME%\bin\jre\release" (
    REM === PARSE JAVA_VERSION
    FOR /F "tokens=2 delims==" %%A IN ('findstr /B "JAVA_VERSION=" "%ABS_WEBSERVERHOME%\bin\jre\release"') DO (
        SET "JAVA_VERSION=%%~A"
    )

    REM === PARSE IMPLEMENTOR_VERSION
    FOR /F "tokens=2 delims==" %%A IN ('findstr /B "IMPLEMENTOR_VERSION=" "%ABS_WEBSERVERHOME%\bin\jre\release"') DO (
        SET "IMPLEMENTOR_VERSION=%%~A"
    )

    IF DEFINED JAVA_VERSION (
        REM STRIP SURROUNDING QUOTES
        SET "JAVA_VERSION=!JAVA_VERSION:"=!"
        SET "IMPLEMENTOR_VERSION=!IMPLEMENTOR_VERSION:"=!"

        ECHO JAVA VERSION IS: !JAVA_VERSION!
        ECHO IMPLEMENTOR VERSION IS: !IMPLEMENTOR_VERSION!
    ) ELSE (
        ECHO COULD NOT FIND JAVA_VERSION IN RELEASE FILE.
    )
) ELSE (
    ECHO RELEASE FILE NOT FOUND.
)

REM Cleanup temp extraction & zip
ECHO.
ECHO [...] Clean up...
rmdir /s /q "%TMP_EXTRACT%"
del /f /q "%TEMP_ZIP%"

:End
ECHO.
ECHO Install process completed....New JAVA version Installed: %JAVA_VERSION% - %IMPLEMENTOR_VERSION%...
ENDLOCAL