#!/bin/bash
# info: delete webmail support for a domain
# options: USER DOMAIN [RESTART] [QUIET]
#
# example: v-delete-mail-domain-webmail user demo.com
#
# This function removes support for webmail from
# a specified mail domain.

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

# Argument definition
user=$1
domain=$2
restart="$3"
quiet=$4

# 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/domain.sh
source $HESTIA/func/domain.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# Additional argument formatting
format_domain
format_domain_idn

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

check_args '2' "$#" 'USER DOMAIN [RESTART]'
is_format_valid 'user' 'domain'
is_system_enabled "$MAIL_SYSTEM" 'MAIL_SYSTEM'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_system_enabled "$IMAP_SYSTEM" 'IMAP_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'mail' 'DOMAIN' "$domain"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

if [ -n "$WEBMAIL_ALIAS" ]; then
	# Delete webmail configuration
	del_webmail_config
	del_webmail_ssl_config

	# Ensure that corresponding DNS records are removed
	if [ -n "$DNS_SYSTEM" ]; then
		dns_domain=$($BIN/v-list-dns-domains "$user" | grep "$domain" | cut -d' ' -f1)
		if [ "WEBMAIL_ALIAS" != "mail" ]; then
			#Prevent mail.domain.com being removed
			webmail_record=$($BIN/v-list-dns-records $user $domain | grep -i " $WEBMAIL_ALIAS " | cut -d' ' -f1)
			if [ "$dns_domain" = "$domain" ]; then
				if [ -n "$webmail_record" ]; then
					if [ "$quiet" = "yes" ]; then
						$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart" 'yes'
					else
						$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart"
					fi
				fi
			fi
		fi
	fi
else
	echo "Error: WEBMAIL_ALIAS is not defined in hestia.conf."
fi

# Set SSL as enabled in configuration
update_object_value 'mail' 'DOMAIN' "$domain" '$WEBMAIL' ""

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

if [ -n "$restart" ]; then
	# Restarting web server
	$BIN/v-restart-web "$restart"
	check_result $? "Web restart failed" > /dev/null

	$BIN/v-restart-proxy "$restart"
	check_result $? "Proxy restart failed" > /dev/null
fi

# Logging
if [ "$quiet" != 'yes' ]; then
	$BIN/v-log-action "$user" "Info" "Mail" "Webmail access disabled (Domain: $domain)."
fi
log_event "$OK" "$ARGUMENTS"

exit
