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

DIR=`dirname "$0"`
cd $DIR

while getopts ":h:p:g:t:" opt
do
  case $opt in
  h)
    host=$OPTARG
    ;;
  p)
    port=$OPTARG
    ;;
  g)
    group=$OPTARG
    ;;
  t)
    tenant=$OPTARG
    ;;
  ?)
    echo "USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] "
    exit 1
    ;;
  esac
done

if [[ -z ${host} ]]; then
    host=localhost
fi
if [[ -z ${port} ]]; then
    port=8848
fi
if [[ -z ${group} ]]; then
    group="SEATA_GROUP"
fi
if [[ -z ${tenant} ]]; then
    tenant=""
fi

nacosAddr=$host:$port
contentType="content-type:application/json;charset=UTF-8"

echo "  set nacosAddr=$nacosAddr"
echo "  set group=$group"

failCount=0
tempLog=$(mktemp -u)

function addConfig() {
  curl -X POST -H "${1}" "http://$2/nacos/v1/cs/configs?dataId=$3&group=$group&content=$4&tenant=$tenant" > "${tempLog}" 2>/dev/null
  if [[ -z $(cat "${tempLog}") ]]; then
    echo "  Please check the cluster status."
    exit 1
  fi
  if [[ $(cat "${tempLog}") =~ "true" ]]; then
    echo "  Set $3=$4 successfully "
  else
    echo "  Set $3=$4 failure "
    (( failCount++ ))
  fi
}

count=0
for line in $(cat ./config.txt); do
  (( count++ ))
	key=${line%%=*}
  value=${line#*=}
	addConfig "${contentType}" "${nacosAddr}" "${key}" "${value}"
done

echo "  ========================================================================="
echo "   Complete initialization parameters, \033[32m total-count:$count \033[0m, \033[31m failure-count:$failCount \033[0m"
echo "  ========================================================================="

if [[ ${failCount} -eq 0 ]]; then
	echo "  \033[32m Init nacos config finished, please start seata-server. \033[0m"
else
	echo "  \033[31m init nacos config fail. \033[0m"
fi
