#!/bin/bash

#
# FusionAuth environment setup. The fusionauth.properties file
# is parsed by this script to setup additional command-line properties including memory settings.
#

# Fail on any command failure
set -e

FUSIONAUTH_PLUGIN_DIR=${CATALINA_HOME}/../../plugins
FUSIONAUTH_CONFIG_DIR=${CATALINA_HOME}/../../config
FUSIONAUTH_JAVA_DIR=${CATALINA_HOME}/../../java
FUSIONAUTH_LOG_DIR=${CATALINA_HOME}/../../logs
CATALINA_OUT=${FUSIONAUTH_LOG_DIR}/fusionauth-app.log
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"
JAVA_OPTS=" -Djava.awt.headless=true -Dcom.sun.org.apache.xml.internal.security.ignoreLineBreaks=true -Dnashorn.args=--no-deprecation-warning --enable-preview"
JAVA_OPTS=$(echo "${JAVA_OPTS}" | tr -d '\r')

CURL_OPTS="-fSL --progress-bar"
# If we are in a non interactive shell then hide the progress but show errors
if ! tty -s; then
  CURL_OPTS="-sS"
fi

if [[ ! -d "${CATALINA_HOME}/logs" ]]; then
  mkdir -p "${CATALINA_HOME}/logs"
fi

if [[ ! -d ${FUSIONAUTH_JAVA_DIR} ]]; then
  mkdir -p "${FUSIONAUTH_JAVA_DIR}"
fi

if [[ ! -d ${FUSIONAUTH_LOG_DIR} ]]; then
  mkdir -p "${FUSIONAUTH_LOG_DIR}"
fi

downloadJava() {
  JAVA_VERSION=14.0.1+7
  BASE_URL_PATH=https://files.fusionauth.io/java/openjdk

  # Ensure both the 'current' and 'jdk-${JAVA_VERSION}' exist, this tells us Java is setup and at the correct version
  if [[ ! -e "${FUSIONAUTH_JAVA_DIR}/current" || ! -d "${FUSIONAUTH_JAVA_DIR}/jdk-${JAVA_VERSION}" ]]; then
    if [[ "$(uname -s)" = "Darwin" ]]; then
      if [[ -e ~/dev/java/current14 ]]; then
        # Development, just sym link to our current version of Java, because only 'current' will exist in dev, we'll always rebuild the symlink.
        cd "${FUSIONAUTH_JAVA_DIR}"
        rm -f current
        ln -s ~/dev/java/current14 current
      else
        curl ${CURL_OPTS} "${BASE_URL_PATH}/openjdk-macos-${JAVA_VERSION}.tar.gz" -o "${FUSIONAUTH_JAVA_DIR}/openjdk-macos-${JAVA_VERSION}.tar.gz"
        tar xfz "${FUSIONAUTH_JAVA_DIR}/openjdk-macos-${JAVA_VERSION}.tar.gz" -C "${FUSIONAUTH_JAVA_DIR}"
        cd "${FUSIONAUTH_JAVA_DIR}"
        rm -f current
        ln -s jdk-${JAVA_VERSION}/Contents/Home current
        rm openjdk-macos-${JAVA_VERSION}.tar.gz
      fi
    elif [[ "$(uname -s)" = "Linux" ]]; then
      curl ${CURL_OPTS} "${BASE_URL_PATH}/openjdk-linux-${JAVA_VERSION}.tar.gz" -o "${FUSIONAUTH_JAVA_DIR}/openjdk-linux-${JAVA_VERSION}.tar.gz"
      tar xfz "${FUSIONAUTH_JAVA_DIR}/openjdk-linux-${JAVA_VERSION}.tar.gz" -C "${FUSIONAUTH_JAVA_DIR}"
      cd "${FUSIONAUTH_JAVA_DIR}"
      rm -f current
      ln -s jdk-${JAVA_VERSION} current
      rm openjdk-linux-${JAVA_VERSION}.tar.gz
    fi
  fi
}

appendToVariable() {
  local varName=$1
  local value=$2
  if [[ -n ${!varName} ]]; then
    export "${varName}"="${!varName} ${value}"
  else
    export "${varName}"="${value}"
  fi
}

# Arguments are: (envVarName, cmdOption, oldName, newName, oldEnvName, newEnvName)
resolveConfigurationProperty() {
  local envVarName=$1
  local cmdOption=$2
  local oldName=$3
  local newName=$4
  local oldEnvName=$5
  local newEnvName=$6

  if [[ -n ${!newEnvName} ]]; then
    appendToVariable "${envVarName}" "${cmdOption}${!newEnvName}"
    return
  elif [[ -n ${oldEnvName} && -n ${!oldEnvName} ]]; then
    echo "You are using a deprecated environment variable of [${oldEnvName}]. The new variable name is [${newEnvName}]"
    appendToVariable "${envVarName}" "${cmdOption}${!oldEnvName}"
    return
  elif [[ -f "${FUSIONAUTH_CONFIG_DIR}/fusionauth.properties" ]]; then
    if [[ -n ${newName} ]]; then
      newValue=$(grep "^${newName//[.]/\.}" "${FUSIONAUTH_CONFIG_DIR}/fusionauth.properties" | awk -F'=' '{ sub(/\r$/,""); print $2}')
      if [[ -n ${newValue} ]]; then
        appendToVariable "${envVarName}" "${cmdOption}${newValue}"
        return
      fi
    fi
    if [[ -n ${oldName} ]]; then
      oldValue=$(grep "^${oldName//[.]/\.}" "${FUSIONAUTH_CONFIG_DIR}/fusionauth.properties" | awk -F'=' '{ sub(/\r$/,""); print $2}')
      if [[ -n ${oldValue} ]]; then
        echo "You are using a deprecated configuration property of [${oldName}] in [fusionauth.properties]. The new variable name is [${newName}]"
        appendToVariable "${envVarName}" "${cmdOption}${oldValue}"
        return
      fi
    fi
  fi
}

if [[ ${FUSIONAUTH_USE_GLOBAL_JAVA} != 1 ]]; then
  # Download Java if necessary
  downloadJava

  # Set JAVA_HOME
  JAVA_HOME=${FUSIONAUTH_JAVA_DIR}/current
fi

resolveConfigurationProperty JAVA_OPTS -Dfusionauth-app.http.port= fusionauth-app.http-port fusionauth-app.http.port FUSIONAUTH_HTTP_PORT FUSIONAUTH_APP_HTTP_PORT
resolveConfigurationProperty JAVA_OPTS -Dfusionauth-app.https.port= fusionauth-app.https-port fusionauth-app.https.port FUSIONAUTH_HTTPS_PORT FUSIONAUTH_APP_HTTPS_PORT
resolveConfigurationProperty JAVA_OPTS -Dfusionauth-app.ajp.port= fusionauth-app.ajp-port fusionauth-app.ajp.port FUSIONAUTH_AJP_PORT FUSIONAUTH_APP_AJP_PORT
resolveConfigurationProperty JAVA_OPTS -Dfusionauth-app.management.port= fusionauth-app.management-port fusionauth-app.management.port FUSIONAUTH_MANAGEMENT_PORT FUSIONAUTH_APP_MANAGEMENT_PORT
resolveConfigurationProperty CATALINA_OPTS -Xmx "" fusionauth-app.memory FUSIONAUTH_MEMORY FUSIONAUTH_APP_MEMORY
resolveConfigurationProperty CATALINA_OPTS -Xms "" fusionauth-app.memory FUSIONAUTH_MEMORY FUSIONAUTH_APP_MEMORY
resolveConfigurationProperty CATALINA_OPTS "" "" fusionauth-app.additional-java-args FUSIONAUTH_ADDITIONAL_JAVA_ARGS FUSIONAUTH_APP_ADDITIONAL_JAVA_ARGS
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
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
resolveConfigurationProperty JAVA_OPTS -Dfusionauth-app.http.relaxed-path-chars= "" fusionauth-app.http.relaxed-path-chars "" FUSIONAUTH_APP_HTTP_RELAXED_PATH_CHARS
resolveConfigurationProperty JAVA_OPTS -Dfusionauth-app.http.relaxed-query-chars= "" fusionauth-app.http.relaxed-query-chars "" FUSIONAUTH_APP_HTTP_RELAXED_QUERY_CHARS