#!/bin/bash
# info: delete mail domain
# options: USER DOMAIN
#
# example: v-delete-mail-domain admin mydomain.tld
#
# This function for deleting MAIL domain. By deleting it all accounts will
# also be deleted.

#----------------------------------------------------------#
#                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
# 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'
is_format_valid 'user' 'domain'
is_system_enabled "$MAIL_SYSTEM" 'MAIL_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                             #
#----------------------------------------------------------#

# Get domain values
get_domain_values 'mail'
accounts=$(wc -l "$USER_DATA/mail/$domain.conf" | cut -f 1 -d ' ')

# Deleting exim configuration files
if [[ "$MAIL_SYSTEM" =~ exim ]]; then
	rm -f /etc/$MAIL_SYSTEM/domains/$domain_idn
	rm -rf $HOMEDIR/$user/conf/mail/$domain
	rm -rf $HOMEDIR/$user/mail/$domain_idn
fi

# Deleting dkim dns record
if [ "$DKIM" = 'yes' ] && [ -e "$USER_DATA/dns/$domain.conf" ]; then
	dkim_records=$($BIN/v-list-dns-records "$user" "$domain" | grep -E "\s(mail\._domainkey|_domainkey)\s" | cut -f 1 -d ' ')
	for id in $dkim_records; do
		$BIN/v-delete-dns-record "$user" "$domain" "$id" '' 'yes'
	done
fi

# Delete SSL certificates and configuration
if [ "$SSL" = 'yes' ] || [ -e "$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt" ]; then
	$BIN/v-delete-mail-domain-ssl "$user" "$domain"
fi

# Delete webmail configuration if it exists
if [ -n "$WEB_SYSTEM" ] || [ -n "$PROXY_SYSTEM" ]; then
	if [ -n "$IMAP_SYSTEM" ]; then
		$BIN/v-delete-mail-domain-webmail "$user" "$domain" 'yes'
	fi
fi

# Run extension trigger
if [ -x "$HESTIA/data/extensions/delete-mail-domain.sh" ]; then
	$HESTIA/data/extensions/delete-mail-domain.sh "$user" "$domain"
fi

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

# Delete domain config
sed -i "/DOMAIN='$domain'/ d" $USER_DATA/mail.conf
rm -f $USER_DATA/mail/$domain.conf
rm -f $USER_DATA/mail/$domain.pem
rm -f $USER_DATA/mail/$domain.pub
rm -f $USER_DATA/mail/*@$domain.msg

# Decreasing domain value
decrease_user_value "$user" '$U_MAIL_DOMAINS'
if [ "$DKIM" = 'yes' ]; then
	decrease_user_value "$user" '$U_MAIL_DKIM'
fi

decrease_user_value "$user" '$U_MAIL_ACCOUNTS' "$accounts"

# Check if is suspended to decrease the suspended value
if [ -n "$SUSPENDED" ]; then
	if [ "$SUSPENDED" == "yes" ]; then
		decrease_user_value "$user" '$SUSPENDED_MAIL'
	fi
fi

# Logging
$BIN/v-log-action "$user" "Info" "Mail" "Mail domain deleted (Domain: $domain)."
log_event "$OK" "$ARGUMENTS"

exit
