set SCRIPT=%0

rem determine Elasticsearch home; to do this, we strip from the path until we
rem find bin, and then strip bin (there is an assumption here that there is no
rem nested directory under bin also named bin)
for %%I in (%SCRIPT%) do set ES_HOME=%%~dpI

:es_home_loop
for %%I in ("%ES_HOME:~1,-1%") do set DIRNAME=%%~nxI
if not "%DIRNAME%" == "bin" (
  for %%I in ("%ES_HOME%..") do set ES_HOME=%%~dpfI
  goto es_home_loop
)
for %%I in ("%ES_HOME%..") do set ES_HOME=%%~dpfI

rem now set the classpath
set ES_CLASSPATH=!ES_HOME!\lib\*

rem ====== FusionAuth Stuff ======

set FUSIONAUTH_PLUGIN_DIR=%ES_HOME%\..\..\plugins
set FUSIONAUTH_CONFIG_DIR=%ES_HOME%\..\..\config
set FUSIONAUTH_JAVA_DIR=%ES_HOME%\..\..\java
set FUSIONAUTH_LOG_DIR=%ES_HOME%\..\..\logs
set ES_JAVA_OPTS=-XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfusionauth.home.directory=%ES_HOME%/.. -Dfusionauth.config.directory=%FUSIONAUTH_CONFIG_DIR% -Dfusionauth.plugin.directory=%FUSIONAUTH_PLUGIN_DIR% -Dlog4j2.disable.jmx=true -Djava.awt.headless=true

set JAVA_VERSION=14.0.1
set JAVA_REVISION=7

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

set JAVA_HOME=%FUSIONAUTH_JAVA_DIR%\jdk-%JAVA_VERSION%+%JAVA_REVISION%

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

setlocal enableextensions enabledelayedexpansion

call :resolveConfigurationProperty ELASTICSEARCH_HTTP_PORT not-used fusionauth-search.http-port fusionauth-search.http.port FUSIONAUTHSEARCH_HTTP_PORT FUSIONAUTH_SEARCH_HTTP_PORT
call :resolveConfigurationProperty ELASTICSEARCH_NETWORK_HOSTS not-used not-used fusionauth-search.hosts FUSIONAUTHSEARCH_HOSTS FUSIONAUTH_SEARCH_HOSTS _local_
call :resolveConfigurationProperty ELASTICSEARCH_TRANSPORT_PORT not-used fusionauth-search.transport-port fusionauth-search.transport.port FUSIONAUTHSEARCH_TRANSPORT_PORT FUSIONAUTH_SEARCH_TRANSPORT_PORT
call :resolveConfigurationProperty ES_JAVA_OPTS "-Xmx" not-used fusionauth-search.memory FUSIONAUTHSEARCH_MEMORY FUSIONAUTH_SEARCH_MEMORY
call :resolveConfigurationProperty ES_JAVA_OPTS "-Xms" not-used fusionauth-search.memory FUSIONAUTHSEARCH_MEMORY FUSIONAUTH_SEARCH_MEMORY
call :resolveConfigurationProperty ELASTICSEARCH_DATA_DIRECTORY not-used not-used fusionauth-search.data-directory FUSIONAUTHSEARCH_DATA_DIRECTORY FUSIONAUTH_SEARCH_DATA_DIRECTORY "%ES_HOME%\..\..\data\search\esv6"
call :resolveConfigurationProperty ES_JAVA_OPTS not-used not-used fusionauth-search.additional-java-args FUSIONAUTHSEARCH_ADDITIONAL_JAVA_ARGS FUSIONAUTH_SEARCH_ADDITIONAL_JAVA_ARGS

endlocal & set "ES_JAVA_OPTS=%ES_JAVA_OPTS%" & set "ELASTICSEARCH_HTTP_PORT=%ELASTICSEARCH_HTTP_PORT%" & set "ELASTICSEARCH_SERVERS=%ELASTICSEARCH_SERVERS%" & set "ELASTICSEARCH_TRANSPORT_PORT=%ELASTICSEARCH_TRANSPORT_PORT%" & set "ELASTICSEARCH_DATA_DIRECTORY=%ELASTICSEARCH_DATA_DIRECTORY%" & set "ELASTICSEARCH_NETWORK_HOSTS=%ELASTICSEARCH_NETWORK_HOSTS%"

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

rem ====== End FusionAuth Stuff ======

rem now set the path to java
set JAVA="%JAVA_HOME%\bin\java.exe"
set JAVA_TYPE=JAVA_HOME

if not exist !JAVA! (
  echo "could not find java in !JAVA_TYPE! at !JAVA!" >&2
  exit /b 1
)

set HOSTNAME=%COMPUTERNAME%

if not defined ES_PATH_CONF (
  set ES_PATH_CONF=!ES_HOME!\config
)

rem now make ES_PATH_CONF absolute
for %%I in ("%ES_PATH_CONF%..") do set ES_PATH_CONF=%%~dpfI

set ES_DISTRIBUTION_FLAVOR=oss
set ES_DISTRIBUTION_TYPE=tar
set ES_BUNDLED_JDK=false
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
set default=%~7

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 "tokens=* eol=# delims=" %%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
        )
      )
    )
  )
)

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