#!/bin/bash
# info: list mail domain account
# options: USER DOMAIN ACCOUNT [FORMAT]
#
# example: v-list-mail-account admin domain.tld tester
#
# This function of obtaining the list of account parameters.

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

# Argument definition
user=$1
domain=$2
account=$3
format=${4-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 '    "'$account'": {
        "ALIAS": "'$ALIAS'",
        "FWD": "'$FWD'",
        "FWD_ONLY": "'$FWD_ONLY'",
        "AUTOREPLY": "'$AUTOREPLY'",
        "QUOTA": "'$QUOTA'",
        "RATE_LIMIT": "'$RATE_LIMIT'",
        "U_DISK": "'$U_DISK'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'",
        "WEBMAIL_ALIAS": "'$WEBMAIL_ALIAS.$domain'"
    }'
	echo '}'
}

# SHELL list function
shell_list() {
	echo "ACCOUNT:        $account"
	echo "ALIAS:          $ALIAS"
	echo "FORWARD:        $FWD"
	echo "FORWARD_ONLY:   $FWD_ONLY"
	echo "AUTOREPLY:      $AUTOREPLY"
	echo "QUOTA:          $QUOTA"
	echo "DISK:           $U_DISK"
	echo "SUSPENDED:      $SUSPENDED"
	echo "TIME:           $TIME"
	echo "DATE:           $DATE"
	echo "WEBMAIL_ALIAS:  $WEBMAIL_ALIAS.$domain"
}

# PLAIN list function
plain_list() {
	echo -ne "$account\t$ALIAS\t$FWD\t$FWD_ONLY\t$AUTOREPLY\t$QUOTA\t"
	echo -e "$U_DISK\t$SUSPENDED\t$TIME\t$DATE\t$WEBMAIL_ALIAS.$domain"
}

# CSV list function
csv_list() {
	echo -n "ACCOUNT,ALIAS,FWD,FWD_ONLY,AUTOREPLY,QUOTA,U_DISK"
	echo "SUSPENDED,TIME,DATE"
	echo -n "$ACCOUNT,\"$ALIAS\",\"$FWD\",$FWD_ONLY,$AUTOREPLY,$QUOTA,"
	echo "$U_DISK,$SUSPENDED,$TIME,$DATE\t$WEBMAIL_ALIAS.$domain"
}

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

check_args '3' "$#" 'USER DOMAIN ACCOUNT [FORMAT]'
is_format_valid 'user' 'domain' 'account'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'mail' 'DOMAIN' "$domain"
is_object_valid "mail/$domain" 'ACCOUNT' "$account"

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

# Parsing mail account
parse_object_kv_list $(grep "ACCOUNT='$account'" $USER_DATA/mail/$domain.conf)

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

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

exit
