#!/bin/bash
# info: list dns domain records
# options: USER DOMAIN [FORMAT]
#
# example: v-list-dns-records admin example.com
#
# This function for getting all DNS domain records.

#----------------------------------------------------------#
#                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() {
	IFS=$'\n'
	i=1
	objects=$(grep ID $USER_DATA/dns/$domain.conf | wc -l)
	echo "{"
	while read str; do
		unset TTL
		parse_object_kv_list "$str"
		[ -z "$TTL" ] && TTL="$zone_ttl"
		VALUE=$(echo "$VALUE" | sed -e 's/"/\\"/g' -e "s/%quote%/'/g" -e 's/     /\\u0009/g')
		echo -n '    "'$ID'": {
        "RECORD": "'$RECORD'",
        "TYPE": "'$TYPE'",
        "PRIORITY": "'$PRIORITY'",
        "VALUE": "'$VALUE'",
        "ID": "'$ID'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'",
        "TTL": "'$TTL'"
    }'
		if [ "$i" -lt "$objects" ]; then
			echo ','
		else
			echo
		fi
		((i++))
	done < <(cat $USER_DATA/dns/$domain.conf)
	echo '}'
}

# SHELL list function
shell_list() {
	IFS=$'\n'
	echo "ID^RECORD^TYPE^VALUE^DATE^TTL"
	echo "--^------^----^-----^----^----"
	while read str; do
		unset TTL
		parse_object_kv_list "$str"
		[ -z "$TTL" ] && TTL="$zone_ttl"
		echo "$ID^$RECORD^$TYPE^${VALUE:0:30}^$DATE^$TTL"
	done < <(cat $USER_DATA/dns/$domain.conf)
}

# PLAIN list function
plain_list() {
	IFS=$'\n'
	while read str; do
		unset TTL
		parse_object_kv_list "$str"
		[ -z "$TTL" ] && TTL="$zone_ttl"
		VALUE=$(echo "$VALUE" | sed -e "s/%quote%/\\'/g")
		echo -ne "$ID\t$RECORD\t$TYPE\t$PRIORITY\t$VALUE\t"
		echo -e "$SUSPENDED\t$TIME\t$DATE\t$TTL"
	done < <(cat $USER_DATA/dns/$domain.conf)
}

# CSV list function
csv_list() {
	IFS=$'\n'
	echo "ID,RECORD,TYPE,PRIORITY,VALUE,SUSPENDED,TIME,DATE,TTL"
	while read str; do
		unset TTL
		parse_object_kv_list "$str"
		[ -z "$TTL" ] && TTL="$zone_ttl"
		VALUE=$(echo "$VALUE" | sed -e "s/%quote%/\\'/g")
		echo -n "$ID,$RECORD,$TYPE,$PRIORITY,\"$VALUE\","
		echo "$SUSPENDED,$TIME,$DATE,$TTL"
	done < <(cat $USER_DATA/dns/$domain.conf)
}

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

zone_ttl="$(get_object_value 'dns' 'DOMAIN' "$domain" '$TTL')"

# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list | column -t -s '^' ;;
esac

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

exit
