#!/bin/bash
# info: list dns domain
# options: USER DOMAIN [FORMAT]
#
# example: v-list-dns-domain alice wonderland.com
#
# This function of obtaining the list of dns domain parameters.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
user=$1
domain=$2
format=${3-shell}

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# JSON list function
json_list() {
	echo '{'
	echo '    "'$DOMAIN'": {
        "IP": "'$IP'",
        "TPL": "'$TPL'",
        "TTL": "'$TTL'",
        "EXP": "'$EXP'",
        "SOA": "'$SOA'",
        "SERIAL": "'$SERIAL'",
        "DNSSEC": "'$DNSSEC'",
        "SRC": "'$SRC'",
        "RECORDS": "'$RECORDS'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
	echo '}'
}

# SHELL list function
shell_list() {
	echo "DOMAIN:         $DOMAIN"
	echo "IP:             $IP"
	echo "TEMPLATE:       $TPL"
	echo "TTL:            $TTL"
	echo "EXP:            $EXP"
	echo "SOA:            $SOA"
	echo "SERIAL:         $SERIAL"
	echo "DNSSEC:         $DNSSEC"
	echo "RECORDS:        $RECORDS"
	echo "SUSPENDED:      $SUSPENDED"
	echo "TIME:           $TIME"
	echo "DATE:           $DATE"
}

# PLAIN list function
plain_list() {
	echo -ne "$DOMAIN\t$IP\t$TPL\t$TTL\t$EXP\t$SOA\t$SERIAL\t$DNSSEC\t$RECORDS\t"
	echo -e "$SUSPENDED\t$TIME\t$DATE"
}

# CSV list function
csv_list() {
	echo "DOMAIN,IP,TPL,TTL,EXP,SOA,SERIAL,DNSSEC,RECORDS,SUSPENDED,TIME,DATE"
	echo -n "$DOMAIN,$IP,$TPL,$TTL,$EXP,$SOA,$SERIAL,$DNSSEC,$RECORDS,$SUSPENDED,"
	echo "$TIME,$DATE"
}

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

check_args '2' "$#" 'USER DOMAIN [FORMAT]'
is_format_valid 'user' 'domain'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'dns' 'DOMAIN' "$domain"

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

# Parsing domain
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)

# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list ;;
esac

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

exit
