#!/bin/bash
# info: list mail domain accounts
# options: USER DOMAIN [FORMAT]
#
# example: v-list-mail-accounts admin acme.com
#
# This function of obtaining the list of all user domains.

#----------------------------------------------------------#
#                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() {
	IFS=$'\n'
	i=1
	objects=$(grep ACCOUNT $USER_DATA/mail/$domain.conf | wc -l)
	echo "{"
	while read str; do
		parse_object_kv_list "$str"
		echo -n '    "'$ACCOUNT'": {
        "ALIAS": "'$ALIAS'",
        "FWD": "'$FWD'",
        "FWD_ONLY": "'$FWD_ONLY'",
        "AUTOREPLY": "'$AUTOREPLY'",
        "QUOTA": "'$QUOTA'",
        "U_DISK": "'$U_DISK'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
		if [ "$i" -lt "$objects" ]; then
			echo ','
		else
			echo
		fi
		((i++))
	done < <(cat $USER_DATA/mail/$domain.conf)
	echo '}'
}

# SHELL list function
shell_list() {
	IFS=$'\n'
	echo "ACCOUNT   ALIAS   FWD   DISK  DATE"
	echo "-------   -----   ---   ----  ----"
	while read str; do
		parse_object_kv_list "$str"
		if [ -z "$ALIAS" ]; then
			ALIAS='no'
		else
			if [ "${#ALIAS}" -gt 12 ]; then
				ALIAS="${ALIAS:0:12}..."
			fi
		fi
		if [ -z "$FWD" ]; then
			FWD='no'
		else
			if [ "${#FWD}" -gt 20 ]; then
				FWD="${FWD:0:20}..."
			fi
		fi
		echo "$ACCOUNT $ALIAS $FWD $U_DISK $DATE"
	done < <(cat $USER_DATA/mail/$domain.conf)
}

# PLAIN list function
plain_list() {
	IFS=$'\n'
	while read str; do
		parse_object_kv_list "$str"
		echo -ne "$ACCOUNT\t$ALIAS\t$FWD\t$FWD_ONLY\t$AUTOREPLY\t"
		echo -e "$QUOTA\t$U_DISK\t$SUSPENDED\t$TIME\t$DATE"
	done < <(cat $USER_DATA/mail/$domain.conf)
}

# CSV list function
csv_list() {
	IFS=$'\n'
	echo -n "ACCOUNT,ALIAS,FWD,FWD_ONLY,AUTOREPLY,QUOTA,U_DISK,"
	echo "SUSPENDED,TIME,DATE"
	while read str; do
		parse_object_kv_list "$str"
		echo -n "$ACCOUNT,\"$ALIAS\",\"$FWD\",$FWD_ONLY,$AUTOREPLY,"
		echo "$QUOTA,$U_DISK,$SUSPENDED,$TIME,$DATE"
	done < <(cat $USER_DATA/mail/$domain.conf)
}

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

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

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

exit
