#!/bin/bash
# info: add dns domain
# options: USER DOMAIN IP [NS1] [NS2] [NS3] [NS4] [RESTART]
#
# The function adds DNS zone with records defined in the template. If the exp
# argument isn't stated, the expiration date value will be set to next year.
# The soa argument is responsible for the relevant record. By default the first
# user's NS server is used. TTL is set as common for the zone and for all of
# its records with a default value of 14400 seconds.


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

# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | sed -e 's/\.*$//g' -e 's/^\.*//g')
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
ip=$3
ns1=$4
ns2=$5
ns3=$6
ns4=$7
restart=$8

# Includes
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/conf/vesta.conf


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

check_args '3' "$#" 'USER DOMAIN IP [NS1] [NS2] [NS3] [NS4]'
validate_format 'user' 'domain' 'ip'
is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_domain_new 'dns'
is_package_full 'DNS_DOMAINS'

template=$(get_user_value '$DNS_TEMPLATE')
is_dns_template_valid

if [ ! -z "$ns1" ]; then
    ns1=$(echo $4 | sed -e 's/\.*$//g' -e 's/^\.*//g')
    validate_format 'ns1'
fi
if [ ! -z "$ns2" ]; then
    ns2=$(echo $5 | sed -e 's/\.*$//g' -e 's/^\.*//g')
    validate_format 'ns2'
fi

if [ ! -z "$ns3" ]; then
    ns3=$(echo $6 | sed -e 's/\.*$//g' -e 's/^\.*//g')
    validate_format 'ns3'
fi
if [ ! -z "$ns4" ]; then
    ns4=$(echo $7 | sed -e 's/\.*$//g' -e 's/^\.*//g')
    validate_format 'ns4'
fi


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

# Defining NS variables
if [ -z $ns2 ]; then
    i=1
    ns=$(get_user_value '$NS')
    for nameserver in ${ns//,/ };do
        eval ns$i=$nameserver
        (( ++i))
    done
fi
soa="$ns1"
exp=$(date +%F -d "+ 1 year")
ttl=14400

# 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

# Add dns zone to the user config
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/%time%/$TIME/g" \
        -e "s/%date%/$DATE/g" > $USER_DATA/dns/$domain.conf

chmod 660 $USER_DATA/dns/$domain.conf
records="$(wc -l $USER_DATA/dns/$domain.conf |cut -f 1 -d ' ')"

# Adding dns.conf record
dns_rec="DOMAIN='$domain' IP='$ip' TPL='$template' TTL='$ttl' EXP='$exp'"
dns_rec="$dns_rec SOA='$soa' RECORDS='$records' SUSPENDED='no' TIME='$TIME'"
dns_rec="$dns_rec DATE='$DATE'"

echo "$dns_rec" >> $USER_DATA/dns.conf
chmod 660 $USER_DATA/dns.conf

# Create system configs
if [[ "$DNS_SYSTEM" =~ named|bind ]]; then
    if [ -e '/etc/named.conf' ]; then
        dns_conf='/etc/named.conf'
        dns_group='named'
    else
        dns_conf='/etc/bind/named.conf'
        dns_group='bind'
    fi

    # Adding zone in named.conf
    named="zone \"$domain_idn\" {type master; file"
    named="$named \"$HOMEDIR/$user/conf/dns/$domain.db\";};"
    echo "$named" >> $dns_conf

    # Updating domain dns zone
    update_domain_zone

    # Set permissions
    chmod 640 $conf
    chown root:$dns_group $conf
fi

# Updating dns-cluster queue
if [ ! -z "$DNS_CLUSTER" ]; then
    cmd="$BIN/v-add-remote-dns-domain $user $domain no"
    echo "$cmd" >> $VESTA/data/queue/dns-cluster.pipe
fi


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

# Increasing domain value
increase_user_value "$user" '$U_DNS_DOMAINS'
increase_user_value "$user" '$U_DNS_RECORDS' "$records"

# Restart named
if [ "$restart" != 'no' ]; then
    $BIN/v-restart-dns
    if [ $? -ne 0 ]; then
        exit $E_RESTART
    fi
fi

# Logging
log_history "added dns domain $domain"
log_event "$OK" "$EVENT"

exit
