#!/bin/bash
# info: delete dns record
# options: USER DOMAIN ID
#
# The function for deleting a certain record of DNS zone.


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

# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain_idn=$(idn -t --quiet -a "$domain")
id=$3

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


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

check_args '3' "$#" 'USER DOMAIN ID'
validate_format 'user' 'domain' 'id'
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_object_valid "dns/$domain" 'ID' "$id"


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

# Deleting record
sed -i "/^ID='$id'/d" $USER_DATA/dns/$domain.conf

# Updating zone
update_domain_zone


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

# Upddate counters
records="$(wc -l $USER_DATA/dns/$domain.conf | cut -f1 -d ' ')"
update_object_value 'dns' 'DOMAIN' "$domain" '$RECORDS' "$records"
decrease_user_value "$user" '$U_DNS_RECORDS'

# Restart named
$BIN/v-restart-dns "$EVENT"

# Logging
log_history "deleted dns record $id on $domain"
log_event "$OK" "$EVENT"

exit
