@echo off
if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem Start/Stop Script for the WILLOW Server
rem
rem Environment Variable Prequisites
rem
rem   WILLOW_HOME     Base directory for resolving dynamic portions
rem                   of a Willow installation.  If not present, resolves to
rem                   the same directory that WILLOW_HOME points to.
rem
rem   WILLOW_OPTS    (Optional) Java runtime options used when the "start",
rem                   "stop", or "run" command is executed.
rem
rem   WILLOW_TMPDIR  (Optional) Directory path location of temporary directory
rem                   the JVM should use (java.io.tmpdir).  Defaults to
rem                   %WILLOW_HOME%\temp.
rem
rem   WILLOW_PLUGIN  (Optional) Directory path location of plugin directory
rem                   the WILLOW should use .  Defaults to
rem                   %WILLOW_HOME%\plugin.
rem
rem   JAVA_HOME       Must point at your Java Development Kit installation.
rem
rem   JAVA_OPTS       (Optional) Java runtime options used when the "start",
rem                   "stop", or "run" command is executed.
rem
rem ---------------------------------------------------------------------------

rem Guess WILLOW_HOME if not defined
if not "%WILLOW_HOME%" == "" goto gotHome
set WILLOW_HOME=.
if exist "%WILLOW_HOME%\bin\willow.bat" goto okHome
set WILLOW_HOME=..

:gotHome
if exist "%WILLOW_HOME%\bin\willow.bat" goto okHome
echo The WILLOW_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end

:okHome
if not "%WILLOW_PLUGIN%" == "" goto gotPlugIn
set WILLOW_PLUGIN=%WILLOW_HOME%\plugin
:gotPlugIn
rem Get standard environment variables
if exist "%WILLOW_HOME%\bin\setenv.bat" call "%WILLOW_HOME%\bin\setenv.bat"

rem Get standard Java environment variables
if exist "%WILLOW_HOME%\bin\setclasspath.bat" goto okSetclasspath
echo Cannot find %WILLOW_HOME%\bin\setclasspath.bat
echo This file is needed to run this program
goto end
:okSetclasspath
set BASEDIR=%WILLOW_HOME%
call "%WILLOW_HOME%\bin\setclasspath.bat"
set CLASSPATH=%CLASSPATH%;%WILLOW_HOME%\bin\bootstrap.jar

set CLASSPATH=%CLASSPATH%;%WILLOW_PLUGIN%\classes
for %%i in ("%WILLOW_PLUGIN%\lib\*.jar") do call "%WILLOW_HOME%\bin\cpappend.bat" %%i

if not "%WILLOW_TMPDIR%" == "" goto gotTmpdir
set WILLOW_TMPDIR=%WILLOW_HOME%\temp
:gotTmpdir

rem ----- Execute The Requested Command ---------------------------------------

echo Using WILLOW_HOME:   %WILLOW_HOME%
echo Using WILLOW_TMPDIR: %WILLOW_TMPDIR%
echo Using WILLOW_PLUGIN: %WILLOW_PLUGIN%
echo Using JAVA_HOME:     %JAVA_HOME%
echo Using CLASSPATH:     %CLASSPATH%

set _EXECJAVA=%_RUNJAVA%
set MAINCLASS=org.huihoo.willow.bootstrap.Bootstrap
set ACTION=start

if ""%1"" == ""embedded"" goto doEmbedded
if ""%1"" == ""run"" goto doRun
if ""%1"" == ""start"" goto doStart
if ""%1"" == ""stop"" goto doStop

echo Usage:  willow ( commands ... )
echo commands:
echo   embedded          Start Willow in embedded mode
echo   run               Start Willow in the current window
echo   start             Start Willow in a separate window
echo   stop              Stop Willow
goto end

:doEmbedded
shift
set MAINCLASS=org.huihoo.willow.startup.Embedded
goto execCmd

:doRun
shift
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set SECURITY_POLICY_FILE=%WILLOW_HOME%\conf\willow.policy
goto execCmd

:doStart
shift
if not "%OS%" == "Windows_NT" goto noTitle
rem set _EXECJAVA=start "Willow" %_RUNJAVA%
set _EXECJAVA=%_RUNJAVA%
goto gotTitle
:noTitle
rem set _EXECJAVA=start %_RUNJAVA%
set _EXECJAVA=%_RUNJAVA%
:gotTitle
if not ""%1"" == ""-security"" goto execCmd
shift

:doStop
shift
set ACTION=stop
goto execCmd

:execCmd
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

rem Execute Java with the applicable properties

%_EXECJAVA% %JAVA_OPTS% %WILLOW_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dwillow.home="%WILLOW_HOME%" -Dwebserver.factory=org.huihoo.willow.tomcat.TomcatServer -Djava.io.tmpdir="%WILLOW_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%

goto end

:end
