#!/usr/bin/env bash
#
# Copyright 2016-2017 Confluent Inc.
#
set -o errexit \
    -o verbose

echo "===> Adding disk usage agent to the java command ... " 
export EXTRA_ARGS="${EXTRA_ARGS} -javaagent:/usr/share/java/cc-base/disk-usage-agent-${CC_BASE_VERSION}.jar=/opt/caas/config/kafka/disk-usage-agent.properties"

echo "===> Adding jolokia agent to the java command ... " 
export JOLOKIA_AGENT_PORT=${JOLOKIA_AGENT_PORT:-7777}
export JOLOKIA_AGENT_HOST=${JOLOKIA_AGENT_HOST:-"0.0.0.0"}
export EXTRA_ARGS="${EXTRA_ARGS} -javaagent:/opt/caas/lib/jolokia/jolokia-jvm-${JOLOKIA_AGENT_VERSION}.jar=port=${JOLOKIA_AGENT_PORT},host=${JOLOKIA_AGENT_HOST}"

echo "===> Adding jmx exporter to the java command ... " 
export JMX_EXPORTER_AGENT_PORT=${JMX_EXPORTER_AGENT_PORT:-7778}
export JMX_EXPORTER_AGENT_HOST=${JMX_EXPORTER_AGENT_HOST:-"0.0.0.0"}

export EXTRA_ARGS="${EXTRA_ARGS} -javaagent:/opt/caas/lib/jmx_prometheus_javaagent-${JMX_EXPORTER_AGENT_VERSION}.jar=${JMX_EXPORTER_AGENT_PORT}:/opt/caas/config/jmx-exporter.yaml"


echo "===> Adding JAAS security property to the java command ... "
SERVER_JAAS_FILE="${KAFKA_CONFIG_DIR}/shared/server_jaas.conf"
if [ -f "$SERVER_JAAS_FILE" ]; then
  echo "${SERVER_JAAS_FILE} exists"
  export EXTRA_ARGS="${EXTRA_ARGS} -Djava.security.auth.login.config=${KAFKA_CONFIG_DIR}/shared/server_jaas.conf"
else
  echo "No server_jaas.conf file present, using JAAS config from the server.properties file"
fi

echo "===> Adding logging json layout to CLASSPATH ... "
export CLASSPATH=/usr/share/java/cc-base/log4j-json-layout-${CC_BASE_VERSION}.jar

echo "===> Adding kafka broker plugins to CLASSPATH ... "
export CLASSPATH="/opt/confluent/libs/*:$CLASSPATH"

echo "===> Adding kafka log4j config ... "
cat /opt/caas/config/kafka/log4j.properties 
export KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:/opt/caas/config/kafka/log4j.properties"

echo "===> Adding JVM config to the java command ... "
cat /opt/caas/config/kafka/jvm.config
export EXTRA_ARGS="$(cat /opt/caas/config/kafka/jvm.config | xargs) ${EXTRA_ARGS}"
# These ensure that the "if" sections for heap sizing, GC tuning, and JMX opts in kafka launch script do not trigger.
export KAFKA_HEAP_OPTS=' '
export KAFKA_JVM_PERFORMANCE_OPTS=' '
export JMX_PORT=' '

# The following block of checks will prevent this script
# from starting up a Kafka server if confluent.repair.mode
# has been enabled (set to true) in the K8s configMap.
# This mode is targteted to allow the operator to execute certain
# runbooks on a Kafka Server when it is hosted within Kubernetes.
# This is an artifact of how Kafka is hosted in CCloud and
# some Kubernetes architectural requirements.

export logfile=${KAFKA_LOG4J_DIR}/main.log
trap_with_arg() {
    func="$1" ; shift
    for sig ; do
        trap "$func $sig" "$sig"
    done
}
export -f trap_with_arg
handle_signal() {
    echo "Caught Signal $1. Terminating Loop!" | tee -a $logfile
    exit
}
export -f handle_signal
kafka_repair_idle_loop() {
    # Signal handler registration
    trap_with_arg handle_signal SIGHUP SIGINT SIGTERM
    while true; do 
        echo "$(date) Running repair mode and staying alive forever." | tee -a $logfile
        echo "$(date) Refer to runbook - https://github.com/confluentinc/cc-documentation/blob/master/Operations/RunBook/KafkaCore/Confluent%20Repair%20Mode" | tee -a $logfile
        echo "-" | tee -a $logfile
        sleep 10; 
    done
}
export -f kafka_repair_idle_loop
export CONFLUENT_REPAIR_MODE=$(grep "^#confluent.repair.mode=" ${KAFKA_CONFIG_DIR}/kafka.properties | cut -d '=' -f 2)

SKIP_KAFKA_STARTUP="${CONFLUENT_REPAIR_MODE:-false}"

if [[ $SKIP_KAFKA_STARTUP == "true" || $SKIP_KAFKA_STARTUP == "TRUE" || $SKIP_KAFKA_STARTUP == "True" ]]; then
    # Run a dummy loop to keep PID 1 alive and emit some logs
    echo "===> CONFLUENT_REPAIR_MODE is ${CONFLUENT_REPAIR_MODE}. ${COMPONENT} will not be started in this mode"
    echo "===> Launching Repair Mode for ${COMPONENT} ... "
    exec /bin/bash -c 'kafka_repair_idle_loop' {} \;
fi

echo "===> Launching ${COMPONENT} ... "
exec /opt/confluent/bin/kafka-run-class.sh $EXTRA_ARGS kafka.Kafka ${KAFKA_CONFIG_DIR}/kafka.properties
