#!/bin/bash

# Compose clickhouse-operator .yaml manifest from components

# Paths
CUR_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
PROJECT_ROOT="$(realpath "${CUR_DIR}/../..")"

# Relative and abs paths where templates live
TEMPLATES_PATH="deploy/builder/templates-config"
TEMPLATES_DIR="${PROJECT_ROOT}/${TEMPLATES_PATH}"

# Relative and abs paths where users.d templates live
TEMPLATES_USERSD_PATH="${TEMPLATES_PATH}/users.d"
TEMPLATES_USERSD_DIR="${PROJECT_ROOT}/${TEMPLATES_USERSD_PATH}"

# Relative and abs paths where config live
CONFIG_PATH="config"
CONFIG_DIR="${1:-"${PROJECT_ROOT}/${CONFIG_PATH}"}"

source "${CUR_DIR}/lib/lib.sh"

#
# Renders config file with all variables
#
function render_file() {
    local src="${1}"
    local dst="${2}"

    # Render file header
    if [[ "${dst: -4}" == ".xml" ]]; then
        # XML file
        cat <<EOF > "${dst}"
<!-- IMPORTANT -->
<!-- This file is auto-generated -->
<!-- Do not edit this file - all changes would be lost -->
<!-- Edit appropriate template in the following folder: -->
<!-- ${TEMPLATES_PATH} -->
<!-- IMPORTANT -->
EOF
    elif [[ "${dst: -5}" == ".yaml" ]]; then
        # YAML file
        cat <<EOF > "${dst}"
# IMPORTANT
# This file is auto-generated
# Do not edit this file - all changes would be lost
# Edit appropriate template in the following folder:
# ${TEMPLATES_PATH}
# IMPORTANT
EOF
    else
        # Unknown file
        echo -n "" > "${dst}"
    fi
    # Render file body
    cat "${src}" |                                                                        \
        WATCH_NAMESPACES="${WATCH_NAMESPACES:-""}"                                        \
        CH_USERNAME_PLAIN="${CH_USERNAME_PLAIN:-""}"                                      \
        CH_PASSWORD_PLAIN="${CH_PASSWORD_PLAIN:-""}"                                      \
        CH_CREDENTIALS_SECRET_NAMESPACE="${CH_CREDENTIALS_SECRET_NAMESPACE:-""}"          \
        CH_CREDENTIALS_SECRET_NAME="${CH_CREDENTIALS_SECRET_NAME:-"clickhouse-operator"}" \
        VERBOSITY="${VERBOSITY:-"1"}"                                                     \
        envsubst                                                                          \
        >> "${dst}"
}

# Iterate recursively over files in "${TEMPLATES_DIR}" and render them
find "${TEMPLATES_DIR}" -type f -printf '%P\n' | while read -r relative_file_name
do
    # Source
    # Full path to source file
    src_file_path=$(realpath "${TEMPLATES_DIR}/${relative_file_name}")
    relative_dir_name=$(dirname "${relative_file_name}")

    # Destination
    dst_dir="${CONFIG_DIR}/${relative_dir_name}"
    #echo "relative_file_name: ${relative_file_name}"
    #echo "relative_dir_name: ${relative_dir_name}"
    #echo "create dst dir: ${dst_dir}"
    mkdir -p "${dst_dir}"

    dst_file_path="${CONFIG_DIR}/${relative_file_name}"
    #echo "render ${dst_file_path}"
    #echo "from   ${src_file_path}"
    render_file "${src_file_path}" "${dst_file_path}"
done
