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=1.8.0
set JAVA_REVISION=171

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

set JAVA_HOME=%FUSIONAUTH_JAVA_DIR%\jre%JAVA_VERSION%_%JAVA_REVISION%

@setlocal enableextensions enabledelayedexpansion

rem default ELASTICSEARCH_NETWORK_HOSTS
set ELASTICSEARCH_NETWORK_HOSTS=_local_

rem Default directory to ..\..\data\search\esv<major version> for index compatibility
set ELASTICSEARCH_DATA_DIRECTORY=%ES_HOME%\..\..\data\search\esv6

if exist "%FUSIONAUTH_CONFIG_DIR%\fusionauth.properties" (
  for /F "eol=#" %%L in (%FUSIONAUTH_CONFIG_DIR%\fusionauth.properties) do (
    for /F "delims== tokens=1,2" %%A in ("%%L") do (
      set name=%%A
      set value=%%B
    )

    rem echo first!name!=!value!

    if "!name:.http-port=!" == "fusionauth-search" (
      set ELASTICSEARCH_HTTP_PORT=!value!
    )

    if "!name:.servers=!" == "fusionauth-search" (
      set ELASTICSEARCH_SERVERS=!value!
    )

    rem Optional, do not set if not defined, default value is assigned above
    if "!name:.hosts=!" == "fusionauth-search" (
      IF NOT [!value!] == [] (
        set ELASTICSEARCH_NETWORK_HOSTS=!value!
      )
    )

    if "!name:.transport-port=!" == "fusionauth-search" (
      set ELASTICSEARCH_TRANSPORT_PORT=!value!
    )

    rem Optional, do not set if not defined, default value is assigned above
    if "!name:.data-directory=!" == "fusionauth-search" (
      IF NOT [!value!] == [] (
        set ELASTICSEARCH_DATA_DIRECTORY=!value!
      )
    )

    if "!name:.memory=!" == "fusionauth-search" (
      set ES_JAVA_OPTS=!ES_JAVA_OPTS! -Xmx!value! -Xms!value!
    )

    if "!name:.additional-java-args=!" == "fusionauth-search" (
      call :extractAdditionalArgs "%%L" "!name!"
    )
  )
)

goto :fusionauthConfigDone

:extractAdditionalArgs
set line=%1
set replacement=%2
set line=%line:~1,-1%
set replacement=%replacement:~1,-1%
set line=!line:%replacement%=!
set line=%line:~1%
set ES_JAVA_OPTS=!ES_JAVA_OPTS! %line%
goto :eof

:fusionauthConfigDone

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
if defined JAVA_HOME (
  set JAVA="%JAVA_HOME%\bin\java.exe"
) else (
  for %%I in (java.exe) do set JAVA="%%~$PATH:I"
)

if not exist %JAVA% (
  echo could not find java; set JAVA_HOME or ensure java is in PATH 1>&2
  exit /b 1
)

rem do not let JAVA_TOOL_OPTIONS slip in (as the JVM does by default)
if defined JAVA_TOOL_OPTIONS (
  echo warning: ignoring JAVA_TOOL_OPTIONS=%JAVA_TOOL_OPTIONS%
  set JAVA_TOOL_OPTIONS=
)

rem JAVA_OPTS is not a built-in JVM mechanism but some people think it is so we
rem warn them that we are not observing the value of %JAVA_OPTS%
if defined JAVA_OPTS (
  (echo|set /p=warning: ignoring JAVA_OPTS=%JAVA_OPTS%; )
  echo pass JVM parameters via ES_JAVA_OPTS
)

rem check the Java version
rem Comment out because we are controlling the version of Java and it will mess up our Windows PS service script.
rem We expect the java process to be the one that stars up ElasticSearch
rem %JAVA% -cp "%ES_CLASSPATH%" "org.elasticsearch.tools.launchers.JavaVersionChecker" || 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=default
set ES_DISTRIBUTION_TYPE=tar

if not defined ES_TMPDIR (
  set ES_TMPDIR=!TMP!\elasticsearch
  if not exist "!ES_TMPDIR!" (
    mkdir "!ES_TMPDIR!"
  )
)
