#!/bin/bash
# info: delete dns domains based on SRC field
# options: USER SRC [RESTART]
#
# example: v-delete-dns-domains-src admin '' yes
#
# This function for deleting DNS domains related to a certain host.

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

# Argument definition
user=$1
src=$2
restart=$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
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '2' "$#" 'USER SRC [RESTART]'
is_format_valid 'user' 'src'
is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
is_object_valid 'user' 'USER' "$user"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Starting delete loop
for domain in $(search_objects 'dns' 'SRC' "$src" 'DOMAIN'); do
	$BIN/v-delete-dns-domain "$user" "$domain" 'no'
done

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

# Restart named
$BIN/v-restart-dns "$restart"
check_result $? "Bind restart failed" > /dev/null

# Logging
log_event "$OK" "$ARGUMENTS"

exit