#!/bin/bash
# info: update user disk quota
# options: USER
#
# The functions upates disk quota for specific user


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument defenition
user=$1

# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf


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

check_args '1' "$#" 'USER'
validate_format 'user'
is_object_valid 'user' 'USER' "$user"


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

# Updating disk quota
# Had quota equals package value. Soft quota equals 90% of package value for warnings.
quota=$(get_user_value '$DISK_QUOTA')
soft=$((quota * 1024 * 0.90))
hard=$((quota * 1024))

# Searching home mount point
mnt=$(df -P /home |awk '{print $6}' |tail -n1)

# Checking unlinmited quota
if [ "$quota" = 'unlimited' ]; then
    setquota $user 0 0 0 0 $mnt
else
    setquota $user $soft $hard 0 0 $mnt
fi


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Logging
log_event "$OK" "$EVENT"

exit
