#!/bin/bash
# info: change web domain name
# options: USER DOMAIN NEW_DOMAIN [RESTART]
#
# example: v-change-web-domain-name alice wonderland.com lookinglass.com yes
#
# This function is used for changing the domain name.

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

# Argument defenition
user=$1
domain=$2
domain_idn=$2
new_domain=$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 NEW_DOMAIN [RESTART]'
is_format_valid 'user' 'domain'
is_domain_format_valid "$new_domain"
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"
check_new_domain=$(grep "DOMAIN='$new_domain'" $HESTIA/data/users/*/web.conf)
if [ -n "$check_new_domain" ]; then
	check_result "$E_EXISTS" "$new_domain already exists"
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Fetching domain variables
get_domain_values 'web'

# Changing domain in web.conf
sed -i "s/DOMAIN='$domain'/DOMAIN='$new_domain'/" $USER_DATA/web.conf
new_alias=$(echo "$ALIAS,$domain" \
	| sed -e "s/,/\n/g" \
	| sed -e "s/^$new_domain$//g" \
	| sed -e "/^$/d" \
	| sed -e ':a;N;$!ba;s/\n/,/g')

# Updating domain alias
if [ "$ALIAS" != "$new_alias" ]; then
	sed -i "s/ALIAS='$ALIAS'/ALIAS='$new_alias'/" $USER_DATA/web.conf
fi

# Moving document root and domain logs
mv "$HOMEDIR/$user/web/$domain" "$HOMEDIR/$user/web/$new_domain/"
cd /var/log/$WEB_SYSTEM/domains
mv "$domain.log" "$new_domain.log"
mv "$domain.error.log" "$new_domain.error.log"
mv "$domain.bytes" "$new_domain.bytes"
rm -f "$HOMEDIR/$user/web/$new_domain/logs/$domain.*"

# Updating domain certificates
if [ -e "$USER_DATA/ssl/$domain.crt" ]; then
	cd $USER_DATA/ssl
	mv $USER_DATA/ssl/$domain.crt $USER_DATA/ssl/$new_domain.crt
	mv $USER_DATA/ssl/$domain.ca $USER_DATA/ssl/$new_domain.ca
	mv $USER_DATA/ssl/$domain.pem $USER_DATA/ssl/$new_domain.pem
	mv $USER_DATA/ssl/$domain.key $USER_DATA/ssl/$new_domain.key
	rm -f $HOMEDIR/$user/conf/web/$domain/ssl/$domain.*
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"
fi

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

# Delete backend
if [ -n "$BACKEND" ]; then
	prepare_web_backend
	delete_web_backend
fi

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

# Rebuilding vhosts
$BIN/v-rebuild-web-domains "$user" "$restart"

# Logging
$BIN/v-log-action "$user" "Info" "Web" "Renamed web domain (Old Domain: $domain, New Domain: $new_domain)."
log_event "$OK" "$EVENT"

exit
