#!/bin/bash
# info: update domains disk usage
# options: USER
#
# example: v-update-web-domains-disk alice
#
# This function recalculates disk usage for all user webdomains.

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

# Argument definition
user=$1

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

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

check_args '1' "$#" 'USER'
is_format_valid 'user'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"

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

# Loop through all domains that are not suspended
for domain in $($BIN/v-list-web-domains $user plain | cut -f 1); do
	home_dir="$HOMEDIR/$user/web/$domain/"
	if [ -e "$home_dir" ]; then
		disk_usage=$(nice -n 19 du -shm $home_dir | cut -f 1)
	fi
	update_object_value 'web' 'DOMAIN' "$domain" '$U_DISK' "$disk_usage"
done

# Recalculating user disk space
recalc_user_disk_usage

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

# Logging
log_event "$OK" "$ARGUMENTS"

exit