#!/bin/bash
# info: deleting web domain proxy configuration
# options: USER DOMAIN [RESTART]
#
# example: v-delete-web-domain-proxy alice lookinglass.com
#
# This function of deleting the virtualhost proxy configuration.

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

# Argument definition
user=$1
domain=$2
domain_idn=$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
# TODO: $domain_idn not used in this script - maybe $domain should be converted to $doman_idn ?

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

check_args '2' "$#" 'USER DOMAIN [RESTART]'
is_format_valid 'user' 'domain' 'restart'
is_system_enabled "$PROXY_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_value_exist 'web' 'DOMAIN' "$domain" '$PROXY'

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Defining domain parameters
get_domain_values 'web'
del_web_config "$PROXY_SYSTEM" "$PROXY.tpl"

# Checking SSL
if [ "$SSL" = 'yes' ]; then
	del_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
fi

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

# Update config
update_object_value 'web' 'DOMAIN' "$domain" '$PROXY' ''
update_object_value 'web' 'DOMAIN' "$domain" '$PROXY_EXT' ''

# Restart proxy server
$BIN/v-restart-proxy "$restart"
check_result $? "Proxy restart failed" > /dev/null

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

exit
