#!/bin/bash
# info: delete mail domain ssl support
# options: USER DOMAIN
#
# example: v-delete-mail-domain-ssl user demo.com
#
# This function delete ssl certificates.

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

# Argument definition
user=$1
domain=$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
# 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"
is_object_value_exist 'mail' 'DOMAIN' "$domain" '$SSL'

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Delete mail domain SSL configuration
del_mail_ssl_config
del_mail_ssl_certificates
del_webmail_ssl_config

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

# Updating config
update_object_value 'mail' 'DOMAIN' "$domain" '$SSL' 'no'
update_object_value 'mail' 'DOMAIN' "$domain" '$LETSENCRYPT' 'no'
decrease_user_value "$user" '$U_MAIL_SSL'

# Restarting mail server
$BIN/v-restart-mail "$restart"
check_result $? "Mail restart failed" > /dev/null

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

exit