#!/bin/bash
# info: delete web domain
# options: USER DOMAIN [RESTART]
#
# example: v-delete-web-domain admin wonderland.com
#
# The call of function leads to the removal of domain and all its components
# (statistics, folders contents, ssl certificates, etc.). This operation is
# not fully supported by "undo" function, so the data recovery is possible
# only with a help of reserve copy.

#----------------------------------------------------------#
#                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
# shellcheck source=/usr/local/hestia/func/ip.sh
source $HESTIA/func/ip.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'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Parsing domain values
get_domain_values 'web'
local_ip=$(get_real_ip "$IP")

# Deleting ftp users
if [ -n "$FTP_USER" ]; then
	for ftp_user in ${FTP_USER//:/ }; do
		$BIN/v-delete-web-domain-ftp "$user" "$domain" "$ftp_user"
	done
fi

# Deleting web backend
if [ -n "$WEB_BACKEND" ]; then
	$BIN/v-delete-web-domain-backend "$user" "$domain" 'no'
fi

# Deleting vhost configuration
del_web_config "$WEB_SYSTEM" "$TPL.tpl"

# Deleting SSL configuration and certificates
if [ "$SSL" = 'yes' ]; then
	del_web_config "$WEB_SYSTEM" "$TPL.stpl"
	$BIN/v-delete-web-domain-ssl-force "$user" "$domain" 'no' 'yes'
	rm -f $HOMEDIR/$user/conf/web/$domain/ssl/$domain.*
	rm -f $USER_DATA/ssl/$domain.*
fi

if [ "$FASTCGI_CACHE" = "yes" ]; then
	# make sure no trails left behind
	$BIN/v-delete-fastcgi-cache "$user" "$domain"
fi

# Deleting domain from web.conf
sed -i "/DOMAIN='$domain'/ d" "$USER_DATA/web.conf"

pconfd=$(get_conf_d_name "$PROXY_SYSTEM")

# Deleting proxy
if [ -n "$PROXY_SYSTEM" ]; then
	del_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
	if [ "$SSL" = 'yes' ]; then
		del_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
	fi
	if [ -e "/etc/$PROXY_SYSTEM/$pconfd/01_caching_pool.conf" ]; then
		sed -i "/=$domain:/d" "/etc/$PROXY_SYSTEM/$pconfd/01_caching_pool.conf"
	fi
fi

# Deleting web stats
if [ -n "$STATS" ] && [ "$STATS" != 'no' ]; then
	sed -i "/ $domain$/d" $HESTIA/data/queue/webstats.pipe
	rm -f $HOMEDIR/$user/conf/web/$domain/$STATS.conf
	rm -f /etc/awstats/$STATS.$domain.conf
fi

# Deleting logs
rm -f /var/log/$WEB_SYSTEM/domains/$domain.log*
rm -f /var/log/$WEB_SYSTEM/domains/$domain.bytes
rm -f /var/log/$WEB_SYSTEM/domains/$domain.error*

# Deleting directory
rm -rf $HOMEDIR/$user/web/$domain
rm -rf $HOMEDIR/$user/conf/web/$domain

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

# Decreasing user counters
decrease_ip_value "$local_ip"
decrease_user_value "$user" '$U_WEB_DOMAINS'
if [ "$SSL" = 'yes' ]; then
	decrease_user_value "$user" '$U_WEB_SSL'
fi
if [ -n "$ALIAS" ]; then
	aliases=$(echo "$ALIAS" | tr ',' '\n' | wc -l)
	decrease_user_value "$user" '$U_WEB_ALIASES' "$aliases"
fi

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

# Detect php version

if [[ $BACKEND =~ ^.*PHP-([0-9])\_([0-9])$ ]]; then
	version="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}"
else
	version=$(multiphp_default_version)
fi

# Delete ruby if exists
if [ -e /usr/local/hestia/bin/v-ext-modules ]; then
	res=$(/usr/local/hestia/bin/v-ext-modules state passenger_manager csv | tail -n 1 | /usr/bin/xargs | cut -d"," -f6 )
	if [ -n "$res" ]; then
		enabled=$(echo "$res" | grep enabled)
		if [ -n "$enabled" ]; then
			/usr/local/hestia/bin/v-ext-modules-run passenger_manager disable_user "$domain"
		fi
	fi
fi

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

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

# Restarting php interpreter
$BIN/v-restart-web-backend "$restart" "$version"
check_result $? "PHP restart failed" > /dev/null

# Logging
$BIN/v-log-action "$user" "Info" "Web" "Deleted web domain (Name: $domain)."
log_event "$OK" "$ARGUMENTS"

exit