@echo off

rem #  ------------------------------------------------------------
rem #  Company: Software- und Organisations-Service GmbH
rem #  Date:    2025-12-11
rem #  Purpose: Download Graal Polyglot Libraries using Maven Wrapper
rem #  ------------------------------------------------------------

SETLOCAL

set AGENT_HOME=
set ARCHIVE_PATH=
set GRAALVM_VERSION=latest
set MAVEN_VERSION=3.9.11
set MAVEN_WRAPPER_VERSION=latest
set TEMP_DIR=%TMP%
set TEMP_MAVEN_HOME=
set TAR_DIR=
set MAVEN_TERMINATE_CMD=on


rem ##########################
rem # read command line options
if "%~1" == "" call :usage & exit /b 1
:read_command_line_options

if x-h == x%1 call :usage & exit /b 0
if x--help == x%1 call :usage & exit /b 0
if x--graalvm-version == x%1 set GRAALVM_VERSION=%~2& shift
if x--maven-wrapper-version == x%1 set MAVEN_WRAPPER_VERSION=%~2& shift
if x--archive == x%1 set ARCHIVE_PATH=%~2& shift
if x--agent-home == x%1 set AGENT_HOME=%~2& shift
if x--temp-dir == x%1 set TEMP_DIR=%~2& shift
if x--java-home == x%1 set JAVA_HOME=%~2& shift

shift
if "%~1" == "" goto read_command_line_options_end
goto read_command_line_options
:read_command_line_options_end


rem ##########################
rem validation
if defined ARCHIVE_PATH (
    if defined AGENT_HOME (
        echo. >&2
        echo one of the options --archive or --agent-home must be specified >&2
        echo. >&2
        call :usage & exit /b 1
    )
    call :normalize_archivepath "%ARCHIVE_PATH%"
) else (
    if not defined AGENT_HOME (
        echo. >&2
        echo one of the options --archive or --agent-home must be specified >&2
        echo. >&2
        call :usage & exit /b 1
    )
)

if defined AGENT_HOME (
    if not exist "%AGENT_HOME%\lib\user_lib" (
        echo. >&2
        echo could not find Agent directory: "%AGENT_HOME%\lib\user_lib" >&2
        echo. >&2
        call :usage & exit /b 1
    )
)

if defined JAVA_HOME (
    if not exist "%JAVA_HOME%" (
        echo. >&2
        echo could not find Java Home directory: --java-home="%JAVA_HOME%" >&2
        echo. >&2
        call :usage & exit /b 1
    )
    if not exist "%JAVA_HOME%\bin\java.exe" (
        echo. >&2
        echo Java binary .\bin\java.exe not found in Java Home directory: --java-home="%JAVA_HOME%" >&2
        echo. >&2
        call :usage & exit /b 1
    )
) else (
    echo. >&2
    echo specify Java Home directory: --java-home= >&2
    echo. >&2
    call :usage & exit /b 1
)

if xlatest == x%GRAALVM_VERSION% call :set_graalvmversion
if not defined GRAALVM_VERSION call :set_graalvmversion
if not defined GRAALVM_VERSION (
    echo. >&2
    echo could not identify latest GraalVM version, use --graalvm-version=^<identifier^> >&2
    echo. >&2
    call :usage & exit /b 2
)

if xlatest == x%MAVEN_WRAPPER_VERSION% call :set_mavenwrapperversion
if not defined MAVEN_WRAPPER_VERSION call :set_mavenwrapperversion
if not defined MAVEN_WRAPPER_VERSION (
    echo. >&2
    echo could not identify latest Maven wrapper version, use --maven-wrapper-version=^<identifier^> >&2
    echo. >&2
    call :usage & exit /b 2
)


rem ##########################
rem # ...and action
set TEMP_MAVEN_HOME=%TEMP_DIR%\mvnw.%RANDOM%

if exist "%TEMP_MAVEN_HOME%" rmdir /s /q "%TEMP_MAVEN_HOME%"
if not exist "%TEMP_MAVEN_HOME%" mkdir "%TEMP_MAVEN_HOME%"
set MAVEN_WRAPPER_TARBALL=maven-wrapper-distribution-%MAVEN_WRAPPER_VERSION%-bin.zip
set CUR_DIR=%CD%

set TAR_DIR=%TEMP_DIR%\download-graal-libs-%RANDOM%
if exist "%TAR_DIR%" rmdir /s /q "%TAR_DIR%"
if not exist "%TAR_DIR%" mkdir "%TAR_DIR%"

if exist "%TEMP_MAVEN_HOME%" (
    cd /d "%TEMP_MAVEN_HOME%"
    echo .. downloading "https://repo1.maven.org/maven2/org/apache/maven/wrapper/maven-wrapper-distribution/%MAVEN_WRAPPER_VERSION%/%MAVEN_WRAPPER_TARBALL%" >&2
    curl -k -L -s -S https://repo1.maven.org/maven2/org/apache/maven/wrapper/maven-wrapper-distribution/%MAVEN_WRAPPER_VERSION%/%MAVEN_WRAPPER_TARBALL% -O
    echo .. extracting "%TEMP_MAVEN_HOME%\%MAVEN_WRAPPER_TARBALL%" >&2
    tar -xf "%MAVEN_WRAPPER_TARBALL%"
    
    if not exist "settings.xml" (
        echo .. writing Maven settings file: "%TEMP_MAVEN_HOME%\settings.xml" >&2
        call :make_maven_settings "settings.xml"
    )
    
    if not exist ".mvn\wrapper\maven-wrapper.properties" (
        echo .. creating Maven wrapper directory: "%TEMP_MAVEN_HOME%\.mvn\wrapper" >&2
        if not exist ".mvn\wrapper" mkdir ".mvn\wrapper"
        echo .. writing Maven properties file: "%TEMP_MAVEN_HOME%\.mvn\wrapper\maven-wrapper.properties" >&2
        call :make_maven_properties ".mvn\wrapper\maven-wrapper.properties"
    )
    
    if not exist "pom.xml" call :make_maven_pom "pom.xml"
    
    echo .. downloading graal libs in "%TAR_DIR%" >&2
    start "download-graal-libs" /D "%TEMP_MAVEN_HOME%" /B /WAIT mvnw.cmd dependency:copy-dependencies ^
        -s "%TEMP_MAVEN_HOME%\settings.xml" ^
        -gs "%TEMP_MAVEN_HOME%\settings.xml" ^
        -DoutputDirectory="%TAR_DIR%" ^
        -DincludeTypes=jar ^
        -Doverwrite=true
    
    cd /d "%CUR_DIR%"
)

dir /b "%TAR_DIR%" > "%TAR_DIR%\.graal-libs"

if defined AGENT_HOME (
    if exist "%AGENT_HOME%\lib\user_lib\.graal-libs" (
        for /f "usebackq" %%i in ("%AGENT_HOME%\lib\user_lib\.graal-libs") do del /f /q "%AGENT_HOME%\lib\user_lib\%%i"
    )
    echo .. copying graal libs to "%AGENT_HOME%\lib\user_lib" >&2
    xcopy /e /k /h /i /q "%TAR_DIR%" "%AGENT_HOME%\lib\user_lib" >&2
)

if defined ARCHIVE_PATH (
    echo .. creating graal libs tarball "%ARCHIVE_PATH%.tar.gz" >&2
    tar -C "%TAR_DIR%" -czf "%ARCHIVE_PATH%.tar.gz" * >&2
)

rem cleanup temp. files
if defined TEMP_MAVEN_HOME (
    if exist "%TEMP_MAVEN_HOME%" rmdir /s /q "%TEMP_MAVEN_HOME%"
)
if defined TAR_DIR (
    if exist "%TAR_DIR%" rmdir /s /q "%TAR_DIR%"
)

goto finally


rem ##########################
rem # usage
:usage
echo Usage: %0 [Options] [Switches] >&2
echo.
echo   Options:
echo     --agent-home=^<path^>                ^| optional: Agent home directory  >&2
echo     --archive=^<path^>                   ^| optional: path to .tar.gz archive file for Graal Libraries  >&2
echo     --graalvm-version=^<version^>        ^| optional: GraalVM version such as 25.0.0, default: %GRAALVM_VERSION% >&2
echo     --maven-version=^<version^>          ^| optional: Maven version such as 3.9.11, default: %MAVEN_VERSION% >&2
echo     --maven-wrapper-version=^<version^>  ^| optional: Maven Wrapper version such as 3.3.4, default: %MAVEN_WRAPPER_VERSION% >&2
echo     --temp-dir=^<path^>                  ^| optional: Temporary directory for Maven files, default: %TEMP_DIR% >&2
if defined JAVA_HOME     echo     --java-home=^<directory^>            ^| optional: Java Home directory, default: %JAVA_HOME% >&2
if not defined JAVA_HOME echo     --java-home=^<directory^>            ^| required: Java Home directory >&2
echo.
echo   Switches:
echo     -h ^| --help                         ^| displays usage >&2
echo.
goto final

rem ##########################
rem # set graalvm version
:set_graalvmversion
for /f "usebackq tokens=2 delims=,: " %%i in (`curl -k -L -s -S https://api.github.com/repos/graalvm/graalvm-ce-builds/releases/latest ^| find """tag_name"""`) do (
    call :set_graalvmversion2 %%~i
    goto final
)
goto final
:set_graalvmversion2
set a=%1
set a=%a:jdk-=%
set GRAALVM_VERSION=%a%
goto final

rem ##########################
rem # set maven wrapper version
:set_mavenwrapperversion
for /f "usebackq tokens=3 delims=<>" %%i in (`curl -k -L -s -S https://repo1.maven.org/maven2/org/apache/maven/wrapper/maven-wrapper-distribution/maven-metadata.xml ^| find "<latest>" ^| find "</latest>"`) do (
    call :set_mavenwrapperversion2 %%~i
    goto final
)
goto final
:set_mavenwrapperversion2
set MAVEN_WRAPPER_VERSION=%1
goto final

rem ##########################
rem # normalize archive path: 
rem #   delete .tar(.gz) extension
rem #   sets absolute path in ARCHIVE_PATH
:normalize_archivepath
if "%~x1" == ".gz" call :normalize_archivepath "%~dpn1"
if "%~x1" == ".tar" call :normalize_archivepath "%~dpn1"
call :set_archivepath "%~dpn1"
goto final

:set_archivepath
set ARCHIVE_PATH=%~dpn1
goto final

rem ##########################
rem # create maven settings
:make_maven_settings
(
@echo.^<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
@echo.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@echo.         xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"^>
@echo.    ^<localRepository^>%TEMP_MAVEN_HOME%^</localRepository^>
@echo.^</settings^>
) > "%~1"
goto final

rem ##########################
rem # create maven properties
:make_maven_properties
(
@echo.distributionUrl=https://dlcdn.apache.org/maven/maven-3/%MAVEN_VERSION%/binaries/apache-maven-%MAVEN_VERSION%-bin.zip
) > "%~1"
goto final

rem ##########################
rem # create maven pom
:make_maven_pom
(
@echo.^<?xml version="1.0" encoding="UTF-8"?^>
@echo.^<project^>
@echo.    ^<modelVersion^>4.0.0^</modelVersion^>
@echo.    ^<groupId^>download-graal^</groupId^>
@echo.    ^<artifactId^>download-graal-libs^</artifactId^>
@echo.    ^<version^>1.0^</version^>
@echo.
@echo.    ^<properties^>
@echo.        ^<graalvm.version^>%GRAALVM_VERSION%^</graalvm.version^>
@echo.    ^</properties^>
@echo.
@echo.    ^<dependencies^>
@echo.        ^<!-- Core --^>
@echo.        ^<dependency^>
@echo.            ^<groupId^>org.graalvm.sdk^</groupId^>
@echo.            ^<artifactId^>graal-sdk^</artifactId^>
@echo.            ^<version^>${graalvm.version}^</version^>
@echo.            ^<exclusions^>
@echo.                ^<exclusion^>
@echo.                    ^<groupId^>org.bouncycastle^</groupId^>
@echo.                    ^<artifactId^>*^</artifactId^>
@echo.                ^</exclusion^>
@echo.            ^</exclusions^>
@echo.        ^</dependency^>
@echo.
@echo.        ^<dependency^>
@echo.            ^<groupId^>org.graalvm.polyglot^</groupId^>
@echo.            ^<artifactId^>polyglot^</artifactId^>
@echo.            ^<version^>${graalvm.version}^</version^>
@echo.            ^<type^>jar^</type^>
@echo.            ^<exclusions^>
@echo.                ^<exclusion^>
@echo.                    ^<groupId^>org.bouncycastle^</groupId^>
@echo.                    ^<artifactId^>*^</artifactId^>
@echo.                ^</exclusion^>
@echo.            ^</exclusions^>
@echo.        ^</dependency^>
@echo.
@echo.        ^<!-- JavaScript --^>
@echo.        ^<dependency^>
@echo.            ^<groupId^>org.graalvm.polyglot^</groupId^>
@echo.            ^<artifactId^>js-community^</artifactId^>
@echo.            ^<version^>${graalvm.version}^</version^>
@echo.            ^<type^>pom^</type^>
@echo.            ^<exclusions^>
@echo.                ^<exclusion^>
@echo.                    ^<groupId^>org.bouncycastle^</groupId^>
@echo.                    ^<artifactId^>*^</artifactId^>
@echo.                ^</exclusion^>
@echo.            ^</exclusions^>
@echo.        ^</dependency^>
@echo.
@echo.        ^<!-- Python --^>
@echo.        ^<dependency^>
@echo.            ^<groupId^>org.graalvm.polyglot^</groupId^>
@echo.            ^<artifactId^>python-community^</artifactId^>
@echo.            ^<version^>${graalvm.version}^</version^>
@echo.            ^<type^>pom^</type^>
@echo.            ^<exclusions^>
@echo.                ^<exclusion^>
@echo.                    ^<groupId^>org.bouncycastle^</groupId^>
@echo.                    ^<artifactId^>*^</artifactId^>
@echo.                ^</exclusion^>
@echo.            ^</exclusions^>
@echo.
@echo.        ^</dependency^>
@echo.    ^</dependencies^>
@echo.^</project^>
) > "%~1"
goto final

:finally
ENDLOCAL & exit /b %ERRORLEVEL%

:final