#!/bin/bash
# info: list public dnssec key
# options: USER DOMAIN [FROMAT]
#
# example: v-list-dns-public-key admin acme.com
#
# This function list the public key to be used with DNSSEC and needs to be added to the domain register.

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

# Argument definition
user=$1
domain=$2
format=$3
dnstype=$4

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

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

format_domain
format_domain_idn

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

# JSON list function
json_list() {
	echo '{'
	echo '    "'$DOMAIN'": {
       "RECORD": "'$record'",
       "FLAG": "'$flag'",
       "ALGORITHM": "'$algorithm'",
       "KEY": "'$key'",
       "DS":"'$ds'"
    }'
	echo '}'
}

# SHELL list function
shell_list() {
	echo "RECORD:    $record"
	echo "DS:        $DS"
	echo "FLAG:      $flag"
	echo "ALGORITHM: $algorithm"
	echo "KEY:       $key"
}

# PLAIN list function
plain_list() {
	if [ "$dnstype" != "DS" ]; then
		echo -e "$record"
	else
		echo -e "$ds"
	fi
}

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

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

if [ -n "$KEY" ]; then
	record=$(cat "/var/cache/bind/K$domain_idn.+013+$KEY.key" | grep DNSKEY)
	flag=$(echo "$record" | cut -d' ' -f5)
	algorithm=$(echo "$record" | cut -d' ' -f7)
	key="$(echo "$record" | cut -d' ' -f8) $(echo "$record" | cut -d' ' -f9)"
	ds=$(dnssec-dsfromkey "/var/cache/bind/K$domain_idn.+013+$KEY.key")
fi

case $format in
	json) json_list ;;
	plain) plain_list ;;
	shell) shell_list ;;
esac

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

exit
