#!/bin/bash
# info: Changes the document root for an existing web domain
# options: USER DOMAIN TARGET_DOMAIN [DIRECTORY] [PHP]
#
# example: v-change-web-domain-docroot admin domain.tld otherdomain.tld
#          # add custom docroot
#          # points domain.tld to otherdomain.tld's document root.
#
# example: v-change-web-domain-docroot admin test.local default
#          # remove custom docroot
#          # returns document root to default value for domain.
#
# This call changes the document root of a chosen web domain
# to another available domain under the user context.

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

# Argument definition
user=$1
domain=$2

# Export target domain and directory
# so they are correctly passed through to domain.sh
export target_domain=$3
export target_directory=$4
export php=$5
restart=$6

# 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
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# Additional argument formatting
format_domain

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

check_args '2' "$#" 'USER DOMAIN [TARGET_DOMAIN] [DIRECTORY] [PHP-DOCROOT] [RESTART]'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
# Check to ensure that target domain is valid if we're
# not setting the docroot value back to defaults
if [ "$target_domain" != "default" ]; then
	is_format_valid 'user' 'domain' 'target_domain' 'restart'
	is_object_valid 'web' 'DOMAIN' "$target_domain"
else
	is_format_valid 'user' 'domain' 'restart'
fi
is_object_valid 'user' 'USER' "$user" "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_unsuspended 'web' 'DOMAIN' "$domain"
is_object_value_empty 'web' 'DOMAIN' "$domain" '$docroot'
is_dir_symlink "$HOMEDIR/$user/web"
is_dir_symlink "$HOMEDIR/$user/web/$target_domain"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Unset existing custom document root path
if [ -n "$CUSTOM_DOCROOT" ]; then
	update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_DOCROOT' ""
	update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_PHPROOT' ""
fi

# If target domain value is 'default', remove the custom document root
# value and rebuild web domain to restore default configuration.
# Otherwise, set target document root path accordingly based on passed values.
if [ "$target_domain" = "default" ]; then
	update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_DOCROOT' ""
	update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_PHPROOT' ""
else
	# Check for existence of specified directory under target domain's public_html folder
	target_domain_directory="$HOMEDIR/$user/web/$target_domain"
	if [ -n "$target_directory" ]; then
		# Checking destination path
		real_target_directory="$(readlink -e "${target_domain_directory}/public_html/$target_directory/")"
		if [ ! -e "$real_target_directory" ]; then
			check_result "$E_NOTEXIST" "Directory $target_directory does not exist under $HOMEDIR/$user/web/$target_domain/public_html/."
		elif [ -z "$(echo $real_target_directory | egrep "^$target_domain_directory\b")" ]; then
			check_result "$E_FORBIDEN" "Target dir outside of target domain dir"
		else
			CUSTOM_DOCROOT="$real_target_directory"
			if [ -n "$php" ]; then
				custom_phproot="${target_domain_directory}/public_html/"
			else
				custom_phproot="$real_target_directory"
			fi
		fi
	else
		CUSTOM_DOCROOT="${target_domain_directory}/public_html/"
		custom_phproot="${target_domain_directory}/public_html/"
	fi

	add_object_key 'web' 'DOMAIN' "$domain" 'CUSTOM_DOCROOT' 'IP6'
	add_object_key 'web' 'DOMAIN' "$domain" 'CUSTOM_PHPROOT' 'IP6'

	update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_DOCROOT' "$CUSTOM_DOCROOT"
	# CUSTOM_PHPROOT got overwriten by default
	CUSTOM_PHPROOT=$custom_phproot
	update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_PHPROOT' "$CUSTOM_PHPROOT"
fi

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

# Rebuild domain configuration
$BIN/v-rebuild-web-domain "$user" "$domain" "$restart"

# Logging
if [ "$target_domain" = "default" ]; then
	$BIN/v-log-action "$user" "Info" "Web" "Domain document root reset (Domain: $domain)."
else
	$BIN/v-log-action "$user" "Info" "Web" "Domain document root updated (Domain: $domain, Target: $target_domain)."
fi

log_event "$OK" "$ARGUMENTS"

# Unset variables
unset target_domain
unset target_directory

exit
