#!/usr/bin/env bash
#
# Copyright 2016 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

declare -A env_defaults
env_defaults=(
# Replace CLUSTER_ID with a unique base64 UUID using "bin/kafka-storage.sh random-uuid"
# See https://docs.confluent.io/kafka/operations-tools/kafka-tools.html#kafka-storage-sh
  ["CLUSTER_ID"]="4L6g3nShT-eMCtK--X86sw"
  ["KAFKA_NODE_ID"]=1
  ["KAFKA_LISTENER_SECURITY_PROTOCOL_MAP"]="CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT"
  ["KAFKA_LISTENERS"]="PLAINTEXT://localhost:29092,CONTROLLER://localhost:29093,PLAINTEXT_HOST://0.0.0.0:9092"
  ["KAFKA_ADVERTISED_LISTENERS"]="PLAINTEXT://localhost:29092,PLAINTEXT_HOST://localhost:9092"
  ["KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR"]=1
  ["KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS"]=0
  ["KAFKA_TRANSACTION_STATE_LOG_MIN_ISR"]=1
  ["KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR"]=1
  ["KAFKA_PROCESS_ROLES"]="broker,controller"
  ["KAFKA_CONTROLLER_QUORUM_VOTERS"]="1@localhost:29093"
  ["KAFKA_INTER_BROKER_LISTENER_NAME"]="PLAINTEXT"
  ["KAFKA_CONTROLLER_LISTENER_NAMES"]="CONTROLLER"
  ["KAFKA_LOG_DIRS"]="/tmp/kraft-combined-logs"
  ["KAFKA_REST_HOST_NAME"]="rest-proxy"
  ["KAFKA_REST_BOOTSTRAP_SERVERS"]="localhost:29092"
  ["KAFKA_REST_LISTENERS"]="http://0.0.0.0:8082"
)

for key in "${!env_defaults[@]}"; do
  if [[ -z "${!key:-}" ]]; then
    echo ${key} not set. Setting it to default value: \"${env_defaults[$key]}\"
    export "$key"="${env_defaults[$key]}"
  fi
done
