@echo off
setlocal enabledelayedexpansion
rem ****************************************************************************
rem Test initializing graphical macro many times in succession
rem
rem Arguments: 1st: (optional) desired NMROOT [DEF=login's NMROOT]
rem            2nd: (optional) desired number of iterations [DEF=10,000]
rem
rem @bug:       2499: Panel: SWING AWT deadlocks when running a macro
rem @bug:       2500: Occasional exceptions when running sd360 a multitude of times
rem @since:     NeXtMidas 4.1.4
rem @author:    Zachary Matthews
rem
rem NOTE: Based on original test_sd360_loop.sh by JC4 / J Bauer / Neon Ngo
rem
rem ****************************************************************************

rem Get start time:
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do (
  set /a "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
echo start time = %time% %date%

rem if arg1 is given, use it for path to NMROOT
if not ["%1"] == [""] (
  set NMROOT = %1
)

rem if arg2 is given, use it for number of iteration to run
if not ["%2"] == [""] (
  set /a numIter = %2
) else (
  set /a numIter = 100000
)

echo using NMROOT = %NMROOT%
call "%NMROOT%\os\dos\nmstart.bat"

echo turn on stacktrace for debugging
call "%NMROOT%\os\dos\nm.bat" debug on trace

echo Running sd360 in continuous loop for %numIter% times ...

set /a numIterMinus1 = %numIter%-1
for /l %%i in (0,1,%numIterMinus1%) do (
  echo|set /p="%%i: "
  call "%NMROOT%\os\dos\nm.bat" sd360 1e5
)

rem Get end time:
for /f "tokens=1-4 delims=:.," %%a in ("%time%") do (
  set /a "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
echo end time = %time% %date%

rem Get elapsed time:
set /A elapsed = (%end%-%start%)/100
echo Total elapsed time (sec) = %elapsed%

set numIter=
set numIterMinus1=
set start=
set end=
set elapsed=
