@echo off
REM
REM FusionAuth environment setup. This script parses the fusionauth.properties files to set JAVA_OPTS.
REM

set FUSIONAUTH_PLUGIN_DIR=%CATALINA_HOME%\..\..\plugins
set FUSIONAUTH_CONFIG_DIR=%CATALINA_HOME%\..\..\config
set FUSIONAUTH_JAVA_DIR=%CATALINA_HOME%\..\..\java
set FUSIONAUTH_LOG_DIR=%CATALINA_HOME%\..\..\logs
set CATALINA_OUT=%FUSIONAUTH_LOG_DIR%\fusionauth-app.log
set CATALINA_OPTS=-Dfusionauth.home.directory=%CATALINA_HOME%\.. -Dfusionauth.config.directory=%FUSIONAUTH_CONFIG_DIR% -Dfusionauth.log.directory=%FUSIONAUTH_LOG_DIR% -Dfusionauth.plugin.directory=%FUSIONAUTH_PLUGIN_DIR%

set JAVA_VERSION=14.0.1
set JAVA_REVISION=7

Powershell.exe -executionpolicy ByPass -File %CATALINA_HOME%\bin\install_java.ps1

set JAVA_HOME=%FUSIONAUTH_JAVA_DIR%\jdk-%JAVA_VERSION%+%JAVA_REVISION%
set JAVA_OPTS=-Djava.awt.headless=true -Dcom.sun.org.apache.xml.internal.security.ignoreLineBreaks=true -Dnashorn.args=--no-deprecation-warning --enable-preview

if not exist "%FUSIONAUTH_LOG_DIR%" (
  mkdir "%FUSIONAUTH_LOG_DIR%"
)

if not exist "%CATALINA_HOME%\logs" (
  mkdir "%CATALINA_HOME%\logs"
)

setlocal enableextensions enabledelayedexpansion

call :resolveConfigurationProperty JAVA_OPTS "-Dfusionauth-app.http.port=" fusionauth-app.http-port fusionauth-app.http.port FUSIONAUTH_HTTP_PORT FUSIONAUTH_APP_HTTP_PORT
call :resolveConfigurationProperty JAVA_OPTS "-Dfusionauth-app.https.port=" fusionauth-app.https-port fusionauth-app.https.port FUSIONAUTH_HTTPS_PORT FUSIONAUTH_APP_HTTPS_PORT
call :resolveConfigurationProperty JAVA_OPTS "-Dfusionauth-app.ajp.port=" fusionauth-app.ajp-port fusionauth-app.ajp.port FUSIONAUTH_AJP_PORT FUSIONAUTH_APP_AJP_PORT
call :resolveConfigurationProperty JAVA_OPTS "-Dfusionauth-app.management.port=" fusionauth-app.management-port fusionauth-app.management.port FUSIONAUTH_MANAGEMENT_PORT FUSIONAUTH_APP_MANAGEMENT_PORT
call :resolveConfigurationProperty CATALINA_OPTS "-Xmx" not-used fusionauth-app.memory FUSIONAUTH_MEMORY FUSIONAUTH_APP_MEMORY
call :resolveConfigurationProperty CATALINA_OPTS "-Xms" not-used fusionauth-app.memory FUSIONAUTH_MEMORY FUSIONAUTH_APP_MEMORY
call :resolveConfigurationProperty CATALINA_OPTS not-used not-used fusionauth-app.additional-java-args FUSIONAUTH_ADDITIONAL_JAVA_ARGS FUSIONAUTH_APP_ADDITIONAL_JAVA_ARGS
call :resolveConfigurationProperty JAVA_OPTS "-Dfusionauth-app.http.cookie.same-site-policy=" fusionauth-app.cookie-same-site-policy fusionauth-app.http.cookie.same-site-policy FUSIONAUTH_COOKIE_SAME_SITE_POLICY FUSIONAUTH_APP_HTTP_COOKIE_SAME_SITE_POLICY
call :resolveConfigurationProperty JAVA_OPTS "-Dfusionauth-app.http.max-header-size=" fusionauth-app.http-max-header-size fusionauth-app.http.max-header-size FUSIONAUTH_HTTP_MAX_HEADER_SIZE FUSIONAUTH_APP_HTTP_MAX_HEADER_SIZE
call :resolveConfigurationProperty JAVA_OPTS "-Dfusionauth-app.http.relaxed-path-chars=" not-used fusionauth-app.http.relaxed-path-chars not-used FUSIONAUTH_APP_HTTP_RELAXED_PATH_CHARS
call :resolveConfigurationProperty JAVA_OPTS "-Dfusionauth-app.http.relaxed-query-chars=" not-used fusionauth-app.http.relaxed-query-chars not-used FUSIONAUTH_APP_HTTP_RELAXED_QUERY_CHARS

endlocal & set "JAVA_OPTS=%JAVA_OPTS%" & set "CATALINA_OPTS=%CATALINA_OPTS%"
goto:EOF


REM ====== FusionAuth functions ======

:appendToVariable
set varName=%~1
set value=%~2
if not "!%varName%!" == "" (
  set %varName%=!%varName%! %value%
) else (
  set %varName%=%value%
)
goto:EOF

:resolveConfigurationProperty
set envVarName=%~1
set cmdOption=%~2
set oldName=%~3
set newName=%~4
set oldEnvName=%~5
set newEnvName=%~6

if not "!%newEnvName%!" == "" (
  call :appendToVariable %envVarName% "!cmdOption:not-used=!!%newEnvName%!"
  goto:EOF
)

if not "!%oldEnvName%!" == "" (
  echo You are using a deprecated environment variable of [!oldEnvName!]. The new variable name is [!newEnvName!]
  call :appendToVariable %envVarName% "!cmdOption:not-used=!!%oldEnvName%!"
  goto:EOF
)

if exist "%FUSIONAUTH_CONFIG_DIR%\fusionauth.properties" (
  for /F "eol=#" %%L in (%FUSIONAUTH_CONFIG_DIR%\fusionauth.properties) do (
    for /F "tokens=1* delims==" %%A in ("%%L") do (
      if not "%%B" == "" (
        if "%%A" == "%newName%" (
          call :appendToVariable %envVarName% "!cmdOption:not-used=!%%B"
          goto:EOF
        )
        if "%%A" == "%oldName%" (
          echo You are using a deprecated configuration property of [!oldName!] in [fusionauth.properties]. The new variable name is [!newName!]
          call :appendToVariable %envVarName% "!cmdOption:not-used=!%%B"
          goto:EOF
        )
      )
    )
  )
)

goto:EOF