@echo %DEBUG% off
rem ############################################################################
rem NeXtMidas shell instantiation script
rem
rem convention is to user all lower case for local variables
rem
rem nm_java_cmd   Used to run Java, configured for use with NeXtMidas (containing
rem               appropriate JVM flags and options. This is used to run java and
rem               "boot-up" NeXtMidas by configuring and starting the required
rem               class loader and then running the NeXtMidas shell (set using
rem               -DSHELL=<shell class>).
rem
rem nm_javac_cmd  Used to compile the Java files in the %NMROOT%\tools directory
rem               which are used to build the rest of NeXtMidas.
rem               This has the NeXtMidas boot-strap build / compile options.
rem               Again, there should be NO NEED to CHANGE this since it only
rem               affects files built under $NMROOT/tools.
rem
rem NOTE:  The flags in nm_javac_cmd are used for boot strap build of NeXtMidas
rem        and should not be modified. To configure your option tree's target JVM
rem        and/or compile options, set java.version and/or java.flags.append
rem        in <option_tree>\cfg\build.props. See %NMROOT%\nxm\sys\cfg\build.props
rem        for default values.
rem
rem @author Jeff Schoen / Neon Ngo
rem ############################################################################

rem save all command line arguments into args (this does not include %0)
set args=%*

if not defined NM_BOOT_CP_APPEND (
  set nm_boot_classpath=%NMROOT%/tools
) else (
  set nm_boot_classpath=%NMROOT%/tools;%NM_BOOT_CP_APPEND%
)

rem set NeXtMidas baseline (SYS) javac flags for bootstrap build ONLY (%NMROOT\tools\*.java)
rem using -Xlint:-options to suppress JDK8+ warning: [options] bootstrap class path not set in conjunction with -source 1.8
set nm_sys_javac_flags=-source 1.8 -target 1.8 -Xlint:-options

rem get java version since it will be used to set the reflective-access flag
for /f tokens^=3 %%i in ('%JAVAHOME%\bin\java.exe -version 2^>^&1 ^| findstr /i "version"') do set java_version=%%i
for /f tokens^=1^ delims^=^". %%j in ('echo %java_version% 2^>^&1') do set java_version=%%j

rem java.util.logging keeps LogUtilites quiet. java.security keeps SSLUtilites quiet
if %java_version% lss 9 (
  set nm_reflective_access=
) else (
  set nm_reflective_access=--add-opens java.logging/java.util.logging=ALL-UNNAMED --add-opens java.base/java.security=ALL-UNNAMED --add-opens java.base/sun.security.util=ALL-UNNAMED --add-opens java.base/sun.security.pkcs=ALL-UNNAMED
)

rem If the tools.jar exists, it is added, otherwise it is not
if exist "%JAVAHOME%\lib\tools.jar" (
  set nm_java_cmd="%NM_JAVA%" %NM_JVM_FLAGS% -classpath "%nm_boot_classpath%;%JAVAHOME%\lib\tools.jar"
  set nm_javac_cmd="%NM_JAVAC%" -g -Xlint %nm_sys_javac_flags% -classpath "%nm_boot_classpath%;%NMROOT%;%JAVAHOME%\lib\tools.jar"
) else (
  set nm_java_cmd="%NM_JAVA%" %NM_JVM_FLAGS% %nm_reflective_access% -classpath "%nm_boot_classpath%"
  set nm_javac_cmd="%NM_JAVAC%" -g -Xlint %nm_sys_javac_flags% -classpath "%nm_boot_classpath%;%NMROOT%"
)

if [%1] == [make] (
  call %NM_OS%\nmjdkswap.bat %java_version%

  rem # BUG 1607: only run the boot-strap build when building ALL or SYS option tree(s)
  rem #           or when making zip archive for distribution
  rem #           or when cleaning under $NMROOT
  rem #           if option tree (3rd argument) is empty, it defaults to ALL
  rem # /I switch is for case insensitive string compares
  if /I [%2] == [boot]  goto MAKE
  if /I [%2] == [clean] goto MAKE
  if /I [%2] == [zip]   goto MAKE
  if    [%3] == []      goto MAKE
  if /I [%3] == [all]   goto MAKE
  if /I [%3] == [sys]   goto MAKE
)
if [%1] == [end]  goto END
if [%1] == [edit] goto EDIT
if not exist "%NMROOT%\nxm\sys\lib\Shell.class"   goto NOBUILD
if not exist "%NMROOT%\tools\NmClassLoader.class" goto BUILDNMCLASSLOADER
goto SHELL

rem ############################################################################
rem # NeXtMidas (boot-strap) Build
:MAKE
  echo Compiling Tools
  %nm_javac_cmd% %NMROOT%\tools\*.java

  %nm_java_cmd% -DSHELL=tools.NmMake NmClassLoader %args%

  rem Need to run the bat file to rename the DLL's
  if not exist "%NM_SF%.bat" goto DONE
  call "%NM_SF%.bat"
  del  "%NM_SF%.bat"

  rem reload the nmstartup.mm file if shell is built now
  if not exist "%NMROOT%\nxm\sys\lib\Shell.class" goto DONE
  %nm_java_cmd% -DSHELL=nxm.sys.lib.Shell -DCONFIGURE=TRUE NmClassLoader
  goto DONE

rem ############################################################################
rem # Note: Unconfigured IDEs (Netbeans/Eclipse) may delete just NmClassLoader.class
rem #       and nothing else (as it is not in the "tools" package, so just compile
rem #       this one file and continue to run the shell
:BUILDNMCLASSLOADER
  echo Compiling NmClassLoader
  %nm_javac_cmd% "%NMROOT%\tools\NmClassLoader.java"
  goto SHELL

rem ############################################################################
:END
  call "%NM_OS%\nm%*"
  goto DONE

:EDIT
  call "%NM_OS%\nm%*"
  goto DONE

:NOBUILD
  echo NeXtMidas not built yet - see %NMROOT%\readme.txt  (or nm make all)
  goto DONE

rem ############################################################################
:SHELL
  if exist "%NM_SF%.bat" del "%NM_SF%.bat"

  %nm_java_cmd% -DSHELL=nxm.sys.lib.Shell NmClassLoader %args%

  if not exist "%NM_SF%.bat" goto DONE

  call "%NM_SF%.bat"
  rem if NM_SF var is no longer set, then goto DONE to prevent error messages
  if ["%NM_SF%"] == [""] goto DONE

  if exist "%NM_SF%.bat" del /Q "%NM_SF%.bat"

  @rem BUG 1956, only re-enter NeXtMidas shell when nm is run without any arguments
  if ["%args%"] == [""] goto SHELL

rem ############################################################################
:DONE
rem cleanup local variables
set args=
set nm_boot_classpath=
set nm_java_cmd=
set nm_javac_cmd=
set nm_sys_javac_flags=

rem Set %ERRORLEVEL% on exit and also set environment variable nm_cmd_errorlevel
rem %ERRORLEVEL% is only reliable if executing nm and checking error
rem   level immediately afterwards in a batch file

if not ["%NM_PID%"] == [""] (
  if exist "%HOME%\nm_last_cmd_status_%NM_PID%.txt" (
    set /P nm_cmd_errorlevel=< "%HOME%\nm_last_cmd_status_%NM_PID%.txt"
    del "%HOME%\nm_last_cmd_status_%NM_PID%.txt"
  ) else (
    set /a nm_cmd_errorlevel=0
  )
)
exit /B %nm_cmd_errorlevel%
