#!/bin/bash
# info: suspend dns domain
# options: USER DOMAIN [RESTART]
#
# example: v-suspend-dns-domain alice acme.com
#
# This function suspends a certain user's domain.

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

# Argument definition
user=$1
domain=$2
domain_idn=$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"

# Additional argument formatting
format_domain
format_domain_idn
# TODO: $domain_idn not used in this script - maybe $domain should be converted to $doman_idn ?

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

check_args '2' "$#" 'USER DOMAIN [RESTART]'
is_format_valid 'user' 'domain'
is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'dns' 'DOMAIN' "$domain"
is_object_unsuspended 'dns' 'DOMAIN' "$domain"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Deleting system configs
if [[ "$DNS_SYSTEM" =~ named|bind ]]; then
	if [ -e '/etc/named.conf' ]; then
		dns_conf='/etc/named.conf'
	else
		dns_conf='/etc/bind/named.conf'
	fi

	sed -i "/\/$user\/conf\/dns\/$domain.db\"/d" $dns_conf
fi

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

# Adding suspend in config
update_object_value 'dns' 'DOMAIN' "$domain" '$SUSPENDED' 'yes'
sed -i "s/SUSPENDED='no'/SUSPENDED='yes'/g" "$USER_DATA/dns/$domain.conf"
increase_user_value "$user" '$SUSPENDED_DNS'

# Logging
$BIN/v-log-action "system" "Info" "DNS" "Suspended DNS zone (User: $user, Domain: $domain)"
log_event "$OK" "$ARGUMENTS"

exit