#!/bin/bash
# info: update mail domain disk usage
# options: USER DOMAIN
#
# example: v-update-mail-domain-disk admin example.com
#
# This function updates domain disk usage.

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

# Argument definition
user=$1
domain=$2
domain_idn=$2

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

# Additional argument formatting
format_domain
format_domain_idn
# TODO: $domain_idn not used in this script - maybe $domain should be converted to $doman_idn ?

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

check_args '2' "$#" 'USER DOMAIN'
is_format_valid 'user' 'domain'
is_system_enabled "$MAIL_SYSTEM" 'MAIL_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'mail' 'DOMAIN' "$domain"
if [ -z "$MAIL_SYSTEM" ] || [ "$MAIL_SYSTEM" = 'remote' ]; then
	exit
fi

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

# Starting loop
dom_disk=0
for account in $($BIN/v-list-mail-accounts $user $domain plain | cut -f 1); do
	home_dir=$HOMEDIR/$user/mail/$domain/$account
	if [ -e "$home_dir" ]; then
		udisk=$(nice -n 19 du -shm $home_dir | cut -f 1)
	else
		udisk=0
	fi
	update_object_value "mail/$domain" 'ACCOUNT' "$account" '$U_DISK' "$udisk"
	dom_disk=$((dom_disk + udisk))
done

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

update_object_value 'mail' 'DOMAIN' "$domain" '$U_DISK' "$dom_disk"
recalc_user_disk_usage

# Logging
log_event "$OK" "$ARGUMENTS"

exit