#!/usr/bin/env bash
#
# Copyright 2020 Confluent Inc.
#

set -o nounset
set -o errexit

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

cat "${KSQL_CONFIG_DIR}/${CP_COMPONENT_NAME}.properties" > "${CP_COMPONENT_SCRIPT_DIR}/${CP_COMPONENT_NAME}.properties"
cat "${KSQL_CONFIG_DIR}/shared/${CP_COMPONENT_NAME}.properties" >> "${CP_COMPONENT_SCRIPT_DIR}/${CP_COMPONENT_NAME}.properties"
cat "${KSQL_CONFIG_DIR}/log4j.properties" > "${CP_COMPONENT_SCRIPT_DIR}/log4j.properties"
cat "${KSQL_CONFIG_DIR}/shared/log4j.properties" >> "${CP_COMPONENT_SCRIPT_DIR}/log4j.properties"
cat "${KSQL_CONFIG_DIR}/shared/disk-usage-agent.properties" > "${CP_COMPONENT_SCRIPT_DIR}/disk-usage-agent.properties"
cat "${KSQL_CONFIG_DIR}/jvm.config" > "${CP_COMPONENT_SCRIPT_DIR}/jvm.config"
cat "${KSQL_CONFIG_DIR}/shared/jvm.config" >> "${CP_COMPONENT_SCRIPT_DIR}/jvm.config"
if [ -e "${KSQL_CONFIG_DIR}/shared/jolokia.config" ]; then
  cat "${KSQL_CONFIG_DIR}/shared/jolokia.config" > "${CP_COMPONENT_SCRIPT_DIR}/jolokia.config"
fi
# backward compatibility
if [ -e "${KSQL_CONFIG_DIR}/shared/server_jaas.conf" ]; then
  cat "${KSQL_CONFIG_DIR}/shared/server_jaas.conf > ${CP_COMPONENT_SCRIPT_DIR}/server_jaas.conf"
fi
if [ -e "${KSQL_CONFIG_DIR}/log4j2/log4j2.yaml" ]; then
  cat "${KSQL_CONFIG_DIR}/log4j2/log4j2.yaml" > "${CP_COMPONENT_SCRIPT_DIR}/log4j2.yaml"
fi

# Setup JMX files (password and access control)
source ${KSQL_CONFIG_DIR}/jmx_setup.sh
setup_jmx_files "${CP_COMPONENT_NAME}" "${CP_COMPONENT_SCRIPT_DIR}"
