#!/bin/bash
# info: rebuild web domain
# options: USER DOMAIN [RESTART]
#
# example: v-rebuild-web-domain user domain.tld
#
# This function rebuilds web configuration files.

#----------------------------------------------------------#
#                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
# shellcheck source=/usr/local/hestia/func/ip.sh
source $HESTIA/func/ip.sh
# shellcheck source=/usr/local/hestia/func/rebuild.sh
source $HESTIA/func/rebuild.sh
# shellcheck source=/usr/local/hestia/func/syshealth.sh
source $HESTIA/func/syshealth.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '2' "$#" 'USER DOMAIN [RESTART]'
is_format_valid 'user' 'domain' 'restart'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
check_user=$(is_object_unsuspended 'user' 'USER' "$user")
if [ -n "$check_user" ]; then
	exit
fi
is_object_valid 'web' 'DOMAIN' "$domain"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Deleting old web configs
if [ -e "/etc/$WEB_SYSTEM/conf.d/hestia.conf" ]; then
	sed -i "/.*\/$user\/conf\/web\//d" /etc/$WEB_SYSTEM/conf.d/hestia.conf
fi

if [ -e "$HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.conf" ]; then
	rm -f $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.conf
	rm -f /etc/$WEB_SYSTEM/conf.d/domains/$domain.conf
fi
if [ -e "$HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.ssl.conf" ]; then
	rm $HOMEDIR/$user/conf/web/$domain/$WEB_SYSTEM.ssl.conf
	rm -f /etc/$WEB_SYSTEM/conf.d/domains/$domain.ssl.conf
fi

# Deleting old proxy configs
if [ -n "$PROXY_SYSTEM" ]; then
	if [ -e "/etc/$PROXY_SYSTEM/conf.d/hestia.conf" ]; then
		sed -i "/.*\/$user\/conf\/web\//d" /etc/$PROXY_SYSTEM/conf.d/hestia.conf
	fi

	if [ -e "$HOMEDIR/$user/conf/web/$domain/$PROXY_SYSTEM.conf" ]; then
		rm -f $HOMEDIR/$user/conf/web/$domain/$PROXY_SYSTEM.conf
		rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$domain.conf
	fi

	if [ -e "$HOMEDIR/$user/conf/web/$domain/$PROXY_SYSTEM.ssl.conf" ]; then
		rm -f $HOMEDIR/$user/conf/web/$domain/$PROXY_SYSTEM.ssl.conf
		rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$domain.ssl.conf
	fi
fi

# Deleting backend configs
if [ -n "$WEB_BACKEND" ]; then
	template=$(get_object_value 'web' 'DOMAIN' "$domain" '$BACKEND')
	if [ -z "$template" ]; then
		template="default"
		update_object_value 'web' 'DOMAIN' "$domain" '$BACKEND' 'default'
	fi
	prepare_web_backend
	delete_web_backend
	$BIN/v-add-web-domain-backend $user $domain $template $restart
fi

rebuild_web_domain_conf

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

# Updating user counters
$BIN/v-update-user-counters "$user"

# 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 "system" "Info" "System" "Rebuilt web domain (User: $user, Domain: $domain)."
log_event "$OK" "$ARGUMENTS"

exit
