#!/bin/bash
# info: update disk usage for domain
# options: USER DOMAIN
#
# example: v-update-web-domain-disk alice wonderland.com
#
# This function recalculates disk usage for specific webdomain.

#----------------------------------------------------------#
#                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
# shellcheck source=/usr/local/hestia/func/domain.sh
source $HESTIA/func/domain.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 "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"

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

# Defining home directory
home_dir="$HOMEDIR/$user/web/$domain/"

# Checking home directory exist
if [ -e "$home_dir" ]; then
	disk_usage=$(nice -n 19 du -shm $home_dir | cut -f 1)
fi

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

# Updating disk value in config
update_object_value 'web' 'DOMAIN' "$domain" '$U_DISK' "$disk_usage"
recalc_user_disk_usage

# Logging
log_event "$OK" "$ARGUMENTS"

exit
