@echo off
setlocal

set "SCRIPT_DIR=%~dp0"
for %%I in ("%SCRIPT_DIR%..") do set "APP_HOME=%%~fI"
set "CONFIG_FILE=%APP_HOME%\conf\castara.conf"

if exist "%CONFIG_FILE%" (
	for /f "usebackq eol=# tokens=1* delims==" %%A in ("%CONFIG_FILE%") do (
		if not "%%~A"=="" set "%%~A=%%~B"
	)
)

if not defined MEDIA_SERVER_PORT set "MEDIA_SERVER_PORT=8181"
if not defined MEDIA_SERVER_BROWSER_HOST set "MEDIA_SERVER_BROWSER_HOST=127.0.0.1"
if not defined MEDIA_SERVER_OPEN_BROWSER set "MEDIA_SERVER_OPEN_BROWSER=true"

set "JAVA_EXE=java.exe"
if defined JAVA_HOME set "JAVA_EXE=%JAVA_HOME%\bin\java.exe"

set "JAVA_ARGS=-Dmedia.server.port=%MEDIA_SERVER_PORT% -Dmedia.server.browserHost=%MEDIA_SERVER_BROWSER_HOST% -Dmedia.server.openBrowser=%MEDIA_SERVER_OPEN_BROWSER%"
if defined MEDIA_SERVER_BIND_HOST set "JAVA_ARGS=%JAVA_ARGS% -Dmedia.server.bindHost=%MEDIA_SERVER_BIND_HOST%"
if defined MEDIA_SERVER_ADVERTISED_HOST set "JAVA_ARGS=%JAVA_ARGS% -Dmedia.server.advertisedHost=%MEDIA_SERVER_ADVERTISED_HOST%"

"%JAVA_EXE%" %JAVA_ARGS% -cp "%APP_HOME%\lib\*" com.maa.mediaserver.server.MainKt
endlocal