@echo off
SETLOCAL ENABLEEXTENSIONS

if EXIST batch.log del batch.log

set runmode=standalone >> batch.log

if "%~1"=="" goto standalone
if NOT "%~1"=="service" goto standalone
set runmode=service
:standalone

echo startup.bat: runmode=%runmode% >> batch.log

call setenv.bat %runmode%
call wowzaversion.bat

if not %WMSENVOK% == "true" goto end

set _WINDOWNAME="Wowza Streaming Engine 4.0"
set _EXESERVER=
if "%1"=="newwindow" (
set _EXESERVER=start %_WINDOWNAME%
shift
)

set CLASSPATH="%WMSAPP_HOME%\bin\wms-bootstrap.jar"
set CLASSPATH=%CLASSPATH:\=/%
set CLASSPATH=%CLASSPATH://=/%
   
set TUNEPATH="%WMSAPP_HOME%\bin\tune.bat" %1
set TUNEPATH=%TUNEPATH:\\=\%
set TUNEPATH=%TUNEPATH:\=/%

rem cacls jmxremote.password /P username:R
rem cacls jmxremote.access /P username:R

set JMXOPTIONS=-Dcom.sun.management.jmxremote=true

rem log interceptor com.wowza.wms.logging.LogNotify - see Javadocs for ILogNotify
:RESTART

for /f "delims=" %%i in ('%TUNEPATH%') do set WMSTUNE_OPTS=%%i

rem echo Tuning: %WMSTUNE_OPTS%
echo startup.bat: Tuning Opts: "%WMSTUNE_OPTS%" >> batch.log

%_EXESERVER% "%_EXECJAVA%" %WMSTUNE_OPTS% %JMXOPTIONS% -Dcom.wowza.wms.runmode="%runmode%" -Dcom.wowza.wms.native.base="win" -Dcom.wowza.wms.ConfigURL="%WMSCONFIG_URL%" -cp %CLASSPATH% com.wowza.wms.bootstrap.Bootstrap start
set EVAL=%ERRORLEVEL%

IF %EVAL%==10 GOTO BADLICENSEKEY
:LICENSERESTART
IF %EVAL%==400 GOTO RESTART

echo startup.bat java returned: %EVAL% >> batch.log

set serviceName=%WowzaVersion: =%
set serviceName=%serviceName:.=%
set serviceName=%serviceName:-=%
sc stop %serviceName% > NUL
EXIT /B %EVAL%
GOTO end

:BADLICENSEKEY
%_EXESERVER% "%_EXECJAVA%" -cp "%WMSAPP_HOME%\lib\slf4j-api-1.6.4.jar;%WMSAPP_HOME%\lib\slf4j-log4j12-1.6.4.jar;%WMSAPP_HOME%\lib\wms-xstream-1.4.7.jar;%WMSAPP_HOME%\lib\wms-jax.validation.jar;%WMSAPP_HOME%\lib\wms-core.jar;%WMSAPP_HOME%\lib\bcprov-jdk15on-149.jar;%WMSAPP_HOME%\lib\commons-lang-2.6.jar;%WMSAPP_HOME%\lib\wms-license-rest.jar;%WMSAPP_HOME%\lib\wms-rest.jar;%WMSAPP_HOME%\lib\wms-server.jar;%WMSAPP_HOME%\lib\wms-bootstrap.jar;%WMSAPP_HOME%\lib\wms-restlet-2.1.6.jar;%WMSAPP_HOME%\lib\wms-restlet-2.1.6.org.restlet.ext.simple.jar;%WMSAPP_HOME%\lib\log4j-1.2.17.jar"  -Dcom.wowza.wms.runmode="%runmode%" -Dcom.wowza.wms.native.base="win" -Dcom.wowza.wms.ConfigHome="%WMSCONFIG_HOME%" com.wowza.wms.standalone.rest.LicenseUpdateServer
set EVAL=%ERRORLEVEL%
GOTO :LICENSERESTART

:end
