#!/bin/bash
# info: change user default template
# options: USER TYPE TEMPLATE
#
# example: v-change-user-template admin WEB wordpress
#
# This function changes default user web template.

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

# Argument definition
user=$1
type=$(echo "$2" | tr '[:lower:]' '[:upper:]')
template=$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
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '3' "$#" 'USER TYPE TEMPLATE'
is_format_valid 'user' 'template'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

case $type in
	WEB)
		is_web_template_valid "$template"
		update_user_value "$user" '$WEB_TEMPLATE' "$template"
		;;
	PROXY)
		is_proxy_template_valid "$template"
		update_user_value "$user" '$PROXY_TEMPLATE' "$template"
		;;
	BACKEND)
		is_backend_template_valid "$template"
		update_user_value "$user" '$BACKEND_TEMPLATE' "$template"
		;;
	DNS)
		is_dns_template_valid "$template"
		update_user_value "$user" '$DNS_TEMPLATE' "$template"
		;;
	*) check_args '1' '0' 'USER TYPE TEMPLATE' ;;
esac

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

# Logging
$BIN/v-log-action "$user" "Info" "Web" "Default domain template changed (Type: $type, Template: $template)."
log_event "$OK" "$ARGUMENTS"

exit
