#!/bin/bash
# info: add remote dns domain
# options: USER DOMAIN [FLUSH]
#
# example: v-add-remote-dns-domain admin mydomain.tld yes
#
# This function synchronise dns domain with the remote server.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
user=$1
domain=$2
flush=$3

# Includes
# shellcheck source=/etc/tuliocp/tulio.conf
source /etc/tuliocp/tulio.conf
# shellcheck source=/usr/local/tulio/func/main.sh
source $TULIO/func/main.sh
# shellcheck source=/usr/local/tulio/func/remote.sh
source $TULIO/func/remote.sh
# load config file
source_conf "$TULIO/conf/tulio.conf"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '2' "$#" 'USER DOMAIN [FLUSH]'
is_format_valid 'user' 'domain'
if [ -n "$flush" ]; then
	is_type_valid "records yes no" "$flush"
fi
is_system_enabled "$DNS_CLUSTER" 'DNS_CLUSTER'
is_procces_running
remote_dns_health_check

# Perform verification if read-only mode is enabled
check_tulio_demo_mode

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Parsing domain record
str=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf 2> /dev/null)
if [ -z "$str" ]; then
	pipe="$TULIO/data/queue/dns-cluster.pipe"
	queue_str=$(grep -n "$SCRIPT $1 $2 " $pipe | cut -f1 -d: | head -n1)
	if [ -n "$queue_str" ]; then
		sed -i "$queue_str d" $pipe
	fi
	exit
fi
if [ "$DNS_CLUSTER_SYSTEM" = "tulio-zone" ]; then
	str=$(echo "$str" | sed "s/SLAVE='no'/SLAVE='yes'/g")
	str=$(echo "$str" | sed "s/SLAVE=''/SLAVE='yes'/g")
	# Use Tulio's own configured system IP as the DNS master address.
	# Enumerating interfaces is non-deterministic and can return a
	# VPN/Docker/WireGuard address on servers with virtual interfaces,
	# propagating a wrong MASTER and breaking AXFR/IXFR zone transfers to the
	# slaves (#5469). Fall back to interface detection only when no system IP
	# is configured.
	ip=""
	LOCAL_IP=$(ip route get 1.1.1.1 2> /dev/null | grep -oP 'src \K\S+')
	if [[ -n "$LOCAL_IP" && -e "${TULIO}/data/ips/${LOCAL_IP}" ]]; then
		ip="$LOCAL_IP"
	fi
	# Fallback if nothing was detected
	if [[ -z "$ip" ]]; then
		ip=$(ip addr | grep 'inet ' | grep global | head -n1 | awk '{print $2}' | cut -f1 -d/)
	fi
	source_conf $TULIO/data/ips/$ip
	if [ -z $NAT ]; then
		str=$(echo "$str" | sed "s/MASTER=''/MASTER='$ip'/g")
	else
		str=$(echo "$str" | sed "s/MASTER=''/MASTER='$NAT'/g")
	fi
fi

IFS=$'\n'
for cluster in $(grep "SUSPENDED='no'" $TULIO/conf/dns-cluster.conf); do
	# Reset user, password and hash vars
	clear_dns_cluster_settings

	# Parsing remote dns host parameters
	parse_object_kv_list "$cluster"

	# Parsing domain parameters
	parse_object_kv_list "$str"

	if [ "$DNS_CLUSTER_SYSTEM" = "tulio-zone" ]; then
		# Syncing domain data
		cluster_cmd v-insert-dns-domain $DNS_USER "$str" $HOSTNAME $flush 'no'
		check_result $? "$HOST connection failed" "$E_CONNECT"

		cluster_cmd v-rebuild-dns-domain $DNS_USER $domain 'yes' 'no'
		check_result $? "$HOST connection failed" "$E_CONNECT"

	else
		# Syncing domain data
		cluster_cmd v-insert-dns-domain $DNS_USER "$str" $HOSTNAME $flush 'no'
		check_result $? "$HOST connection failed" "$E_CONNECT"

		# Syncing domain records
		tmp_file="/tmp/vst-sync.$DOMAIN"
		cluster_file $USER_DATA/dns/$DOMAIN.conf $tmp_file
		check_result $? "$HOST connection failed" "$E_CONNECT"

		# Inserting synced records
		cluster_cmd v-insert-dns-records $DNS_USER $DOMAIN $tmp_file 'no'
		check_result $? "$HOST connection failed" "$E_CONNECT"

		# Rebuilding dns zone
		cluster_cmd v-rebuild-dns-domain $DNS_USER $domain 'yes' 'no'
		check_result $? "$HOST connection failed" "$E_CONNECT"
	fi
done

if [ "$DNS_CLUSTER_SYSTEM" = "tulio-zone" ]; then
	rndc notify $domain > /dev/null 2>&1
fi
#----------------------------------------------------------#
#                       Tulio                             #
#----------------------------------------------------------#

# Updating pipe
rm -f $tmpfile
pipe="$TULIO/data/queue/dns-cluster.pipe"
str=$(grep -n "$SCRIPT $1 $2 " $pipe | cut -f1 -d: | head -n1)
if [ -n "$str" ]; then
	sed -i "$str d" $pipe
fi

exit
