#!/bin/bash
# info: change web domain backend template
# options: USER DOMAIN TEMPLATE [RESTART]
#
# The function changes backend template


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
user=$1
domain=$2
domain_idn=$2
template=$3
restart=$4

# Includes
source $HESTIA/func/main.sh
source $HESTIA/func/domain.sh
source $HESTIA/func/ip.sh
source $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                             #
#----------------------------------------------------------#

# Deleting current backend
delete_web_backend

# Prepare new backend configuration
prepare_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 [ ! -z "$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" ]; 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 [ ! -z "$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 [ ! -z "$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"
fi


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


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

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

# Logging
log_history "changed backend template for $domain to $template"
log_event "$OK" "$ARGUMENTS"

exit
