#!/bin/bash
# info: change web domain backend template
# options: USER DOMAIN TEMPLATE [RESTART]
#
# example: v-change-web-domain-backend-tpl admin acme.com PHP-7_4
#
# This function changes backend template

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

# Argument definition
user=$1
domain=$2
domain_idn=$2
template=$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"

# Additional argument formatting
format_domain
format_domain_idn

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

check_args '3' "$#" 'USER DOMAIN TEMPLATE [RESTART]'
is_format_valid 'user' 'domain' 'template'
is_system_enabled "$WEB_BACKEND" 'WEB_BACKEND'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_unsuspended 'web' 'DOMAIN' "$domain"
is_backend_template_valid "$template"
get_domain_values 'web'

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Prepare new backend configuration
prepare_web_backend

# Deleting current backend
delete_web_backend

# Allocating backend port
backend_port=9000
ports=$(grep -v '^;' $pool/* 2> /dev/null | grep listen | grep -o :[0-9].*)
ports=$(echo "$ports" | sed "s/://" | sort -n)
for port in $ports; do
	if [ "$backend_port" -eq "$port" ]; then
		backend_port=$((backend_port + 1))
	fi
done

# Changing backend config
cat $WEBTPL/$WEB_BACKEND/$template.tpl \
	| sed -e "s|%backend_port%|$backend_port|" \
		-e "s|%user%|$user|g" \
		-e "s|%domain%|$domain|g" \
		-e "s|%domain_idn%|$domain_idn|" \
		-e "s|%backend%|$backend_type|g" \
		-e "s|%backend_version%|$backend_version|g" > $pool/$backend_type.conf

# Set correct document root path
if [ -n "$CUSTOM_DOCROOT" ]; then
	docroot="$CUSTOM_DOCROOT"
	sed -i "s|/home\/$user\/web\/$domain\/public_html|$docroot|g" $pool/$backend_type.conf
fi

# Checking backend pool configuration
if [[ "$backend_type" = "$user" && $WEB_BACKEND_POOL = 'user' ]]; then
	conf=$USER_DATA/web.conf
	fields='$DOMAIN'
	nohead=1

	for domain in $(shell_list); do
		get_domain_values 'web'
		local_ip=$(get_real_ip "$IP")
		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" ] && [ ! -z "$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

		# Update config
		add_object_key "web" 'DOMAIN' "$domain" 'BACKEND' 'PROXY'
		update_object_value 'web' 'DOMAIN' "$domain" '$BACKEND' "$template"
	done

	# Chaning template in user config
	old_template=$(grep BACKEND_TEMPLATE "$USER_DATA/user.conf")
	if [ -z "$old_template" ]; then
		sed -i "s/^WEB_DOMAINS/BACKEND_TEMPLATE='$template'\nWEB_DOMAINS/g" \
			$USER_DATA/user.conf
	else
		update_user_value "$user" '$BACKEND_TEMPLATE' "$template"
	fi
else
	# Parsing domain values
	get_domain_values 'web'
	local_ip=$(get_real_ip "$IP")
	BACKEND="$template"
	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

	# Update config
	add_object_key "web" 'DOMAIN' "$domain" 'BACKEND' 'PROXY'
	update_object_value 'web' 'DOMAIN' "$domain" '$BACKEND' "$template"
fi

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

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

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

$BIN/v-restart-web-backend "$restart" "$version"
if [ "$version" != "$backend_version" ]; then
	$BIN/v-restart-web-backend "$restart" "$backend_version"
fi

check_result $? "PHP restart failed" > /dev/null

# Logging
$BIN/v-log-action "$user" "Info" "Web" "Backend template applied (Domain: $domain, Template: $template)."
log_event "$OK" "$ARGUMENTS"

exit
