@echo off
@REM
@REM Copyright (c) 2018-2022
@REM Beijing Baolande Software Corporation
@REM
@REM All Rights Reserved
@REM

rem Otherwise either JAVA_BASE or JAVA_HOME are fine
if not "%JAVA_BASE%" == "" goto validateJavaBase
if not "%JAVA_HOME%" == "" goto validateJavaHome
echo Neither the JAVA_HOME nor the JAVA_BASE environment variable is defined
echo At least one of these environment variable is needed to run this program
goto error

:validateJavaBase
rem Check if we have a usable JAVA_BASE
if not exist "%JAVA_BASE%\bin\java.exe" goto errorJavaBase
if not exist "%JAVA_BASE%\bin\javaw.exe" goto errorJavaBase
goto rightJavaBase

:validateJavaHome
rem Check if we have a usable JAVA_HOME
if not exist "%JAVA_HOME%\bin\java.exe" goto errorJavaHome
if not exist "%JAVA_HOME%\bin\javaw.exe" goto errorJavaHome
set JAVA_BASE=%JAVA_HOME%
goto rightJavaBase

:errorJavaBase
echo The JAVA_BASE environment variable is not defined correctly
echo This environment variable is needed to run this program
goto error

:errorJavaHome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto error

:rightJavaBase
rem Don't override the endorsed dir if the user has set it previously
if not "%JAVA_ENDORSED_DIRS%" == "" goto gotEndorseddir
rem Java 9 no longer supports the java.endorsed.dirs
rem system property. Only try to use it if
rem BES_HOME/endorsed exists.
if not exist "%BES_HOME%\endorsed" goto gotEndorseddir
set "JAVA_ENDORSED_DIRS=%BES_HOME%\endorsed"
:gotEndorseddir

set JAVA="%JAVA_BASE%\bin\java.exe"

for /F tokens^=2^ delims^=^" %%i in ('%%JAVA%% -version 2^>^&1 ^| findstr [\"].*[\"]') do (set result=%%i)

@echo %result% | findstr "^1.[2|3|4|5|6]" > nul
if "%errorlevel%" == "0" (echo Please use JDK 1.7 or higher.
exit /b 1)

@echo %result% | findstr "^1.[7|8]" > nul
if "%errorlevel%" == "1" goto overAddInJdk78
set JAVA_OPTS=%JAVA_OPTS% -Djava.endorsed.dirs="%BES_HOME%\lib\endorsed"
set JAVA_OPTS=%JAVA_OPTS% -Djava.ext.dirs="%JAVA_BASE%\lib\ext;%JAVA_BASE%\jre\lib\ext;%BES_ROOT%\lib\ext"
:overAddInJdk78

@echo %result% | findstr "^1.7" > nul
if "%errorlevel%" == "0" (
    set JAVA_OPTS=%JAVA_OPTS% -XX:MaxPermSize=256m -XX:PermSize=128m
) else (
    @echo %result% | findstr "^[9|10|11|12|13|14|15|16|17|18]" > nul
    if "%errorlevel%" == "0" (
        set java_add_opens="--add-opens=java.base/java.io=ALL-UNNAMED"
        set java_add_opens=%java_add_opens% --add-opens=java.base/java.lang=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.base/java.lang.reflect=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.base/java.net=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.base/java.nio=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.base/jdk.internal.ref=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.base/sun.net.www.protocol.jar=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
        @REM Support gmssl when using jdk10+.
        set java_add_opens=%$java_add_opens% --add-opens=java.base/javax.crypto=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.base/jdk.internal.misc=ALL-UNNAMED
        @REM Avoid ClassNotFoundException when viewing classloader and using jdk9+.
        set java_add_opens=%$java_add_opens% --add-opens=java.base/jdk.internal.module=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.base/jdk.internal.loader=ALL-UNNAMED
        set java_add_opens=%$java_add_opens% --add-opens=java.base/sun.nio.ch=ALL-UNNAMED
        set JAVA_OPTS=%$java_add_opens% %JAVA_OPTS%
    )
    set JAVA_OPTS=%JAVA_OPTS% -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=128m
)

endlocal

goto end

:error
exit /b 1

:end
exit /b 0