#!/bin/bash
# info: unsuspend dns domain
# options: USER DOMAIN
#
# example: v-unsuspend-dns-domain alice wonderland.com
#
# This function unsuspends a certain user's domain.

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

# Argument definition
user=$1
domain=$2
domain_idn=$2

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

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

check_args '2' "$#" 'USER DOMAIN'
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_suspended 'dns' 'DOMAIN' "$domain"

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

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

	# Adding zone in named.conf
	named="zone \"$domain_idn\" {type master; file"
	named="$named \"$HOMEDIR/$user/conf/dns/$domain.db\";};"
	echo "$named" >> $dns_conf
fi

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

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

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

exit