@REM cortex launcher script
@REM
@REM Environment:
@REM JAVA_HOME - location of a JDK home dir (optional if java on path)
@REM CFG_OPTS  - JVM options (optional)
@REM Configuration:
@REM CORTEX_config.txt found in the CORTEX_HOME.
@setlocal enabledelayedexpansion
@setlocal enableextensions

@echo off


if "%CORTEX_HOME%"=="" (
  set "APP_HOME=%~dp0\\.."

  rem Also set the old env name for backwards compatibility
  set "CORTEX_HOME=%~dp0\\.."
) else (
  set "APP_HOME=%CORTEX_HOME%"
)

set "APP_LIB_DIR=%APP_HOME%\lib\"

rem Detect if we were double clicked, although theoretically A user could
rem manually run cmd /c
for %%x in (!cmdcmdline!) do if %%~x==/c set DOUBLECLICKED=1

rem FIRST we load the config file of extra options.
set "CFG_FILE=%APP_HOME%\CORTEX_config.txt"
set CFG_OPTS=
call :parse_config "%CFG_FILE%" CFG_OPTS

rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
set _JAVA_OPTS=%JAVA_OPTS%
if "!_JAVA_OPTS!"=="" set _JAVA_OPTS=!CFG_OPTS!

rem We keep in _JAVA_PARAMS all -J-prefixed and -D-prefixed arguments
rem "-J" is stripped, "-D" is left as is, and everything is appended to JAVA_OPTS
set _JAVA_PARAMS=
set _APP_ARGS=

set "APP_CLASSPATH=%APP_LIB_DIR%\..\conf\;%APP_LIB_DIR%\org.thehive-project.cortex-4.1.0-1-sans-externalized.jar;%APP_LIB_DIR%\elastic4play.elastic4play-4.1.0-1.jar;%APP_LIB_DIR%\org.scala-lang.scala-library-2.13.17.jar;%APP_LIB_DIR%\org.playframework.twirl.twirl-api_2.13-2.0.9.jar;%APP_LIB_DIR%\org.playframework.play-server_2.13-3.0.9.jar;%APP_LIB_DIR%\org.playframework.play-logback_2.13-3.0.9.jar;%APP_LIB_DIR%\org.playframework.play-pekko-http-server_2.13-3.0.9.jar;%APP_LIB_DIR%\org.playframework.play-filters-helpers_2.13-3.0.9.jar;%APP_LIB_DIR%\org.apache.logging.log4j.log4j-to-slf4j-2.17.0.jar;%APP_LIB_DIR%\org.playframework.play-caffeine-cache_2.13-3.0.9.jar;%APP_LIB_DIR%\org.playframework.play-ws_2.13-3.0.9.jar;%APP_LIB_DIR%\org.playframework.play-ahc-ws_2.13-3.0.9.jar;%APP_LIB_DIR%\org.playframework.play-guice_2.13-3.0.9.jar;%APP_LIB_DIR%\net.codingwell.scala-guice_2.13-6.0.0.jar;%APP_LIB_DIR%\org.reflections.reflections-0.10.2.jar;%APP_LIB_DIR%\net.lingala.zip4j.zip4j-2.11.5.jar;%APP_LIB_DIR%\com.github.docker-java.docker-java-core-3.6.0.jar;%APP_LIB_DIR%\com.github.docker-java.docker-java-transport-zerodep-3.6.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-client-7.4.0.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-cluster_2.13-1.2.1.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-cluster-typed_2.13-1.2.1.jar;%APP_LIB_DIR%\org.playframework.play-cache_2.13-3.0.9.jar;%APP_LIB_DIR%\nl.gn0s1s.elastic4s-core_2.13-8.19.0.jar;%APP_LIB_DIR%\nl.gn0s1s.elastic4s-reactivestreams-pekko_2.13-8.19.0.jar;%APP_LIB_DIR%\nl.gn0s1s.elastic4s-client-esjava_2.13-8.19.0.jar;%APP_LIB_DIR%\org.scalactic.scalactic_2.13-3.2.19.jar;%APP_LIB_DIR%\at.yawk.lz4.lz4-java-1.11.0.jar;%APP_LIB_DIR%\org.scala-lang.modules.scala-xml_2.13-2.2.0.jar;%APP_LIB_DIR%\org.playframework.play_2.13-3.0.9.jar;%APP_LIB_DIR%\ch.qos.logback.logback-classic-1.5.32.jar;%APP_LIB_DIR%\org.playframework.play-streams_2.13-3.0.9.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-http-core_2.13-1.0.1.jar;%APP_LIB_DIR%\org.slf4j.slf4j-api-2.0.17.jar;%APP_LIB_DIR%\org.apache.logging.log4j.log4j-api-2.17.0.jar;%APP_LIB_DIR%\com.github.ben-manes.caffeine.caffeine-3.1.8.jar;%APP_LIB_DIR%\com.github.ben-manes.caffeine.jcache-3.1.8.jar;%APP_LIB_DIR%\javax.cache.cache-api-1.1.1.jar;%APP_LIB_DIR%\org.playframework.play-ws-standalone_2.13-3.0.8.jar;%APP_LIB_DIR%\org.playframework.play-ws-standalone-xml_2.13-3.0.8.jar;%APP_LIB_DIR%\org.playframework.play-ws-standalone-json_2.13-3.0.8.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-stream_2.13-1.2.1.jar;%APP_LIB_DIR%\org.playframework.play-ahc-ws-standalone_2.13-3.0.8.jar;%APP_LIB_DIR%\org.playframework.shaded-asynchttpclient-3.0.8.jar;%APP_LIB_DIR%\org.playframework.shaded-oauth-3.0.8.jar;%APP_LIB_DIR%\com.google.inject.guice-6.0.0.jar;%APP_LIB_DIR%\com.google.inject.extensions.guice-assistedinject-6.0.0.jar;%APP_LIB_DIR%\com.google.code.findbugs.jsr305-3.0.2.jar;%APP_LIB_DIR%\org.scala-lang.modules.scala-collection-compat_2.13-2.1.2.jar;%APP_LIB_DIR%\org.scala-lang.scala-reflect-2.13.17.jar;%APP_LIB_DIR%\org.javassist.javassist-3.28.0-GA.jar;%APP_LIB_DIR%\com.github.docker-java.docker-java-api-3.6.0.jar;%APP_LIB_DIR%\com.github.docker-java.docker-java-transport-3.6.0.jar;%APP_LIB_DIR%\commons-io.commons-io-2.20.0.jar;%APP_LIB_DIR%\org.apache.commons.commons-compress-1.28.0.jar;%APP_LIB_DIR%\org.apache.commons.commons-lang3-3.18.0.jar;%APP_LIB_DIR%\com.fasterxml.jackson.core.jackson-databind-2.22.0.jar;%APP_LIB_DIR%\com.google.guava.guava-32.1.1-jre.jar;%APP_LIB_DIR%\org.bouncycastle.bcpkix-jdk18on-1.84.jar;%APP_LIB_DIR%\net.java.dev.jna.jna-5.17.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-client-api-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-httpclient-vertx-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.zjsonpatch-7.4.0.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-remote_2.13-1.2.1.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-coordination_2.13-1.2.1.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-actor-typed_2.13-1.2.1.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-cluster-tools_2.13-1.2.1.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-distributed-data_2.13-1.2.1.jar;%APP_LIB_DIR%\nl.gn0s1s.elastic4s-domain_2.13-8.19.0.jar;%APP_LIB_DIR%\nl.gn0s1s.elastic4s-client-core_2.13-8.19.0.jar;%APP_LIB_DIR%\nl.gn0s1s.elastic4s-handlers_2.13-8.19.0.jar;%APP_LIB_DIR%\nl.gn0s1s.elastic4s-json-builder_2.13-8.19.0.jar;%APP_LIB_DIR%\com.typesafe.config-1.4.5.jar;%APP_LIB_DIR%\com.fasterxml.jackson.core.jackson-core-2.22.0.jar;%APP_LIB_DIR%\com.fasterxml.jackson.module.jackson-module-scala_2.13-2.22.0.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-actor_2.13-1.2.1.jar;%APP_LIB_DIR%\org.elasticsearch.client.elasticsearch-rest-client-8.19.0.jar;%APP_LIB_DIR%\org.playframework.play-build-link-3.0.9.jar;%APP_LIB_DIR%\org.playframework.play-configuration_2.13-3.0.9.jar;%APP_LIB_DIR%\org.slf4j.jul-to-slf4j-2.0.17.jar;%APP_LIB_DIR%\org.slf4j.jcl-over-slf4j-2.0.17.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-slf4j_2.13-1.2.1.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-serialization-jackson_2.13-1.2.1.jar;%APP_LIB_DIR%\com.fasterxml.jackson.core.jackson-annotations-2.22.jar;%APP_LIB_DIR%\com.fasterxml.jackson.datatype.jackson-datatype-jdk8-2.22.0.jar;%APP_LIB_DIR%\com.fasterxml.jackson.datatype.jackson-datatype-jsr310-2.22.0.jar;%APP_LIB_DIR%\com.fasterxml.jackson.dataformat.jackson-dataformat-cbor-2.22.0.jar;%APP_LIB_DIR%\com.fasterxml.jackson.module.jackson-module-parameter-names-2.22.0.jar;%APP_LIB_DIR%\io.jsonwebtoken.jjwt-api-0.11.5.jar;%APP_LIB_DIR%\io.jsonwebtoken.jjwt-impl-0.11.5.jar;%APP_LIB_DIR%\io.jsonwebtoken.jjwt-jackson-0.11.5.jar;%APP_LIB_DIR%\org.playframework.play-json_2.13-3.0.5.jar;%APP_LIB_DIR%\javax.inject.javax.inject-1.jar;%APP_LIB_DIR%\com.typesafe.ssl-config-core_2.13-0.6.1.jar;%APP_LIB_DIR%\org.scala-lang.modules.scala-parser-combinators_2.13-1.1.2.jar;%APP_LIB_DIR%\ch.qos.logback.logback-core-1.5.32.jar;%APP_LIB_DIR%\org.reactivestreams.reactive-streams-1.0.4.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-parsing_2.13-1.0.1.jar;%APP_LIB_DIR%\org.parboiled.parboiled_2.13-2.5.0.jar;%APP_LIB_DIR%\org.checkerframework.checker-qual-3.37.0.jar;%APP_LIB_DIR%\com.google.errorprone.error_prone_annotations-2.21.1.jar;%APP_LIB_DIR%\org.osgi.org.osgi.service.component.annotations-1.5.1.jar;%APP_LIB_DIR%\jakarta.inject.jakarta.inject-api-2.0.1.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-protobuf-v3_2.13-1.2.1.jar;%APP_LIB_DIR%\org.playframework.cachecontrol_2.13-3.0.1.jar;%APP_LIB_DIR%\aopalliance.aopalliance-1.0.jar;%APP_LIB_DIR%\commons-codec.commons-codec-1.19.0.jar;%APP_LIB_DIR%\com.google.guava.failureaccess-1.0.1.jar;%APP_LIB_DIR%\com.google.guava.listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;%APP_LIB_DIR%\com.google.j2objc.j2objc-annotations-2.8.jar;%APP_LIB_DIR%\org.bouncycastle.bcutil-jdk18on-1.84.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-core-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-gatewayapi-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-resource-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-rbac-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-admissionregistration-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-apps-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-autoscaling-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-apiextensions-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-batch-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-certificates-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-coordination-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-discovery-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-events-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-extensions-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-flowcontrol-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-networking-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-metrics-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-policy-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-scheduling-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-storageclass-7.4.0.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-node-7.4.0.jar;%APP_LIB_DIR%\org.snakeyaml.snakeyaml-engine-2.10.jar;%APP_LIB_DIR%\com.fasterxml.jackson.dataformat.jackson-dataformat-yaml-2.22.0.jar;%APP_LIB_DIR%\io.vertx.vertx-core-4.5.27.jar;%APP_LIB_DIR%\io.vertx.vertx-web-client-4.5.27.jar;%APP_LIB_DIR%\org.apache.pekko.pekko-pki_2.13-1.2.1.jar;%APP_LIB_DIR%\org.agrona.agrona-1.22.0.jar;%APP_LIB_DIR%\org.lmdbjava.lmdbjava-0.9.1.jar;%APP_LIB_DIR%\com.thoughtworks.paranamer.paranamer-2.8.3.jar;%APP_LIB_DIR%\org.apache.httpcomponents.httpclient-4.5.14.jar;%APP_LIB_DIR%\org.apache.httpcomponents.httpcore-4.4.16.jar;%APP_LIB_DIR%\org.apache.httpcomponents.httpasyncclient-4.1.5.jar;%APP_LIB_DIR%\org.apache.httpcomponents.httpcore-nio-4.4.16.jar;%APP_LIB_DIR%\commons-logging.commons-logging-1.2.jar;%APP_LIB_DIR%\org.playframework.play-exceptions-3.0.9.jar;%APP_LIB_DIR%\org.playframework.play-functional_2.13-3.0.5.jar;%APP_LIB_DIR%\org.osgi.osgi.annotation-8.1.0.jar;%APP_LIB_DIR%\org.osgi.org.osgi.namespace.extender-1.0.1.jar;%APP_LIB_DIR%\org.osgi.org.osgi.util.function-1.0.0.jar;%APP_LIB_DIR%\org.osgi.org.osgi.util.promise-1.0.0.jar;%APP_LIB_DIR%\org.bouncycastle.bcprov-jdk18on-1.84.jar;%APP_LIB_DIR%\io.fabric8.kubernetes-model-common-7.4.0.jar;%APP_LIB_DIR%\org.yaml.snakeyaml-2.5.jar;%APP_LIB_DIR%\io.netty.netty-common-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-buffer-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-transport-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-handler-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-handler-proxy-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-codec-http-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-codec-http2-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-resolver-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-resolver-dns-4.1.135.Final.jar;%APP_LIB_DIR%\io.vertx.vertx-web-common-4.5.27.jar;%APP_LIB_DIR%\io.vertx.vertx-auth-common-4.5.27.jar;%APP_LIB_DIR%\com.hierynomus.asn-one-0.6.0.jar;%APP_LIB_DIR%\com.github.jnr.jnr-constants-0.10.4.jar;%APP_LIB_DIR%\com.github.jnr.jnr-ffi-2.2.13.jar;%APP_LIB_DIR%\io.netty.netty-transport-native-unix-common-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-codec-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-codec-socks-4.1.135.Final.jar;%APP_LIB_DIR%\io.netty.netty-codec-dns-4.1.135.Final.jar;%APP_LIB_DIR%\com.github.jnr.jffi-1.3.11.jar;%APP_LIB_DIR%\com.github.jnr.jffi-1.3.11-native.jar;%APP_LIB_DIR%\org.ow2.asm.asm-9.2.jar;%APP_LIB_DIR%\org.ow2.asm.asm-commons-9.2.jar;%APP_LIB_DIR%\org.ow2.asm.asm-analysis-9.2.jar;%APP_LIB_DIR%\org.ow2.asm.asm-tree-9.2.jar;%APP_LIB_DIR%\org.ow2.asm.asm-util-9.2.jar;%APP_LIB_DIR%\com.github.jnr.jnr-a64asm-1.0.0.jar;%APP_LIB_DIR%\com.github.jnr.jnr-x86asm-1.0.2.jar;%APP_LIB_DIR%\org.thehive-project.cortex-4.1.0-1-assets.jar"
set "APP_MAIN_CLASS=play.core.server.ProdServerStart"
set "SCRIPT_CONF_FILE=%APP_HOME%\conf\application.ini"

rem Bundled JRE has priority over standard environment variables
if defined BUNDLED_JVM (
  set "_JAVACMD=%BUNDLED_JVM%\bin\java.exe"
) else (
  if "%JAVACMD%" neq "" (
    set "_JAVACMD=%JAVACMD%"
  ) else (
    if "%JAVA_HOME%" neq "" (
      if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
    )
  )
)

if "%_JAVACMD%"=="" set _JAVACMD=java

rem if configuration files exist, prepend their contents to the script arguments so it can be processed by this runner
call :parse_config "%SCRIPT_CONF_FILE%" SCRIPT_CONF_ARGS

call :process_args %SCRIPT_CONF_ARGS% %%*

rem Detect if this java is ok to use.
for /F %%j in ('"%_JAVACMD%" -version  2^>^&1') do (
  if %%~j==java set JAVAINSTALLED=1
  if %%~j==openjdk set JAVAINSTALLED=1
)

rem BAT has no logical or, so we do it OLD SCHOOL! Oppan Redmond Style
set JAVAOK=true
if not defined JAVAINSTALLED set JAVAOK=false

if "%JAVAOK%"=="false" (
  echo.
  echo A Java JDK is not installed or can't be found.
  if not "%JAVA_HOME%"=="" (
    echo JAVA_HOME = "%JAVA_HOME%"
  )
  echo.
  echo Please go to
  echo   http://www.oracle.com/technetwork/java/javase/downloads/index.html
  echo and download a valid Java JDK and install before running cortex.
  echo.
  echo If you think this message is in error, please check
  echo your environment variables to see if "java.exe" and "javac.exe" are
  echo available via JAVA_HOME or PATH.
  echo.
  if defined DOUBLECLICKED pause
  exit /B 1
)

set _JAVA_OPTS=!_JAVA_OPTS! !_JAVA_PARAMS!

if defined CUSTOM_MAIN_CLASS (
    set MAIN_CLASS=!CUSTOM_MAIN_CLASS!
) else (
    set MAIN_CLASS=!APP_MAIN_CLASS!
)

rem Call the application and pass all arguments unchanged.
"%_JAVACMD%" !_JAVA_OPTS! !CORTEX_OPTS! -cp "%APP_CLASSPATH%" %MAIN_CLASS% !_APP_ARGS!

@endlocal

exit /B %ERRORLEVEL%


rem Loads a configuration file full of default command line options for this script.
rem First argument is the path to the config file.
rem Second argument is the name of the environment variable to write to.
:parse_config
  set _PARSE_FILE=%~1
  set _PARSE_OUT=
  if exist "%_PARSE_FILE%" (
    FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%_PARSE_FILE%") DO (
      set _PARSE_OUT=!_PARSE_OUT! %%i
    )
  )
  set %2=!_PARSE_OUT!
exit /B 0


:add_java
  set _JAVA_PARAMS=!_JAVA_PARAMS! %*
exit /B 0


:add_app
  set _APP_ARGS=!_APP_ARGS! %*
exit /B 0


rem Processes incoming arguments and places them in appropriate global variables
:process_args
  :param_loop
  shift
  call set _PARAM1=%%0
  set "_TEST_PARAM=%~0"

  if ["!_PARAM1!"]==[""] goto param_afterloop

  if "!_TEST_PARAM!"=="-main" (
    call set CUSTOM_MAIN_CLASS=%%1
    shift
    goto param_loop
  )

  if "!_TEST_PARAM!"=="-java-home" (
    set "JAVA_HOME=%~1"
    set "_JAVACMD=%~1\bin\java.exe"
    shift
    goto param_loop
  )

  if "!_TEST_PARAM!"=="-jvm-debug" (
    set "_JAVA_PARAMS=!_JAVA_PARAMS! -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=%~1"
    shift
    goto param_loop
  )

  if "!_TEST_PARAM:~0,2!"=="-J" (
    rem strip -J prefix
    call set _TEST_PARAM=!_TEST_PARAM:~2!
    if not "!_TEST_PARAM:~0,5!" == "-XX:+" if not "!_TEST_PARAM:~0,5!" == "-XX:-" if "!_TEST_PARAM:~0,3!" == "-XX" (
      rem special handling for -J-XX since '=' gets parsed away
      for /F "delims== tokens=1,*" %%G in ("!_TEST_PARAM!") DO (
        call set _TEST_PARAM=!_TEST_PARAM!=%%1
        shift
      )
    )
    set _JAVA_PARAMS=!_JAVA_PARAMS! !_TEST_PARAM!
    goto param_loop
  )

  if "!_TEST_PARAM:~0,2!"=="-D" (
    rem test if this was double-quoted property "-Dprop=42"
    for /F "delims== tokens=1,*" %%G in ("!_TEST_PARAM!") DO (
      if not ["%%H"] == [""] (
        set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1!
      ) else if [%1] neq [] (
        rem it was a normal property: -Dprop=42 or -Drop="42"
        call set _PARAM1=%%0=%%1
        set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1!
        shift
      )
    )
    goto param_loop
  )

  set _APP_ARGS=!_APP_ARGS! !_PARAM1!

  goto param_loop
  :param_afterloop

exit /B 0
