#!/bin/bash
# info: add remote dns domain record
# options: USER DOMAIN ID
#
# example: v-add-remote-dns-record bob acme.com 23
#
# This function synchronise dns domain with the remote server.

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

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

# 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                         #
#----------------------------------------------------------#

check_args '3' "$#" 'USER DOMAIN ID'
is_format_valid 'user' 'domain' 'id'
is_system_enabled "$DNS_CLUSTER" 'DNS_CLUSTER'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'dns' 'DOMAIN' "$domain"
is_procces_running
remote_dns_health_check

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Parsing record
str=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
if [ -z "$str" ]; then
	pipe="$HESTIA/data/queue/dns-cluster.pipe"
	queue_str=$(grep -n "$SCRIPT $1 $2 $3$" $pipe | cut -f1 -d: | head -n1)
	if [ -n "$queue_str" ]; then
		sed -i "$queue_str d" $pipe
	fi
	exit
fi
# $DNS_CLUSTER_SYSTEM = "hestia-zone" doesn't need to be uopdated

if [ "$DNS_CLUSTER_SYSTEM" != "hestia-zone" ]; then
	IFS=$'\n'
	for cluster in $(grep "SUSPENDED='no'" $HESTIA/conf/dns-cluster.conf); do

		# Reset user, password and hash vars
		clear_dns_cluster_settings

		# Parsing remote host parameters
		parse_object_kv_list "$cluster"

		# Syncing serial
		str=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
		# Parsing domain parameters
		parse_object_kv_list "$str"

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

		# Syncing record
		str=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
		cluster_cmd v-insert-dns-record $DNS_USER $domain "$str" 'no'
		check_result $? "$HOST connection failed (record sync)" "$E_CONNECT"

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

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

exit
