#!/bin/bash
# info: rebuild system user
# options: USER [RESTART]
#
# example: v-rebuild-user admin yes
#
# This function rebuilds system user account.

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

# Argument definition
user=$1
restart=$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
# shellcheck source=/usr/local/hestia/func/ip.sh
source $HESTIA/func/ip.sh
# shellcheck source=/usr/local/hestia/func/rebuild.sh
source $HESTIA/func/rebuild.sh
# shellcheck source=/usr/local/hestia/func/syshealth.sh
source $HESTIA/func/syshealth.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '1' "$#" 'USER [RESTART]'
is_format_valid 'user' 'restart'
is_object_valid 'user' 'USER' "$user"
check_user=$(is_object_unsuspended 'user' 'USER' "$user")
if [ -n "$check_user" ]; then
	exit
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Update disk quota
if [ "$DISK_QUOTA" = 'yes' ]; then
	$BIN/v-update-user-quota "$user"
fi

# Rebuild user
rebuild_user_conf

# Update user counters
$BIN/v-update-user-counters "$user"

# Update account usage statistics
$BIN/v-update-user-stats "$user"

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

# Logging
$BIN/v-log-action "system" "Info" "System" "Rebuilt user profile (User: $user)."
log_event "$OK" "$ARGUMENTS"

exit