#!/bin/bash
# info: change user package
# options: USER PACKAGE [FORCE]
#
# The function changes user's hosting package.


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

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

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

is_package_avalable() {

    usr_data=$(cat $USER_DATA/user.conf)
    IFS=$'\n'
    for key in $usr_data; do
        eval ${key%%=*}=${key#*=}
    done

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

    pkg_data=$(cat $VESTA/data/packages/$package.pkg |grep -v TIME |\
        grep -v DATE)
    eval $pkg_data

    # 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_LIMIT "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() {
    eval $(cat $USER_DATA/user.conf)
    eval $(cat $VESTA/data/packages/$package.pkg |egrep -v "TIME|DATE")
    echo "FNAME='$FNAME'
LNAME='$LNAME'
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'
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'
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'
NOTIFICATIONS='$NOTIFICATIONS'
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_avalable
fi


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

# Changing user package
change_user_package

# Update user shell
shell_conf=$(echo "$pkg_data" | grep 'SHELL' | cut -f 2 -d \')
shell=$(grep -w "$shell_conf" /etc/shells |head -n1)
/usr/bin/chsh -s "$shell" "$user" &>/dev/null

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

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


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

# Logging
log_history "changed $user package to $package" '' 'admin'
log_event "$OK" "$ARGUMENTS"

exit
