#!/bin/bash
# info: list mail domain
# options: USER DOMAIN [FORMAT]
#
# The function of obtaining the list of domain parameters.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
user=$1
domain=$2
format=${3-shell}

# Includes
source $HESTIA/func/main.sh
source $HESTIA/conf/hestia.conf

# JSON list function
json_list() {
    parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/mail.conf)
    echo '{'
    echo '    "'$DOMAIN'": {
        "ANTIVIRUS": "'$ANTIVIRUS'",
        "ANTISPAM": "'$ANTISPAM'",
        "DKIM": "'$DKIM'",
        "CATCHALL": "'$CATCHALL'",
        "ACCOUNTS": "'$ACCOUNTS'",
        "U_DISK": "'$U_DISK'",
        "SSL": "'$SSL'",
        "LETSENCRYPT": "'$LETSENCRYPT'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'",
        "WEBMAIL_ALIAS": "'$WEBMAIL_ALIAS.$domain'"
        }'
    echo '}'
}

# SHELL list function
shell_list() {
    echo "DOMAIN:         $DOMAIN"
    echo "ANTIVIRUS:      $ANTIVIRUS"
    echo "ANTISPAM:       $ANTISPAM"
    echo "DKIM:           $DKIM"
    echo "CATCHALL:       $CATCHALL"
    echo "ACCOUNTS:       $ACCOUNTS"
    echo "DISK:           $U_DISK"
    echo "SSL:            $SSL"
    echo "LETSENCRYPT:    $LETSENCRYPT"
    echo "SUSPENDED:      $SUSPENDED"
    echo "TIME:           $TIME"
    echo "DATE:           $DATE"
    echo "WEBMAIL_ALIAS:  $WEBMAIL_ALIAS.$domain"
}

# PLAIN list function
plain_list() {
    echo -ne "$DOMAIN\t$ANTIVIRUS\t$ANTISPAM\t$DKIM\t$CATCHALL\t"
    echo -e "$ACCOUNTS\t$U_DISK\t$SSL\t$LETSENCRYPT\t$SUSPENDED\t$TIME\t$DATE\t$WEBMAIL_ALIAS.$domain"
}

# CSV list function
csv_list() {
    echo -n "DOMAIN,ANTIVIRUS,ANTISPAM,DKIM,CATCHALL,ACCOUNTS,U_DISK,"
    echo "SSL,LETSENCRYPT,SUSPENDED,TIME,DATE"
    echo -n "$DOMAIN,$ANTIVIRUS,$ANTISPAM,$DKIM,$CATCHALL,$ACCOUNTS,$U_DISK"
    echo "$SSL,$LETSENCRYPT,$SUSPENDED,$TIME,$DATE,$WEBMAIL_ALIAS.$domain"
}


#----------------------------------------------------------#
#                    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 mail domain
parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/mail.conf)

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


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

exit
