#!/bin/bash
# info: update user statistics
# options: [USER]
#
# example: v-update-user-stats admin
#
# Function logs user parameters into statistics database.

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

# Importing system environment  as we run this script
#       mostly by cron wich not read it by itself
source /etc/profile.d/hestia.sh

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

# Argument definition
user=$1

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

check_args '0' "$#" '[USER]'
if [ ! -z "$user" ]; then
	is_format_valid 'user'
	is_object_valid 'user' 'USER' "$user"
fi

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

# Creating user_list
if [ -z "$user" ]; then
	user_list=$("$BIN/v-list-users" list)
	update_overall_stats='yes'
else
	user_list="$user"
fi

# Reset overall statistics
TOTAL_IP_OWNED=0
TOTAL_U_DISK=0
TOTAL_U_DISK_DIRS=0
TOTAL_U_DISK_WEB=0
TOTAL_U_DISK_MAIL=0
TOTAL_U_DISK_DB=0
TOTAL_U_BANDWIDTH=0
TOTAL_U_WEB_DOMAINS=0
TOTAL_U_WEB_SSL=0
TOTAL_U_WEB_ALIASES=0
TOTAL_U_DNS_DOMAINS=0
TOTAL_U_DNS_RECORDS=0
TOTAL_U_MAIL_DOMAINS=0
TOTAL_U_MAIL_DKIM=0
TOTAL_U_MAIL_ACCOUNTS=0
TOTAL_U_DATABASES=0
TOTAL_U_CRON_JOBS=0
TOTAL_U_BACKUPS=0
TOTAL_USERS=0

# Updating user stats
for user in $user_list; do
	USER_DATA=$HESTIA/data/users/$user
	if [ ! -f "$HESTIA/data/users/$user/user.conf" ]; then
		continue
	fi
	USER_DATA=$HESTIA/data/users/$user
	source $USER_DATA/user.conf
	next_month=$(date +'%m/01/%y' -d "$(date +%Y-%m-15) 1 month")
	DATE=$(date -d "$next_month -1day" +%F)

	# Compiling report string
	s="DATE='$DATE' TIME='$TIME' PACKAGE='$PACKAGE' IP_OWNED='$IP_OWNED'"
	s="$s DISK_QUOTA='$DISK_QUOTA' U_DISK='$U_DISK'"
	s="$s U_DISK_DIRS='$U_DISK_DIRS' U_DISK_WEB='$U_DISK_WEB'"
	s="$s U_DISK_MAIL='$U_DISK_MAIL' U_DISK_DB='$U_DISK_DB'"
	s="$s BANDWIDTH='$BANDWIDTH' U_BANDWIDTH='$U_BANDWIDTH'"
	s="$s U_WEB_DOMAINS='$U_WEB_DOMAINS' U_WEB_SSL='$U_WEB_SSL'"
	s="$s U_WEB_ALIASES='$U_WEB_ALIASES' U_DNS_DOMAINS='$U_DNS_DOMAINS'"
	s="$s U_DNS_RECORDS='$U_DNS_RECORDS' U_MAIL_DOMAINS='$U_MAIL_DOMAINS'"
	s="$s U_MAIL_DKIM='$U_MAIL_DKIM' U_MAIL_ACCOUNTS='$U_MAIL_ACCOUNTS'"
	s="$s U_DATABASES='$U_DATABASES' U_CRON_JOBS='$U_CRON_JOBS'"
	s="$s U_BACKUPS='$U_BACKUPS'"

	# Updating user stats log
	stats="$USER_DATA/stats.log"

	# Checking duplicates
	check_month=$(grep --no-messages -n "DATE='$DATE'" $stats | cut -f 1 -d :)
	if [ -z "$check_month" ]; then
		# Updating as there no duplicates
		echo "$s" >> $stats
	else
		# Replacing string with new data
		sed -i "$check_month s/.*/$s/" $stats
	fi
	chmod 660 $stats

	# Increase overall counters
	TOTAL_IP_OWNED=$((TOTAL_IP_OWNED + IP_OWNED))
	TOTAL_U_DISK=$((TOTAL_U_DISK + U_DISK))
	TOTAL_U_DISK_DIRS=$((TOTAL_U_DISK_DIRS + U_DISK_DIRS))
	TOTAL_U_DISK_WEB=$((TOTAL_U_DISK_WEB + U_DISK_WEB))
	TOTAL_U_DISK_MAIL=$((TOTAL_U_DISK_MAIL + U_DISK_MAIL))
	TOTAL_U_DISK_DB=$((TOTAL_U_DISK_DB + U_DISK_DB))
	TOTAL_U_BANDWIDTH=$((TOTAL_U_BANDWIDTH + U_BANDWIDTH))
	TOTAL_U_WEB_DOMAINS=$((TOTAL_U_WEB_DOMAINS + U_WEB_DOMAINS))
	TOTAL_U_WEB_SSL=$((TOTAL_U_WEB_SSL + U_WEB_SSL))
	TOTAL_U_WEB_ALIASES=$((TOTAL_U_WEB_ALIASES + U_WEB_ALIASES))
	TOTAL_U_DNS_DOMAINS=$((TOTAL_U_DNS_DOMAINS + U_DNS_DOMAINS))
	TOTAL_U_DNS_RECORDS=$((TOTAL_U_DNS_RECORDS + U_DNS_RECORDS))
	TOTAL_U_MAIL_DOMAINS=$((TOTAL_U_MAIL_DOMAINS + U_MAIL_DOMAINS))
	TOTAL_U_MAIL_DKIM=$((TOTAL_U_MAIL_DKIM + U_MAIL_DKIM))
	TOTAL_U_MAIL_ACCOUNTS=$((TOTAL_U_MAIL_ACCOUNTS + U_MAIL_ACCOUNTS))
	TOTAL_U_DATABASES=$((TOTAL_U_DATABASES + U_DATABASES))
	TOTAL_U_CRON_JOBS=$((TOTAL_U_CRON_JOBS + U_CRON_JOBS))
	TOTAL_U_BACKUPS=$((TOTAL_U_BACKUPS + U_BACKUPS))
	TOTAL_USERS=$((TOTAL_USERS + 1))

done

if [ "$update_overall_stats" = 'yes' ]; then
	# Updating overall stats
	stats="$HESTIA/data/users/$ROOT_USER/overall_stats.log"

	s="DATE='$DATE' TIME='$TIME' PACKAGE='default' IP_OWNED='$TOTAL_IP_OWNED'"
	s="$s DISK_QUOTA='0' U_DISK='$TOTAL_U_DISK' U_DISK_DIRS='$TOTAL_U_DISK_DIRS'"
	s="$s U_DISK_WEB='$TOTAL_U_DISK_WEB' U_DISK_MAIL='$TOTAL_U_DISK_MAIL'"
	s="$s U_DISK_DB='$TOTAL_U_DISK_DB' BANDWIDTH='0'"
	s="$s U_BANDWIDTH='$TOTAL_U_BANDWIDTH' U_WEB_DOMAINS='$TOTAL_U_WEB_DOMAINS'"
	s="$s U_WEB_SSL='$TOTAL_U_WEB_SSL' U_WEB_ALIASES='$TOTAL_U_WEB_ALIASES'"
	s="$s U_DNS_DOMAINS='$TOTAL_U_DNS_DOMAINS'"
	s="$s U_DNS_RECORDS='$TOTAL_U_DNS_RECORDS'"
	s="$s U_MAIL_DOMAINS='$TOTAL_U_MAIL_DOMAINS'"
	s="$s U_MAIL_DKIM='$TOTAL_U_MAIL_DKIM'"
	s="$s U_MAIL_ACCOUNTS='$TOTAL_U_MAIL_ACCOUNTS'"
	s="$s U_DATABASES='$TOTAL_U_DATABASES'"
	s="$s U_CRON_JOBS='$TOTAL_U_CRON_JOBS' U_BACKUPS='$TOTAL_U_BACKUPS'"
	s="$s U_USERS='$TOTAL_USERS'"

	# Checking duplicates
	check_month=$(grep --no-messages -n "DATE='$DATE'" $stats | cut -f 1 -d :)
	if [ -z "$check_month" ]; then
		# Updating as there no duplicates
		echo "$s" >> $stats
	else
		# Replacing string with new data
		sed -i "$check_month s/.*/$s/" $stats
	fi
	chmod 660 $stats
fi

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

# Logging
log_event "$OK" "$ARGUMENTS"

exit
