#!/bin/bash
# info: change user package
# options: USER PACKAGE [FORCE]
#
# example: v-change-user-package admin yourpackage
#
# This function changes user's hosting package.

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

# Argument definition
user=$1
package=$2
force=$3

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

is_package_available() {

	source_conf "$USER_DATA/user.conf"

	WEB_DOMAINS='0'
	DATABASES='0'
	MAIL_DOMAINS='0'
	DNS_DOMAINS='0'
	DISK_QUOTA='0'
	BANDWIDTH='0'

	source_conf "$HESTIA/data/packages/$package.pkg"

	# Checking usage agains package limits
	if [ "$WEB_DOMAINS" != 'unlimited' ]; then
		if [ "$WEB_DOMAINS" -lt "$U_WEB_DOMAINS" ]; then
			check_result "$E_LIMIT" "Package doesn't cover WEB_DOMAIN usage"
		fi
	fi
	if [ "$DNS_DOMAINS" != 'unlimited' ]; then
		if [ "$DNS_DOMAINS" -lt "$U_DNS_DOMAINS" ]; then
			check_result "$E_LIMIT" "Package doesn't cover DNS_DOMAIN usage"
		fi
	fi
	if [ "$MAIL_DOMAINS" != 'unlimited' ]; then
		if [ "$MAIL_DOMAINS" -lt "$U_MAIL_DOMAINS" ]; then
			check_result "$E_LIMI"T "Package doesn't cover MAIL_DOMAIN usage"
		fi
	fi
	if [ "$DATABASES" != 'unlimited' ]; then
		if [ "$DATABASES" -lt "$U_DATABASES" ]; then
			check_result "$E_LIMIT" "Package doesn't cover DATABASE usage"
		fi
	fi
	if [ "$CRON_JOBS" != 'unlimited' ]; then
		if [ "$CRON_JOBS" -lt "$U_CRON_JOBS" ]; then
			check_result "$E_LIMIT" "Package doesn't cover CRON usage"
		fi
	fi
	if [ "$DISK_QUOTA" != 'unlimited' ]; then
		if [ "$DISK_QUOTA" -lt "$U_DISK" ]; then
			check_result "$E_LIMIT" "Package doesn't cover DISK usage"
		fi
	fi
	if [ "$BANDWIDTH" != 'unlimited' ]; then
		if [ "$BANDWIDTH" -lt "$U_BANDWIDTH" ]; then
			check_result "$E_LIMIT" "Package doesn't cover BANDWIDTH usage"
		fi
	fi
}

change_user_package() {
	source_conf "$USER_DATA/user.conf"
	# Keep user creation date and time saved
	time="$TIME"
	date="$DATE"
	source_conf "$HESTIA/data/packages/$package.pkg"
	echo "NAME='$NAME'
PACKAGE='$package'
WEB_TEMPLATE='$WEB_TEMPLATE'
BACKEND_TEMPLATE='$BACKEND_TEMPLATE'
PROXY_TEMPLATE='$PROXY_TEMPLATE'
DNS_TEMPLATE='$DNS_TEMPLATE'
WEB_DOMAINS='$WEB_DOMAINS'
WEB_ALIASES='$WEB_ALIASES'
DNS_DOMAINS='$DNS_DOMAINS'
DNS_RECORDS='$DNS_RECORDS'
MAIL_DOMAINS='$MAIL_DOMAINS'
MAIL_ACCOUNTS='$MAIL_ACCOUNTS'
RATE_LIMIT='$RATE_LIMIT'
DATABASES='$DATABASES'
CRON_JOBS='$CRON_JOBS'
DISK_QUOTA='$DISK_QUOTA'
BANDWIDTH='$BANDWIDTH'
NS='$NS'
SHELL='$SHELL'
BACKUPS='$BACKUPS'
CONTACT='$CONTACT'
CRON_REPORTS='$CRON_REPORTS'
MD5='$MD5'
RKEY='$RKEY'
TWOFA='$TWOFA'
QRCODE='$QRCODE'
PHPCLI='$PHPCLI'
ROLE='$ROLE'
SUSPENDED='$SUSPENDED'
SUSPENDED_USERS='$SUSPENDED_USERS'
SUSPENDED_WEB='$SUSPENDED_WEB'
SUSPENDED_DNS='$SUSPENDED_DNS'
SUSPENDED_MAIL='$SUSPENDED_MAIL'
SUSPENDED_DB='$SUSPENDED_DB'
SUSPENDED_CRON='$SUSPENDED_CRON'
IP_AVAIL='$IP_AVAIL'
IP_OWNED='$IP_OWNED'
U_USERS='$U_USERS'
U_DISK='$U_DISK'
U_DISK_DIRS='$U_DISK_DIRS'
U_DISK_WEB='$U_DISK_WEB'
U_DISK_MAIL='$U_DISK_MAIL'
U_DISK_DB='$U_DISK_DB'
U_BANDWIDTH='$U_BANDWIDTH'
U_WEB_DOMAINS='$U_WEB_DOMAINS'
U_WEB_SSL='$U_WEB_SSL'
U_WEB_ALIASES='$U_WEB_ALIASES'
U_DNS_DOMAINS='$U_DNS_DOMAINS'
U_DNS_RECORDS='$U_DNS_RECORDS'
U_MAIL_DOMAINS='$U_MAIL_DOMAINS'
U_MAIL_DKIM='$U_MAIL_DKIM'
U_MAIL_ACCOUNTS='$U_MAIL_ACCOUNTS'
U_DATABASES='$U_DATABASES'
U_CRON_JOBS='$U_CRON_JOBS'
U_BACKUPS='$U_BACKUPS'
LANGUAGE='$LANGUAGE'
THEME='$THEME'
NOTIFICATIONS='$NOTIFICATIONS'
PREF_UI_SORT='$PREF_UI_SORT'
LOGIN_DISABLED='$LOGIN_DISABLED'
LOGIN_USE_IPLIST='$LOGIN_USE_IPLIST'
LOGIN_ALLOW_IPS='$LOGIN_ALLOW_IPS'
TIME='$time'
DATE='$date'" > $USER_DATA/user.conf
}

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

check_args '2' "$#" 'USER PACKAGE [FORCE]'
is_format_valid 'user' 'package'
is_object_valid 'user' 'USER' "$user"
is_package_valid
if [ "$force" != 'yes' ]; then
	is_package_available
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Changing user package
change_user_package

# Update user shell
shell_conf=$(cat "$HESTIA/data/packages/$package.pkg" | grep 'SHELL' | cut -f 2 -d \')
$BIN/v-change-user-shell "$user" "$shell_conf"

# Run template trigger
if [ -x "$HESTIA/data/packages/$package.sh" ]; then
	$HESTIA/data/packages/$package.sh "$user" "$CONTACT" "$NAME"
fi

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

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

# Logging
$BIN/v-log-action "$user" "Info" "System" "You have been assigned a new package ($package)."
$BIN/v-log-action "system" "Info" "System" "Package limits applied to user (Package: $package, User: $user)."
log_event "$OK" "$ARGUMENTS"

exit
