#!/bin/bash
# info: list mail domain dkim
# options: USER DOMAIN [FORMAT]
#
# example: v-list-mail-domain-dkim admin maildomain.tld
#
# This function of obtaining domain dkim files.

#----------------------------------------------------------#
#                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 -e "\t\"$domain\": {"
	echo "        \"PEM\": \"$pem\","
	echo "        \"PUB\": \"$pub\","
	echo -e "\t}\n}"
}

# SHELL list function
shell_list() {
	echo -e "$pem"
	echo -e "\n$pub"
}

# PLAIN list function
plain_list() {
	echo "$pem\t$pub"
}

# CSV list function
csv_list() {
	echo "PEM,PUB"
	echo "\"$pem\",\"$pub\""
}

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

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

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

# Parsing domain keys
if [ -e "$USER_DATA/mail/$domain.pem" ]; then
	pem=$(cat $USER_DATA/mail/$domain.pem | sed ':a;N;$!ba;s/\n/\\n/g')
fi

if [ -e "$USER_DATA/mail/$domain.pub" ]; then
	pub=$(cat $USER_DATA/mail/$domain.pub | sed ':a;N;$!ba;s/\n/\\n/g')
fi

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

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

exit