@echo %DEBUG% off
rem NeXtMidas startup script
rem
rem @author Jeff Schoen / Neon Ngo
rem @version $Id: nmstart.bat,v 1.48 2012/06/26 12:12:15 ntn Exp $

if not exist "%NMROOT%"         goto NO_NMROOT
if not exist "%NMROOT%\nxm\sys" goto INVALID_NMROOT
if not exist "%NMROOT%\os\dos"  goto INVALID_NMROOT

rem check if %NMROOT% directory contains spaces in it
SET _nmroot=%NMROOT: =%
if not ["%_nmroot%"] == ["%NMROOT%"] goto SPACES_IN_NMROOT
set _nmroot=

rem ############################################################################
rem Cleanup any previous NeXtMidas session
call "%NMROOT%\os\dos\nmend"

rem If GCC compiler is installed, set NM_CC=gcc and NM_CC_INC=pathToCompilerIncludes
rem NM_CC=c:\cygwin\bin\gcc
rem NM_CC_INC=. -mno-cygwin

rem see which java home is defined (JAVAHOME then JAVA_HOME)
if not exist "%JAVAHOME%" (
  if not exist "%JAVA_HOME%" goto NO_JAVAHOME
  set JAVAHOME=%JAVA_HOME%
)

rem check if %JAVAHOME% directory contains spaces in it
SET _javahome=%JAVAHOME: =%
if not ["%_javahome%"] == ["%JAVAHOME%"] goto SPACES_IN_JAVAHOME
set _javahome=

rem ############################################################################
set NM_JB=%JAVAHOME%\bin
set NM_JAVA=%NM_JB%\java
set NM_JAVAC=%NM_JB%\javac
set NM_OS=%NMROOT%\os\dos
set NM_DLLS=%NM_OS%\dlls
set NM_OPT=sys
set NM_DIR=lib
set NM_PID=%RANDOM%
set PATH=%NM_OS%;%PATH%
set NM_SF=%HOME%\nmsf_%NM_PID%

if not exist "%HOME%" goto NO_HOME

rem check if %HOME% directory contains spaces in it
SET _home=%HOME: =%
if not ["%_home%"] == ["%HOME%"] goto SPACES_IN_HOME
set _home=

rem check if USER env is set (fall back to %USERNAME%)
if not defined USER (
  if not defined USERNAME goto NO_USER_ENV
  set USER=%USERNAME%
)

rem strip spaces from USER (by replacing space with blank)
SET _user=%USER: =%
@rem the caret char "^" escapes the redirection char ">"
if not ["%_user%"] == ["%USER%"] echo INFO: [nmstart] Spaces in %%USER%% have been removed: %USER% -^> %_user%
set USER=%_user%
set _user=

rem per bug 1368, look for NMHISTORY_DIR flag for saving nmsf files
rem if provided, set NM_SF to this value
rem otherwise, default to the user's home directory (previous implementation)
rem NM_SF is defined by default at the top of this script and will only be
rem changed with a valid NMHISTORY_DIR directory
if defined NMHISTORY_DIR (
  if exist "%NMHISTORY_DIR%" (
    set NM_SF=%NMHISTORY_DIR%\nmsf_%NM_PID%
    echo Setting NMHISTORY_DIR to %NMHISTORY_DIR%
  )
  if not exist "%NMHISTORY_DIR%" echo NMHISTORY_DIR defined but directory does not exist, using %HOME%.
)

rem Since 3.1.0 (Bug 2482), let users know if they are excluding certain AUXes from FILES and tab-complete
if defined NM_COMPLETION_AUX_EXCLUDE (
   echo NM_COMPLETION_AUX_EXCLUDE defined. FILES and tab-complete excluding %NM_COMPLETION_AUX_EXCLUDE%
)
rem Since 3.1.3: let users know they are using custom user-level startup macro
if defined NM_STARTUP_MACRO (
   echo Using '%NM_STARTUP_MACRO%' as a user-level startup macro
)

rem ### set common defines for java ############################################
set NM_JVM_FLAGS= -DNMROOT=%NMROOT%\ -DNMPID=%NM_PID% -DNMSF=%NM_SF% -DUSER=%USER% -DHOME=%HOME%\

rem # obtain optional user JVM flags from environment
if not ["%NM_USER_JVM_FLAGS%"] == [""]         set NM_JVM_FLAGS=%NM_JVM_FLAGS% %NM_USER_JVM_FLAGS%

rem ###########################################################################
rem # check OS environment variables (which generally takes precedence over settings in NM_USER_JVM_FLAGS)
rem ###########################################################################
if not ["%NM_USER_CP%"] == [""]                set NM_JVM_FLAGS=%NM_JVM_FLAGS% -DCLASSPATH=%NM_USER_CP%
if not ["%NM_USER_CP_AFTER_SYS%"] == [""]      set NM_JVM_FLAGS=%NM_JVM_FLAGS% -DCLASSPATHAFTERSYS=%NM_USER_CP_AFTER_SYS%
if not ["%NM_USER_CP_PREPEND%"] == [""]        set NM_JVM_FLAGS=%NM_JVM_FLAGS% -DCLASSPATHPREPEND=%NM_USER_CP_PREPEND%
if not ["%NM_COMPLETION_AUX_EXCLUDE%"] == [""] set NM_JVM_FLAGS=%NM_JVM_FLAGS% -DCOMPLETIONAUXEXCLUDE=%NM_COMPLETION_AUX_EXCLUDE%
if not ["%NM_STARTUP_MACRO%"] == [""]          set NM_JVM_FLAGS=%NM_JVM_FLAGS% -DNMSTARTUP.USER=%NM_STARTUP_MACRO%

rem Delete leftover midas state file from previous NeXtMidas session
if exist "%NM_SF%.msf" del /Q "%NM_SF%.msf"
if exist "%NM_SF%.bat" del /Q "%NM_SF%.bat"

rem check if java.exe exists
if not exist "%NM_JAVA%.exe" goto NO_JAVA
rem check if javac.exe exists (but only print warning if it doesn't)
if not exist "%NM_JAVAC%.exe" (
    echo WARN: [nmstart] Cannot find Java compiler at: %NM_JAVAC%
    echo       [nmstart] You will not be able to build/compile any Java classes.
)

rem ############################################################################
rem Print out the Copyright notice... do it manually if NeXtMidas has not been
rem compiled yet.
if not exist "%NMROOT%\nxm\sys\lib\Shell.class" goto NOT_BUILT

:BUILT
  rem run %nmstartup and then exit
  nm "-DCONFIGURE=TRUE"
  goto DONE

:NOT_BUILT
  type "%NMROOT%\nxm\sys\version.txt"
  echo NeXtMidas not built yet - see %NMROOT%\readme.txt  (or nm make all)
  goto DONE

rem ############################################################################
:NO_NMROOT
  echo ERROR: [nmstart] Specified NMROOT path does not exist: %NMROOT%
  echo        [nmstart] Please redefine %%NMROOT%% and run %%nmstart%% again
  goto DONE

:INVALID_NMROOT
  echo ERROR: [nmstart] Specified NMROOT path is invalid: %NMROOT%
  echo        [nmstart] Please redefine %%NMROOT%% and run %%nmstart%% again
  goto DONE

:SPACES_IN_NMROOT
  echo ERROR: [nmstart] Specified NMROOT path must not have spaces: %NMROOT%
  echo        [nmstart] Please redefine %%NMROOT%% and run %%nmstart%% again
  goto DONE

:NO_HOME
  echo ERROR: [nmstart] Specified HOME path does not exist: %HOME%
  echo        [nmstart] Please redefine %%HOME%% and run %%nmstart%% again
  goto DONE

:SPACES_IN_HOME
  echo ERROR: [nmstart] Specified HOME path must not have spaces: %HOME%
  echo        [nmstart] Please redefine %%HOME%% and run %%nmstart%% again
  goto DONE

:NO_USER_ENV
  echo ERROR: [nmstart] USER environment is not set: %USER%
  echo        [nmstart] Please define %%USER%% and run %%nmstart%% again
  goto DONE

:NO_JAVAHOME
  echo ERROR: [nmstart] Specified JAVAHOME path does not exist: %JAVAHOME%
  echo        [nmstart] Please redefine %%JAVAHOME%% and run %%nmstart%% again
  goto DONE

:NO_JAVA
  echo ERROR: [nmstart] Cannot find Java at: %NM_JAVA%
  echo        [nmstart] Current %%JAVAHOME%% is: %JAVAHOME%
  echo        [nmstart] Please redefine %%JAVAHOME%% and run %%nmstart%% again
  goto DONE

:SPACES_IN_JAVAHOME
  echo ERROR: [nmstart] Specified JAVAHOME path must not have spaces: %JAVAHOME%
  echo        [nmstart] Please redefine %%JAVAHOME%% and run %%nmstart%% again
  goto DONE

rem ############################################################################
:DONE
