#!/bin/bash
# info: synchronize dns domains
# options: HOST
#
# This function synchronise all dns domains.

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

# Argument definition
host=$1

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# shellcheck source=/usr/local/hestia/func/remote.sh
source $HESTIA/func/remote.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

is_format_valid 'host'
is_system_enabled "$DNS_CLUSTER" 'DNS_CLUSTER'
is_procces_running
remote_dns_health_check 'no_email'

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Selecting remote hosts
IFS=$'\n'
if [ -z $host ]; then
	hosts=$(cat $HESTIA/conf/dns-cluster.conf | grep "SUSPENDED='no'")
else
	hosts=$(grep "HOST='$host'" $HESTIA/conf/dns-cluster.conf)
fi

# Starting cluster loop
for cluster in $hosts; do

	# Reset user, password and hash vars
	clear_dns_cluster_settings

	# Parsing host values
	parse_object_kv_list "$cluster"

	# Wiping remote domains
	cluster_cmd v-delete-dns-domains-src $DNS_USER $HOSTNAME no
	check_result $? "$HOST connection failed" $E_CONNECT

	# Syncing user domains
	user_list=$(ls -d $HESTIA/data/users/*/ | sed "s#$HESTIA/data/users/##" | sed s"/.$//" | grep -v "dns-cluster")
	for user in $user_list; do
		USER_DATA="$HESTIA/data/users/$user"
		ROLE=$(get_user_value '$ROLE')
		if [ "$ROLE" != "dns-cluster" ]; then
			for str in $(cat $HESTIA/data/users/$user/dns.conf); do
				unset $SLAVE
				parse_object_kv_list "$str"
				if [ "$SLAVE" != "yes" ]; then
					if [ "$DNS_CLUSTER_SYSTEM" != "hestia-zone" ]; then
						# Syncing domain index

						cluster_cmd v-insert-dns-domain "$DNS_USER" "$str" "$HOSTNAME" ' ' "no"
						check_result $? "$HOST connection failed" "$E_CONNECT"

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

						cluster_cmd v-insert-dns-records "$DNS_USER" "$DOMAIN" "$tmp_file" 'no'
						check_result $? "$HOST connection failed" "$E_CONNECT"
					fi
					if [ "$DNS_CLUSTER_SYSTEM" = "hestia-zone" ]; then
						str=$(echo "$str" | sed "s/SLAVE='no'/SLAVE='yes'/g")
						str=$(echo "$str" | sed "s/SLAVE=''/SLAVE='yes'/g")

						ip=$(ip addr | grep 'inet ' | grep global | head -n1 | awk '{print $2}' | cut -f1 -d/)
						source_conf $HESTIA/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

						# 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"
						rndc notify $DOMAIN > /dev/null 2>&1
					fi
				fi
			done
		fi
	done
	if [ "$DNS_CLUSTER_SYSTEM" != "hestia-zone" ]; then
		# Rebuilding dns zones
		cluster_cmd v-rebuild-dns-domains "$DNS_USER" "yes" "no"
		check_result $? "$TYPE connection to $HOST failed" "$E_CONNECT"
	fi
done

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Flushing dns-cluster queue
rm -f $HESTIA/data/queue/dns-cluster.pipe
touch $HESTIA/data/queue/dns-cluster.pipe
chmod 660 $HESTIA/data/queue/dns-cluster.pipe

exit
