@echo off
setlocal
cd /d %0\..

if defined IMANAGER_HOME cd %IMANAGER_HOME%\bin

:FIX_PATH
   if not defined CALLED_SELF goto CALL_SELF
   goto FIXUP
:CALL_SELF
   set CALLED_SELF=true
   call iManager.bat FIXUP JAVA_HOME "%JAVA_HOME%\" FIXUP WD "%CD%\" %1 %2 %3 %4 %5 %6 %7 %8 %9
   goto DONE
:FIXUP
   SET FIXPARAM=%1
   if not defined FIXPARAM goto FIXUP_DONE
   if not %FIXPARAM% == FIXUP goto FIXUP_DONE
   shift
   set %1=%~sdp2
   shift
   shift
   goto FIXUP
:FIXUP_DONE

:CHECK_JAVA_HOME
   set JAVA_HOME_BAK=%JAVA_HOME%
   set JAVA_HOME=
   if not defined JAVA_HOME goto FIX_JAVA_HOME
   if %JAVA_HOME% == "" goto FIX_JAVA_HOME
   goto DONE_JAVA_HOME
:FIX_JAVA_HOME
   if NOT EXIST windows\java goto ERROR_JAVA_HOME
   set JAVA_HOME=%WD%windows\java
   set JAVA_EXE=%JAVA_HOME%%JAVA_EXE%
   goto DONE_JAVA_HOME
:ERROR_JAVA_HOME
   if defined JAVA_HOME_BAK goto RESET_JAVA_HOME
   echo '
   echo ERROR!! The JAVA_HOME environment variable must be defined to point to a valid JDK.
   echo See the documentation for more details.
   echo '
   goto DONE_ERROR
:RESET_JAVA_HOME
   set JAVA_HOME=%JAVA_HOME_BAK%
:DONE_JAVA_HOME

set JAVA_EXE=%JAVA_HOME%\bin\javaw
set SHOW_VAR=%1

:SET_DEBUG_OUTPUT
   if not defined SHOW_VAR goto DONE_SET_DEBUG_OUTPUT
   if /i %SHOW_VAR% NEQ ShowTomcatOutput goto DONE_SET_DEBUG_OUTPUT
   set JAVA_EXE=%JAVA_HOME%\bin\java
:DONE_SET_DEBUG_OUTPUT

:CHECK_VALID_JDK
   if NOT EXIST %JAVA_HOME%\lib\tools.jar goto ERROR_VALID_JDK
   goto DONE_VALID_JDK
:ERROR_VALID_JDK
   echo '
   echo ERROR!! The JAVA_HOME environment variable is not set to a full valid JDK.
   echo See the documentation for more details.
   echo '
   goto DONE_ERROR
:DONE_VALID_JDK

:SET_VAR
   set CLASSPATH=%JAVA_HOME%\lib\tools.jar
   set CLASSPATH=%CLASSPATH%;bootstrap.jar
   set CLASSPATH=%CLASSPATH%;windows\swt.jar
   set CLASSPATH=%CLASSPATH%;iManBootstrap.jar

   set SWT_LIB_PATH=%WD%windows
   set NATIVE_LIB_PATH=%WD%..\tomcat\webapps\nps\WEB-INF\bin\windows
   set PATH=%NATIVE_LIB_PATH%;%SWT_LIB_PATH%;%PATH%

   set MAIN_CLASS=com.novell.emframe.bootstrap.iManagerBootstrap
   set NICI_INSTALL_FILE=..\NICI\windows\wcniciu0.exe
   set NICI_DLL_FILE=%SYSTEMROOT%\system32\ccsw32.dll   

   set CLASSPATH=%CLASSPATH%;%SWT_LIB_PATH%\swt.jar
   set CLASSPATH=%CLASSPATH%;%WD%..\tomcat\bin\commons-logging-api.jar
   set CLASSPATH=%CLASSPATH%;%WD%..\tomcat\bin\jmx.jar

   set BPATH=%WD%..\tomcat\common\endorsed\xercesImpl.jar
   set BPATH=%BPATH%;%WD%..\tomcat\common\endorsed\xml-apis.jar
   
   set EPATH=%WD%..\tomcat\common\endorsed

:DONE_VAR

:CHECK_NICI
   if not exist "%NICI_DLL_FILE%" goto INSTALL_NICI
   goto DONE_NICI
:INSTALL_NICI
   echo '
   echo Novell iManager requires NICI to be installed on the computer.
   echo NICI will be installed.
   echo Novell iManager will be started when the NICI installation has been completed.
   %NICI_INSTALL_FILE%
   echo '
:DONE_NICI

:START_IMANAGER
:: echo.
:: echo EPATH = %EPATH%
:: echo BPATH = %BPATH%
:: echo CLASSPATH = %CLASSPATH%
:: echo.
echo Running Novell iManager
:: echo.
:: echo %JAVA_EXE% -server -Xbootclasspath/a:%BPATH%
:: echo -Djava.endorsed.dir=%EPATH%
:: echo -Djava.library.path=%NATIVE_LIB_PATH%;%SWT_LIB_PATH% 
:: echo %DEBUG_PARAMS% %MAIN_CLASS% %1 %2 %3 %4 %5 %6 %7 %8 %9
:: echo.

windows\mozilla\xulrunner --register-user
start %JAVA_EXE% -Xms256m -Xmx512m -Xbootclasspath/a:%BPATH% -cp %CLASSPATH% -Djava.endorsed.dir=%EPATH% -Djava.library.path=%NATIVE_LIB_PATH%;%SWT_LIB_PATH% %DEBUG_PARAMS% %MAIN_CLASS% %1 %2 %3 %4 %5 %6 %7 %8 %9

goto DONE

:DONE_ERROR
pause

:DONE

endlocal