#!/bin/bash
# info: rebuild dns domains
# options: user
#
# The function rebuilds BIND configuration files for all dns domains.


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

# Argument defenition
user=$1

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


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

check_args '1' "$#" 'user'
validate_format 'user'
is_system_enabled "$DNS_SYSTEM"
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"


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

user_domains=0
user_records=0
suspended_dns=0

# Checking dns folder
if [ ! -d "$USER_DATA/dns" ]; then
    rm -f $USER_DATA/dns
    mkdir $USER_DATA/dns
fi

# Defining config and fie
conf="$USER_DATA/dns.conf"


# Defining user name servers
ns=$(get_user_value '$NS')
i=1
for nameserver in ${ns//,/ };do
    eval ns$i="$nameserver"
    i=$((i + 1))
done

# Starting loop
for domain in $(search_objects 'dns' 'DOMAIN' "*" 'DOMAIN'); do
    # Defining variables
    get_domain_values 'dns'
    domain_idn=$(idn -t --quiet -a "$domain")

    # Checking zone file
    if [ ! -e "$USER_DATA/dns/$domain.conf" ]; then
        cat $DNSTPL/$TPL.tpl |\
            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
    fi

    # Sorting records
    sort_dns_records

    # Updating zone
    update_domain_zone

    # Set file permissions
    chmod 640 $HOMEDIR/$user/conf/dns/$domain.db
    chown root:named $HOMEDIR/$user/conf/dns/$domain.db

    # Bind config check
    nconf='/etc/named.conf'
    if [ "$SUSPENDED" = 'yes' ]; then
	rm_string=$(grep -n /etc/namedb/$domain.db $nconf | cut -d : -f 1)
        if [ ! -z "$rm_string" ]; then
            sed -i "$rm_string d" $nconf
        fi
        suspended_dns=$((suspended_dns + 1))
    else
	if [ -z "$(grep /$domain.db $nconf)" ]; then
            named="zone \"$domain_idn\" {type master; file"
            named="$named \"$HOMEDIR/$user/conf/dns/$domain.db\";};"
            echo "$named" >> /etc/named.conf
        fi
    fi
    user_domains=$((user_domains + 1))
    records=$(wc -l $USER_DATA/dns/$domain.conf | cut -f 1 -d ' ')
    user_records=$((user_records + records))
    update_object_value 'dns' 'DOMAIN' "$domain" '$RECORDS' "$records"
done


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

# Updating counters
update_user_value "$user" '$U_DNS_DOMAINS' "$user_domains"
update_user_value "$user" '$U_DNS_RECORDS' "$user_records"
update_user_value "$user" '$SUSPENDED_DNS' "$suspended_dns"

# Restart named
$BIN/v_restart_dns "$EVENT"

# Logging
log_event "$OK" "$EVENT"

exit
