!******************************************************************************!
! Test running a command outside of a Midas Context
!
! @Author:  J. Bauer
! @Bug:     1062, 2275, 2315, 2317
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct   GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch ALTTESTENV  alttestenv GET DEF=FALSE SDEF=TRUE  ! alternate test environment

  res altinstr ""
  if alttestenv then
    res altinstr "/ALTTESTENV"
  endif

  if interAct then
    ! NXM-3845 since NeXtMidas 4.1.1
    if ^{env.NMROOT} EQS "classpath:" then
      invoke pathToJar nxm.sys.lib.Shell.getPathToOptJar(sys)
      if env.osname eqss "Windows" then
        invoke jarsIndex pathToJar.indexOf(\jars\)
      else
        invoke jarsIndex pathToJar.indexOf("/jars/")
      endif

      set nxm_sys_jar pathToJar
      set jarsFolder "^{pathToJar.substring(0,^{jarsIndex}+6).concat("*")}"
      set cpTestCase1and2 "\"^{jarsFolder}\""
      set gradleTestMidasDir ""
      invoke isGradleBuild nxm.sys.lib.Shell.isGradleBuild()
      if isGradleBuild then
        set gradleTestMidasDir "sys/src/test/midas/"
      endif
      set userTestCase3 "^{pathToJar.substring(0,^{jarsIndex}+1)}^{gradleTestMidasDir}nxm/sys/test/custom_startup.mm"
      set nmrootTestCase4and5 "^{pathToJar.substring(0,^{jarsIndex}+1)}"
    else
      file name nxm_sys_jar   (^{env.nmroot},jars) nxm-sys.jar
      file name jarsFolder    (^{env.nmroot},jars) *
      set cpTestCase1and2 "^{env.nmroot}"
      set userTestCase3 "nxm.sys.test.custom_startup.mm"
      set nmrootTestCase4and5 "^{env.nmroot}"
    endif

    invoke javaHome nxm.sys.lib.Shell.getJavaHome()

    if ENV.OSNAME EQSS "Windows" then
      invoke nxm_sys_jar nxm_sys_jar.replace("/",\)
      invoke jarsFolder jarsFolder.replace("/",\)
      invoke cpTestCase1and2 cpTestCase1and2.replace("/",\)
      invoke userTestCase3 userTestCase3.replace("/",\)
      invoke nmrootTestCase4and5 nmrootTestCase4and5.replace("/",\)
      set pathTestCase4Gradle "^{nmrootTestCase4and5}sys\\src\\test\\midas\\nxm\\sys\\test\\runNativeCodeJar.bat"
      set pathTestCase4Standard "^{nmrootTestCase4and5}nxm\\sys\\test\\runNativeCodeJar.bat"
    else
      set pathTestCase4Gradle "^{nmrootTestCase4and5}sys/src/test/midas/nxm/sys/test/runNativeCodeJar.sh"
      set pathTestCase4Standard "^{nmrootTestCase4and5}nxm/sys/test/runNativeCodeJar.sh"
    endif

    ! get OS dependent native jar name
    invoke S:nxm_native_name=nxm.sys.lib.Util.getNativeJarName("SYS",null,null)
    file name nxm_native_jar (^{env.nmroot},jars) ^{nxm_native_name}

    ! Add extra JVM argument if Java major version is greater than or equal to 17 - since NeXtMidas 4.1.1
    set jvmArgLogging ""
    invoke javaMajVer nxm.sys.lib.Shell.getJavaMajorVersion()
    if javaMajVer ge 17 then set jvmArgLogging " --add-opens java.logging/java.util.logging=ALL-UNNAMED"

    import "JAVAHOME" javaHomeDir

    pipe init
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label mainLabel "Main"
        gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
        set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol label panellabel "Instructions"
      gcontrol panel apanel "Instructions" 600 900

      shellgui/attach/bg/win=apanel

      say "Open a new OS shell terminal and run the following 5 tests:"
      say ""
      say "(1) Run the stand-alone java test class 'TestNeXtMidas'
      say "$ ^{javaHome}^{jvmArgLogging} -classpath ^cpTestCase1and2 nxm.sys.test.TestNeXtMidas"
      say "     See and do the following:"
      say "     (a) Plot of a sunset."
      say "         Click 'X' to close the window."
      say "     (b) Plot of a sinewave and on terminal command line:"
      say "           10S: AUX.TEST_NXM = /tmp/bogus
      say "           TEST_CONVOLVE tests should all PASS
      say "         Click 'X' to close the window."
      say "     (c) Plot from test_plot.mm macro."
      say "         Click PASS on the macro gui"
      say "     (d) Panel with 3 sections: top: world, center: SD360, bottom: list"
      say "         AND terminal command line results of TEST_CONVOLVE"
      say "         Click 'X' to close the panel."
      say ""
      say "(2) Run the stand-alone java test class 'TestNeXtMidas' for detecting if Midas knows it's a Java App
      say "$ ^{javaHome}^{jvmArgLogging} -classpath ^cpTestCase1and2 nxm.sys.test.TestNeXtMidas JavaApp"
      say "     See and do the following:"
      say "     (a) List2 of a csv file."
      say "         Click 'X' to close the window."
      ! #3 is test case for BUG 2315 (NeXtMidas does not honor -DNONATIVE=TRUE)
      !   and test case for BUG 2317 (default to -shell if no -command, -cmd, -shell, -prompt, or -script are given)
      !   and test case for BUG 1447 (using custom USER startup macro via NMSTARTUP.USER system property)
      say ""
      say "(3) Run NeXtMidas directly from jar file. A custom startup macro should be executed."
      say "    A NeXtMidas shell via SHELLGUI (if graphics is enabled) or a text terminal should run."
      say "$ ^{javaHome}^{jvmArgLogging} -DNONATIVE=TRUE -DNMSTARTUP.USER=^userTestCase3 -jar ^{nxm_sys_jar}
      ! (3a) test case for NXM-2948 (ERROR running from NeXtMidas Jar)
      say "    (a) In the NeXtMidas shell, STATUS a file without the extension:"
      say "        nM> status apenny"
      say "        This should work and not give a CouldNotFindException."
      say "    (b) There should be no errors on the terminal"
      say "    (C) Close the NeXtMidas shell"
      say ""
      ! #4 Tests NeXtMidas proper loading of native code from a jar NXM-3516
      say "(4) Specifically tests the loading of native code"
      say "    NOTE: JAVAHOME and JAVA_HOME should be set to \"^{javaHomeDir}\" in the new OS shell terminal before running this test."
      say "    When running with Gradle directory structure:
      say "      $ ^{pathTestCase4Gradle}"
      say "    When running with standard directory structure:"
      say "      $ ^{pathTestCase4Standard}"
      say "    You should NOT see any exceptions on the terminal"
      say ""
      ! #5 is a test case for bug 2124 (loading native libraries from JAR files)
      say "(5) Run NeXtMidas using native code directly from jar file using a custom startup macro.""
      say "     A NeXtMidas shell via SHELLGUI (if graphics is enabled) or a text terminal should run."
      say "    NOTE: JAVAHOME and JAVA_HOME should be set to \"^{javaHomeDir}\" in the new OS shell terminal before running this test."
      say "    NOTE: NMROOT should be set to ^{nmrootTestCase4and5} in the new OS shell terminal before running this test."
      say "$ ^{javaHome}^{jvmArgLogging} -DNONATIVE=FALSE -DNMSTARTUP.USER=nxm.sys.test.custom_startup.mm -DNMPID=1234 &
              -DNMROOT="classpath:" -classpath "^jarsFolder" nxm.sys.lib.NeXtMidas -path=DSP,SYS,UCL"
      say "     (a) Run 'CONFIDENCE/BASELINE/INTERACTIVE=FALSE^{altinstr}'in the SHELLGUI.
      say "         There should be no errors."
      say "     (b) Close the NeXtMidas shell"
      say ""
      ! #6 is a test case for NXM-2900 (loading option tree from jar files in class path)
      say "(6) Run the stand-alone java test class 'TestNeXtMidasLoadOptFromClasspath'
      say "$ ^{javaHome}^{jvmArgLogging} -DNONATIVE=TRUE -DNMSTARTUP.USER=nxm.sys.test.custom_startup.mm -classpath "^jarsFolder" &
              nxm.sys.test.TestNeXtMidas addOptToPathFromClasspath"
      say ""
      say "If all the tests pass click 'Pass', otherwise 'Fail' on this macro."
      say "There should NOT be any FAILED tests in the console/terminal."

    pipe off

  endif
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name:  User indicated" msg.data eqss "PASS"
    pipe stop
  endif
return
