#!/bin/bash
# info: update user disk usage
# options: USER
#
# example: v-update-user-disk admin
#
# The functions recalculates disk usage and updates database.

#----------------------------------------------------------#
#                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_object_valid 'user' 'USER' "$user"

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

# Saving old IFS
OLDIFS=$IFS
IFS=$(echo -en "\n\b")

#Starting update disk space excluding web, mail, conf directory
disk_usage='0'
disk_usage=$(du -shm $HOMEDIR/$user --exclude $HOMEDIR/$user/web --exclude $HOMEDIR/$user/mail --exclude $HOMEDIR/$user/conf | cut -f 1)

# Restoring old IFS
IFS=$OLDIFS

# Updating disk value in config
update_user_value "$user" '$U_DISK_DIRS' "$disk_usage"

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

# Recalculating user disk space
recalc_user_disk_usage

# Logging
log_event "$OK" "$ARGUMENTS"

exit
