#!/bin/bash
# info: delete mail domain ssl support
# options: USER DOMAIN
#
# The function delete ssl certificates.

#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
user=$1
domain=$2

# Includes
source $HESTIA/func/main.sh
source $HESTIA/func/domain.sh
source $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_unsuspended 'user' 'USER' "$user"
is_object_valid 'mail' 'DOMAIN' "$domain"
is_object_unsuspended '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
log_history "disabled mail SSL support on $domain"
log_event "$OK" "$ARGUMENTS"

exit
