#!/usr/bin/env bash
#
# Copyright 2020 Confluent Inc.
#
set -o errexit

# Source shared utilities for resolving ${file:path:key} references (DPIC support)
if [ -e "/mnt/config/resolve-file-refs.sh" ]; then
  source "/mnt/config/resolve-file-refs.sh"
fi

# Source JVM opts resolver for JMX/JVM property file references (DPIC support)
if [ -e "/mnt/config/resolve-jvm-opts.sh" ]; then
  source "/mnt/config/resolve-jvm-opts.sh"
fi

echo "===> Configure log4j config"
export CONTROL_CENTER_LOG4J_OPTS="-Dlog4j.configuration=file:${CP_COMPONENT_SCRIPT_DIR}/log4j.properties"

disk-agent() {
  if [ -e "${CP_COMPONENT_SCRIPT_DIR}/disk-usage-agent.properties" ]; then
      echo "===> Configure disk-usage agent"
      disk=("/usr/share/java/cc-base/disk-usage-agent-"*.jar)
      len=${#disk[@]}
      # not found if the 1st element of the array is the unexpanded string
      # (ie, if it contains a "*")
      if [[ ${disk[0]} =~ [*] ]] ; then
        # assume only 1 jar version
        disk=("/usr/share/java/cp-base-new/disk-usage-agent-"*.jar)
        export DISK_JAR=${disk[0]}
      elif [ "${len}" -eq 1 ]; then
        export DISK_JAR=${disk[0]}
      else
        echo "===> Multiple disk agent found, using the last one from ${disk[*]}"
        export DISK_JAR=${disk[len-1]}
      fi
      export CONTROL_CENTER_OPTS="${CONTROL_CENTER_OPTS} -javaagent:${DISK_JAR}=${CP_COMPONENT_SCRIPT_DIR}/disk-usage-agent.properties"
  fi
}

jolokia-agent() {
  echo "===> Configure jolokia agent"
  JOLOKIA_DIR="/usr/share/java/cp-base-new"
  if [ -d "/opt/caas/lib" ]; then
    JOLOKIA_DIR="/opt/caas/lib/jolokia"
  fi

  JOLOKIA_AGENT_PORT=${JOLOKIA_AGENT_PORT:-7777}
  JOLOKIA_AGENT_HOST=${JOLOKIA_AGENT_HOST:-"0.0.0.0"}
  if [ -e "${CP_COMPONENT_SCRIPT_DIR}/jolokia.config" ]; then
    JOLOKIA_CONFIG="$(cat < "${CP_COMPONENT_SCRIPT_DIR}/jolokia.config")"
    # Resolve ${file:...} references if resolver utility is available
    if declare -f resolve_file_password > /dev/null 2>&1; then
      JOLOKIA_CONFIG="$(resolve_file_password "$JOLOKIA_CONFIG")"
    fi
    JOLOKIA_EXTRA_ARGS=",${JOLOKIA_CONFIG}"
    export JOLOKIA_EXTRA_ARGS
  fi

  JOLOKIA=("${JOLOKIA_DIR}/jolokia-jvm-"*.jar)
  export CONTROL_CENTER_OPTS="${CONTROL_CENTER_OPTS} -javaagent:${JOLOKIA[0]}=port=${JOLOKIA_AGENT_PORT},host=${JOLOKIA_AGENT_HOST}${JOLOKIA_EXTRA_ARGS}"
}

jmx-prometheus-agent(){

   if [ -e "/mnt/config/shared/jmx-exporter.yaml" ]; then
      echo "===> Configure jmx prometheus agent"
      PROMETHEUS_DIR="/usr/share/java/cp-base-new"
      if [ -d "/opt/caas/lib" ]; then
          PROMETHEUS_DIR="/opt/caas/lib"
      fi

      JMX_EXPORTER_AGENT_PORT=${JMX_EXPORTER_AGENT_PORT:-7778}
      JMX_EXPORTER_AGENT_HOST=${JMX_EXPORTER_AGENT_HOST:-"0.0.0.0"}
      PROMETHEUS=("${PROMETHEUS_DIR}/jmx_prometheus_javaagent-"*.jar)

      export CONTROL_CENTER_OPTS="${CONTROL_CENTER_OPTS} -javaagent:${PROMETHEUS[0]}=${JMX_EXPORTER_AGENT_PORT}:/mnt/config/shared/jmx-exporter.yaml"
  fi
}

disk-agent
jolokia-agent
jmx-prometheus-agent

## layered images
##
if [ -d /opt/confluent/etc ]; then
  ## requires for now but have to remove in the future iterations
  ## we need to remove javaagent from the "${CP_COMPONENT_SCRIPT_DIR}/jvm.config" as Operator addes as part of
  ## new stack deployment
  echo "===> Configure JVM config"
  CONTROL_CENTER_OPTS="$(sed '/-javaagent*/d' "${CP_COMPONENT_SCRIPT_DIR}/jvm.config" | sed '/^$/d' | tr '\n' ' ') $CONTROL_CENTER_OPTS"
  export CONTROL_CENTER_OPTS
else
  echo "===> Configure JVM config"
  CONTROL_CENTER_OPTS="$(cat < "${CP_COMPONENT_SCRIPT_DIR}/jvm.config" | tr '\n' ' ') ${CONTROL_CENTER_OPTS}"
  export CONTROL_CENTER_OPTS
fi

# Resolve ${file:path:key} references in JVM options (DPIC mode for JMX properties)
if declare -f resolve_jvm_file_refs > /dev/null; then
  CONTROL_CENTER_OPTS="$(resolve_jvm_file_refs "$CONTROL_CENTER_OPTS")"
  export CONTROL_CENTER_OPTS
fi

export CONTROL_CENTER_HEAP_OPTS=' '
export CONTROL_CENTER_JVM_PERFORMANCE_OPTS=' '
export CONTROL_CENTER_JMX_OPTS=' '
export JMX_PORT=' '
exec control-center-start "${CP_COMPONENT_SCRIPT_DIR}/${CP_COMPONENT_NAME}.properties"
