#!/bin/bash
# info: change web domain ip
# options: USER DOMAIN DOMAIN [RESTART]
#
# example: v-change-web-domain-ip admin example.com 167.86.105.230 yes
#
# This function is used for changing domain ip

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

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

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

check_args '3' "$#" 'USER DOMAIN IP [RESTART]'
is_format_valid 'user' 'domain' 'ip'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
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_ip_valid "$ip" "$user"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Preparing variables for vhost replace
get_domain_values 'web'
old=$(get_real_ip "$IP")
new=$(get_real_ip "$ip")

# Replacing vhost
replace_web_config "$WEB_SYSTEM" "$TPL.tpl"
if [ "$SSL" = 'yes' ]; then
	replace_web_config "$WEB_SYSTEM" "$TPL.stpl"
fi

# Replacing proxy vhost
if [ -n "$PROXY_SYSTEM" ] && [ -n "$PROXY" ]; then
	replace_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
	if [ "$SSL" = 'yes' ]; then
		replace_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
	fi
fi

# Check for webmail
if [ -n "$IMAP_SYSTEM" ]; then
	$BIN/v-rebuild-mail-domain "$user" "$domain"
fi

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

# Update config
update_object_value 'web' 'DOMAIN' "$domain" '$IP' "$3"

# Update counters
increase_ip_value "$new"
decrease_ip_value "$old"

# Restart 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" "Web domain IP address changed (IP: $3, Domain: $domain)."
log_event "$OK" "$ARGUMENTS"

exit