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

dynamic_configs_enabled=$1
kraft_enabled=$2
format_cluster_metadata_enabled=$3

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

cat "${KAFKA_CONFIG_DIR}/${CP_COMPONENT_NAME}.properties" > "${CP_COMPONENT_SCRIPT_DIR}/${CP_COMPONENT_NAME}.properties"
cat "${KAFKA_CONFIG_DIR}/shared/${CP_COMPONENT_NAME}.properties" >> "${CP_COMPONENT_SCRIPT_DIR}/${CP_COMPONENT_NAME}.properties"
if [ -e "${KAFKA_CONFIG_DIR}/shared/${CP_COMPONENT_NAME}-dynamic.properties" ]; then
  cat "${KAFKA_CONFIG_DIR}/shared/${CP_COMPONENT_NAME}-dynamic.properties" >> "${CP_COMPONENT_SCRIPT_DIR}/${CP_COMPONENT_NAME}.properties"
fi
cat "${KAFKA_CONFIG_DIR}/log4j.properties" > "${CP_COMPONENT_SCRIPT_DIR}/log4j.properties"
cat "${KAFKA_CONFIG_DIR}/shared/log4j.properties" >> "${CP_COMPONENT_SCRIPT_DIR}/log4j.properties"
cat "${KAFKA_CONFIG_DIR}/shared/disk-usage-agent.properties" > "${CP_COMPONENT_SCRIPT_DIR}/disk-usage-agent.properties"
cat "${KAFKA_CONFIG_DIR}/jvm.config" > "${CP_COMPONENT_SCRIPT_DIR}/jvm.config"
cat "${KAFKA_CONFIG_DIR}/shared/jvm.config" >> "${CP_COMPONENT_SCRIPT_DIR}/jvm.config"
if [ -e "${KAFKA_CONFIG_DIR}/shared/jolokia.config" ]; then
  cat "${KAFKA_CONFIG_DIR}/shared/jolokia.config" > "${CP_COMPONENT_SCRIPT_DIR}/jolokia.config"
fi
if [ -e "${KAFKA_CONFIG_DIR}/log4j2/log4j2.yaml" ]; then
  cat "${KAFKA_CONFIG_DIR}/log4j2/log4j2.yaml" > "${CP_COMPONENT_SCRIPT_DIR}/log4j2.yaml"
fi

# Setup JMX files (password and access control)
# Detect actual component type from POD_NAME for correct paths
JMX_COMPONENT_NAME="${CP_COMPONENT_NAME}"
JMX_DEST_DIR="${CP_COMPONENT_SCRIPT_DIR}"

if echo "${POD_NAME:-}" | grep -q "kraftcontroller"; then
  echo "===> Detected KRaftController pod, using kraftcontroller paths for JMX"
  JMX_COMPONENT_NAME="kraftcontroller"
  # KRaftController looks for JMX files in its own directory
  JMX_DEST_DIR="/opt/confluentinc/etc/kraftcontroller"
  mkdir -p "${JMX_DEST_DIR}"
fi

JMX_PASSWORD_SOURCE="/mnt/secrets/jmx-password-${JMX_COMPONENT_NAME}/jmxremote.password"
JMX_PASSWORD_DEST="${JMX_DEST_DIR}/jmxremote.password"
JMX_ACCESS_SOURCE="/mnt/secrets/jmx-access-${JMX_COMPONENT_NAME}/jmxremote.access"
JMX_ACCESS_DEST="${JMX_DEST_DIR}/jmxremote.access"

if [ -f "${JMX_PASSWORD_SOURCE}" ]; then
  echo "===> Copying JMX password file for component: ${JMX_COMPONENT_NAME} to ${JMX_PASSWORD_DEST}"
  cp "${JMX_PASSWORD_SOURCE}" "${JMX_PASSWORD_DEST}"
  chmod 600 "${JMX_PASSWORD_DEST}"
  echo "===> JMX password file permissions: $(ls -l ${JMX_PASSWORD_DEST})"
fi

if [ -f "${JMX_ACCESS_SOURCE}" ]; then
  echo "===> Copying JMX access control file for component: ${JMX_COMPONENT_NAME} to ${JMX_ACCESS_DEST}"
  cp "${JMX_ACCESS_SOURCE}" "${JMX_ACCESS_DEST}"
  chmod 644 "${JMX_ACCESS_DEST}"
  echo "===> JMX access file permissions: $(ls -l ${JMX_ACCESS_DEST})"
fi

if [ "$dynamic_configs_enabled" -eq 1 ]; then
  # copy filewatcher and create log file
  cp "${KAFKA_CONFIG_DIR}/filewatcher" "${CP_COMPONENT_SCRIPT_DIR}/filewatcher"
  touch "${CP_COMPONENT_SCRIPT_DIR}/filewatcher.log"
fi

if [ "$kraft_enabled" -eq 1 ]; then
  if [ -e "$KRAFT_META_PROPS_FILE" ]; then
    echo "===> KRaft storage formatted already"
  else
    echo "===> Format KRaft storage"
    # format storage directories for kraft
    kraft_cluster_id=$(cat "${KAFKA_CONFIG_DIR}/shared/cluster.id")
    kafka-storage format --config "${CP_COMPONENT_SCRIPT_DIR}/${CP_COMPONENT_NAME}.properties" --cluster-id=$kraft_cluster_id || exit $?
  fi
fi

if [ "$format_cluster_metadata_enabled" -eq 1 ]; then
  echo "===> Removing cluster_metadata from logs"
  # should be consistent with KafkaDataMountPath
  rm -rf /mnt/data/data0/logs/__cluster_metadata*
fi