#!/bin/bash
# info: list dns template
# options: TEMPLATE [FORMAT]
#
# example: v-list-dns-template zoho
#
# This function for obtaining the DNS template parameters.

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

# Argument definition
template=$1
format=${2-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
# shellcheck source=/usr/local/hestia/func/domain.sh
source $HESTIA/func/domain.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# JSON list function
json_list() {
	IFS=$'\n'
	i=1
	objects=$(grep ID $DNSTPL/$template.tpl | wc -l)
	echo "{"
	while read str; do
		parse_object_kv_list "$str"
		VALUE=$(echo "$VALUE" | sed -e 's/"/\\"/g' -e "s/%quote%/'/g")
		echo -n '    "'$ID'": {
        "RECORD": "'$RECORD'",
        "TYPE": "'$TYPE'",
        "PRIORITY": "'$PRIORITY'",
        "VALUE": "'$VALUE'",
        "ID": "'$ID'"
    }'
		if [ "$i" -lt "$objects" ]; then
			echo ','
		else
			echo
		fi
		((i++))
	done < <(cat $DNSTPL/$template.tpl)
	echo '}'
}

# SHELL list function
shell_list() {
	IFS=$'\n'
	echo "ID^RECORD^TYPE^VALUE"
	echo "--^------^----^-----"
	while read str; do
		parse_object_kv_list "$str"
		echo "$ID^$RECORD^$TYPE^$VALUE"
	done < <(cat $DNSTPL/$template.tpl)
}

# PLAIN list function
plain_list() {
	IFS=$'\n'
	while read str; do
		parse_object_kv_list "$str"
		VALUE=$(echo "$VALUE" | sed -e "s/%quote%/\\'/g")
		echo -e "$ID\t$RECORD\t$TYPE\t$PRIORITY\t$VALUE"
	done < <(cat $DNSTPL/$template.tpl)
}

# CSV list function
csv_list() {
	IFS=$'\n'
	echo "ID,RECORD,TYPE,PRIORITY,VALUE"
	while read str; do
		parse_object_kv_list "$str"
		VALUE=$(echo "$VALUE" | sed -e "s/%quote%/\\'/g")
		echo "$ID,$RECORD,$TYPE,$PRIORITY,\"$VALUE\""
	done < <(cat $DNSTPL/$template.tpl)
}

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

check_args '1' "$#" 'TEMPLATE [FORMAT]'
is_format_valid 'template'
is_dns_template_valid "$template"

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

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

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

exit
