#!/bin/bash
# info: change dns domain template
# options: USER DOMAIN TEMPLATE [RESTART]
#
# The function for changing the template of records. By updating old records
# will be removed and new records will be generated in accordance with
# parameters of new template.


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

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

# Includes
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/conf/vesta.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 "$DNS_SYSTEM" 'DNS_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'dns' 'DOMAIN' "$domain"
is_object_unsuspended 'dns' 'DOMAIN' "$domain"
is_dns_template_valid "$template"


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

# Defining variables
get_domain_values 'dns'
i=1
ns=$(get_user_value '$NS')
for nameserver in ${ns//,/ };do
    eval ns$i=$nameserver
    (( ++i))
done

# Reading template
template_data=$(cat $DNSTPL/$template.tpl)

# Deleting unused nameservers
if [ -z "$ns3" ]; then
    template_data=$(echo "$template_data" |grep -v %ns3%)
fi
if [ -z "$ns4" ]; then
    template_data=$(echo "$template_data" |grep -v %ns4%)
fi
if [ -z "$ns5" ]; then
    template_data=$(echo "$template_data" |grep -v %ns5%)
fi
if [ -z "$ns6" ]; then
    template_data=$(echo "$template_data" |grep -v %ns6%)
fi
if [ -z "$ns7" ]; then
    template_data=$(echo "$template_data" |grep -v %ns7%)
fi
if [ -z "$ns8" ]; then
    template_data=$(echo "$template_data" |grep -v %ns8%)
fi

# Changing tpl
echo "$template_data" |\
    sed -e "s/%ip%/$IP/g" \
        -e "s/%domain_idn%/$domain_idn/g" \
        -e "s/%domain%/$domain/g" \
        -e "s/%ns1%/$ns1/g" \
        -e "s/%ns2%/$ns2/g" \
        -e "s/%ns3%/$ns3/g" \
        -e "s/%ns4%/$ns4/g" \
        -e "s/%ns5%/$ns5/g" \
        -e "s/%ns6%/$ns6/g" \
        -e "s/%ns7%/$ns7/g" \
        -e "s/%ns8%/$ns8/g" \
        -e "s/%time%/$TIME/g" \
        -e "s/%date%/$DATE/g" > $USER_DATA/dns/$domain.conf
records="$(wc -l $USER_DATA/dns/$domain.conf |cut -f 1 -d ' ')"

# Updating zone
if [[ "$DNS_SYSTEM" =~ named|bind ]]; then
    update_domain_serial
    update_domain_zone
fi

# Updating dns-cluster queue
if [ ! -z "$DNS_CLUSTER" ]; then
    # Check for first sync
    dlock=$(grep "domain $user $domain" $VESTA/data/queue/dns-cluster.pipe)
    if [ -z "$dlock" ]; then
        cmd="$BIN/v-add-remote-dns-domain $user $domain domain"
        echo "$cmd" >> $VESTA/data/queue/dns-cluster.pipe
    fi
fi


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Updating vesta config
update_object_value 'dns' 'DOMAIN' "$domain" '$TPL' "$template"
update_object_value 'dns' 'DOMAIN' "$domain" '$RECORDS' "$records"

# Restarting named
$BIN/v-restart-dns $restart
check_result $? "DNS restart failed" >/dev/null

# Logging
log_history "changed dns template for $domain to $template" '' 'admin'
log_event "$OK" "$ARGUMENTS"

exit
