@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

::
:: Set HOMEDIR: the application install directory
::
for %%d in ("%~dp0.") do set HOMEDIR=%%~fd

::
:: Get the class being launched from the first command line parameter and store
:: in %JCLASS%. (For example, when launching OrbitIO, the first parameter should
:: be specified as com.sigrity.orbit.OrbitIO and JCLASS will be set to that.)
::
set JCLASS=%1
if "%JCLASS%"=="" (
  echo No Java class specified to launch, exiting.
  exit /b 1
)

::
:: Get the simple name of the class being launched (after the final '.') and
:: store it in %JCLASS_SIMPLE%. (For example, when launching OrbitIO, the first
:: parameter should be specified as com.sigrity.orbit.OrbitIO and JCLASS_SIMPLE
:: will be set to "OrbitIO".)
::
set "JCLASS_SIMPLE=%JCLASS:.=" & set "JCLASS_SIMPLE=%"

::
:: Include the java directory, all jars in the java directory, and all jars recursively in the oem/java directory.
:: (Using wild cards per https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html#A1100762.)
::
if not defined CP (
  set CP="%HOMEDIR%\java"
) else (
  set CP=!CP!;"%HOMEDIR%\java"
)
set CP=%CP%;"%HOMEDIR%\java\*"

for /r "%HOMEDIR%\oem\java" %%d in (.) do if exist "%%~fd\*.jar" set CP=!CP!;"%%~fd\*"

::
:: Set JAVA to the appropriate executable to use to start the Java runtime. Location priority
:: is as follows:
::  1. %JCLASS%.jre evironment variable (bin/java[w] will be appended).
::  2. Local runtime in the "jre" directory of the install directory.
::  3. Local runtime in the "jre{platform}" directory of the install directory where {platform} is
::     the platform where the product is being executed. Currently supported platforms are Win32,
::     and Win64 (other platforms are supported in the "launcher" script used on *nix-like OSes).
::  4. "java" executable found on the system path.
::
set JREVAR=!JCLASS!.jre
set JRE=!%JREVAR%!

set PLATFORM=
if "!PROCESSOR_ARCHITECTURE!"=="x86" (
  set PLATFORM=Win32
) else if "!PROCESSOR_ARCHITECTURE!"=="AMD64" (
  set PLATFORM=Win64
) else (
  set PLATFORM=Win
)

if exist "%HOMEDIR%\jre!PLATFORM!\bin\javaw.exe" (
  set JRE_PLATFORM=!PLATFORM!
)

set JAVAEXE=java
if defined OrbitNoConsole (
  echo Console disabled.
  set JAVAEXE=javaw
)

if not ""=="!JRE!" (
  set JAVA="!JRE!\bin\!JAVAEXE!"
  echo Using Java runtime "!JRE!" specified by environment variable "!JREVAR!".
) else if exist "%HOMEDIR%\jre\bin\javaw.exe" (
  echo Using local Java runtime "%HOMEDIR%\jre".
  set JAVA="%HOMEDIR%\jre\bin\!JAVAEXE!"
) else if not "!JRE_PLATFORM!"=="" (
  echo Using local !JRE_PLATFORM! Java runtime "%HOMEDIR%\jre!JRE_PLATFORM!".
  set JAVA="%HOMEDIR%\jre!JRE_PLATFORM!\bin\!JAVAEXE!"
) else (
  echo Using Java runtime from system path.
  set JAVA=!JAVAEXE!
)

if defined OrbitNoConsole set JAVA=start "OrbitIO" !JAVA!

:: Append any requested classpath items.
set ADDCP_VAR=!JCLASS!.cp
set ADDCP=
if not ""=="!%ADDCP_VAR%!" (
  set ADDCP=!%ADDCP_VAR%!
  echo Appending classpath "!ADDCP!" specified by environment variable "!ADDCP_VAR!".
  set CP=!CP!;!ADDCP!
)

::
:: Read Java VM arguments from configuration files:
::  conf\%JCLASS%.!PLATFORM!.jvmargs
::  %CDS_SITE%\%JCLASS_SIMPLE%\!PLATFORM!.jvmargs
::  !%JCLASS_SIMPLE%_SITE!\!PLATFORM!.jvmargs
::  %USERPROFILE%\.%JCLASS_SIMPLE%\!PLATFORM!.jvmargs
::
set JVMARGS_FILE=

:: Define a function to read a JVM arguments config file.
goto :SkipReadArgsFunc
:ReadArgs
if exist "%~1" (
  for /f "usebackq tokens=*" %%l in ("%~1") do (
    set line=%%l
    if not "!line:~0,1!"=="#" set JVMARGS_FILE=!JVMARGS_FILE! !line!
  )
)
goto :eof
:SkipReadArgsFunc

call :ReadArgs "%HOMEDIR%\conf\%JCLASS%.!PLATFORM!.jvmargs"
if not ""=="%CDS_SITE%" call :ReadArgs "%CDS_SITE%\%JCLASS_SIMPLE%\!PLATFORM!.jvmargs"
if not ""=="!%JCLASS_SIMPLE%_SITE!" call :ReadArgs "!%JCLASS_SIMPLE%_SITE!\!PLATFORM!.jvmargs"
call :ReadArgs "%USERPROFILE%\.%JCLASS_SIMPLE%\!PLATFORM!.jvmargs"

if not ""=="!JVMARGS_FILE!" (
  echo Using Java VM arguments "!JVMARGS_FILE!" specified by configuration files.
)

::
:: Read arguments to be sent to the Java VM from the environment variable %JCLASS%.jvmargs.
::
set JVMARGS_VAR=!JCLASS!.jvmargs
set JVMARGS=
if not ""=="!%JVMARGS_VAR%!" (
  set JVMARGS=!%JVMARGS_VAR%!
  echo Using Java VM arguments "!JVMARGS!" specified by environment variable "!JVMARGS_VAR!".
)

::
:: The next line can be uncommented to disable Direct3D on Windows which may sometimes workaround
:: buggy display drivers and certain performance issues. This option may also be set in a
:: configuration file read earlier in this script.
::
:: set JVMARGS=!JVMARGS! -Dsun.java2d.d3d=false

::
:: The next line can be uncommented to set maximum available memory to 4GB and edited to set
:: a different amount. A -Xmx argument set in the %JCLASS%.jvmargs environment variable
:: will override this amount unless the following -Xmx argument is moved after the !JVMARGS!
:: variable.
::
:: set JVMARGS=-Xmx4096m !JVMARGS!

::
:: Additional JVMARGS
::
set JVMARGS=!JVMARGS! --add-opens java.base/java.util=ALL-UNNAMED
set JVMARGS=!JVMARGS! --add-opens java.base/java.text=ALL-UNNAMED
set JVMARGS=!JVMARGS! --add-opens java.desktop/java.awt.font=ALL-UNNAMED
set JVMARGS=!JVMARGS! --add-opens java.desktop/javax.swing=ALL-UNNAMED
set JVMARGS=!JVMARGS! --add-opens java.desktop/javax.swing.plaf.basic=ALL-UNNAMED

::
set CMD_JVMARGS=

:: Define a function to read a JVM arguments config file.
set "argv[0]=%~0"
set ARG_INDEX=1
for %%x in (%*) do (
  set "argv[!ARG_INDEX!]=%%~x"
  set /a ubound = ARG_INDEX, ARG_INDEX += 1
)

set ARG_INDEX=1
for %%x in (%*) do (
  set /A ARG_INDEX+=1
  if "%%~x"=="-memory" (
    call set LAUNCHER_SPEC_MEM=%%argv[!ARG_INDEX!]%%
    echo Using runtime specified maximum memory "!LAUNCHER_SPEC_MEM!".
    set CMD_JVMARGS=!CMD_JVMARGS! -Xmx!LAUNCHER_SPEC_MEM!
  )
)

:: Update PATH so native libraries can be found.
set PATH="%HOMEDIR%\oem\java";%PATH%

::
:: Run the virtual machine.
::
!JAVA! -cp %CP% !JVMARGS_FILE! !JVMARGS! !CMD_JVMARGS!  -javaagent:%HOMEDIR%\orbitio-crack.jar %*
set EXIT=%errorlevel%
::
:: Examine the exit code and report if there was an unexpected error.
:: Note: Exit code 250 is generated by sourceAndExit command line argument; exit code 130 seems to
:: be generated by a ctrl-c at the command line.
::
if not "%EXIT%"=="0" if not "%EXIT%"=="130" if not "%EXIT%"=="250" (
  echo An error occurred [%EXIT%]. For help, please contact support.
  if not "%launcher_batch%" == "true" (
    echo Press a key to continue...
    pause > nul
  )
)

exit /b %EXIT%
