#!/usr/bin/env bash
#
# Copyright 2019 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.

. /etc/confluent/docker/bash-config

ub ensure CONNECT_BOOTSTRAP_SERVERS
ub ensure CONNECT_GROUP_ID
ub ensure CONNECT_CONFIG_STORAGE_TOPIC
ub ensure CONNECT_OFFSET_STORAGE_TOPIC
ub ensure CONNECT_STATUS_STORAGE_TOPIC
ub ensure CONNECT_KEY_CONVERTER
ub ensure CONNECT_VALUE_CONVERTER
# This is required to avoid config bugs. You should set this to a value that is
# resolvable by all containers.
ub ensure CONNECT_REST_ADVERTISED_HOST_NAME

# Default to 8083, which matches the mesos-overrides. This is here in case we extend the containers to remove the mesos overrides.
if [ -z "$CONNECT_REST_PORT" ]; then
  export CONNECT_REST_PORT=8083
fi

# Fix for https://issues.apache.org/jira/browse/KAFKA-3988
if [[ ${CONNECT_INTERNAL_KEY_CONVERTER-} == "org.apache.kafka.connect.json.JsonConverter" ]] || [[ ${CONNECT_INTERNAL_VALUE_CONVERTER-} == "org.apache.kafka.connect.json.JsonConverter" ]]
then
  export CONNECT_INTERNAL_KEY_CONVERTER_SCHEMAS_ENABLE=false
  export CONNECT_INTERNAL_VALUE_CONVERTER_SCHEMAS_ENABLE=false
fi

if [[ $CONNECT_KEY_CONVERTER == "io.confluent.connect.avro.AvroConverter" ]]
then
  ub ensure CONNECT_KEY_CONVERTER_SCHEMA_REGISTRY_URL
fi

if [[ $CONNECT_VALUE_CONVERTER == "io.confluent.connect.avro.AvroConverter" ]]
then
  ub ensure CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL
fi

ub path /etc/"${COMPONENT}"/ writable

ub render-template "/etc/confluent/docker/${COMPONENT}.properties.template" > /etc/${COMPONENT}/${COMPONENT}.properties

# The connect-distributed script expects the log4j2 config at /etc/kafka/connect-log4j2.yaml.
ub render-template "/etc/confluent/docker/log4j2.yaml.template" > /etc/kafka/connect-log4j2.yaml
