#!/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                              #
#----------------------------------------------------------#

# Restart named
$BIN/v_restart_dns "$EVENT"

# Logging
log_event "$OK" "$EVENT"

exit
