#!/bin/bash
# info: delete web domain alias
# options: USER DOMAIN ALIAS [RESTART]
#
# example: v-delete-web-domain-alias admin example.com www.example.com
#
# This function of deleting the alias domain (parked domain). By this call
# default www aliase can be removed as well.

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

# Argument definition
user=$1
domain=$2
dom_alias=$3
restart=$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
# shellcheck source=/usr/local/hestia/func/ip.sh
source $HESTIA/func/ip.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '3' "$#" 'USER DOMAIN DOM_ALIAS [RESTART]'
is_format_valid 'user' 'domain' 'dom_alias' 'restart'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
get_domain_values 'web'
if [ -z "$(echo $ALIAS | tr ',' '\n' | grep ^$dom_alias$)" ]; then
	check_result "$E_NOTEXIST" "alias $dom_alias doesn't exist"
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Preparing domain values for the template substitution
local_ip=$(get_real_ip $IP)
ALIAS=$(echo "$ALIAS" \
	| sed -e "s/,/\n/g" \
	| sed -e "s/^$dom_alias$//g" \
	| sed -e "/^$/d" \
	| sed -e ':a;N;$!ba;s/\n/,/g')
prepare_web_domain_values

# Rebuilding vhost
del_web_config "$WEB_SYSTEM" "$TPL.tpl"
add_web_config "$WEB_SYSTEM" "$TPL.tpl"
if [ "$SSL" = 'yes' ]; then
	del_web_config "$WEB_SYSTEM" "$TPL.stpl"
	add_web_config "$WEB_SYSTEM" "$TPL.stpl"
fi

# Rebuilding proxy configuration
if [ -n "$PROXY_SYSTEM" ] && [ -n "$PROXY" ]; then
	del_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
	add_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
	if [ "$SSL" = 'yes' ]; then
		del_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
		add_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
	fi
fi

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

# Update config
update_object_value 'web' 'DOMAIN' "$domain" '$ALIAS' "$ALIAS"
decrease_user_value "$user" '$U_WEB_ALIASES'

# 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

# Logging
$BIN/v-log-action "$user" "Info" "Web" "Deleted web domain alias (Alias: $dom_alias, Domain: $domain)."
log_event "$OK" "$ARGUMENTS"

exit
