#!/bin/bash
# info: delete user
# options: USER
#
# This function deletes a certain user and all his resources such as domains,
# databases, cron jobs, etc.


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

# Argument definition
user=$1
restart=$2

# Includes
source $HESTIA/func/main.sh
source $HESTIA/func/domain.sh
source $HESTIA/func/db.sh
source $HESTIA/func/ip.sh
source $HESTIA/conf/hestia.conf


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

check_args '1' "$#" 'USER'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
if [ "$user" = 'admin' ]; then
    exit
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode


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

# Deleting web domains
if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" != 'no' ]; then
    $BIN/v-unsuspend-web-domains $user $restart
    $BIN/v-delete-web-domains $user $restart
    check_result "$?" "web domain deletion failed"
fi

# Deleting dns domains
if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS_SYSTEM" != 'no' ]; then
    $BIN/v-unsuspend-dns-domains $user $restart
    $BIN/v-delete-dns-domains $user $restart
    check_result "$?" "dns domain deletion failed"
fi

# Deleting mail domains
if [ ! -z "$MAIL_SYSTEM" ] && [ "$MAIL_SYSTEM" != 'no' ]; then
    $BIN/v-unsuspend-mail-domains $user
    $BIN/v-delete-mail-domains $user
    check_result "$?" "mail domain deletion failed"
fi

# Deleting databases
if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ]; then
    $BIN/v-unsuspend-databases $user
    $BIN/v-delete-databases $user
    check_result "$?" "database deletion failed"
fi

# Releasing user ips
$BIN/v-delete-user-ips "$user"
check_result "$?" "user ip release failed"

# Deleteing user pipes
sed -i "/ $user$/d" $HESTIA/data/queue/disk.pipe
sed -i "/ $user$/d" $HESTIA/data/queue/traffic.pipe

# Deleting sftp jail
$BIN/v-delete-user-sftp-jail $user

# Deleting system user
/usr/sbin/userdel -f $user >> /dev/null 2>&1
if [ $? -ne 0 ]; then
    sed -i "/^$user:/d" /etc/passwd
    sed -i "/^$user:/d" /etc/shadow
fi

/usr/sbin/groupdel $user >> /dev/null 2>&1
if [ $? -ne 0 ]; then
    sed -i "/^$user:/d" /etc/group
fi

# Deleting user directories
chattr -i $HOMEDIR/$user/conf > /dev/null 2>&1
rm -rf $HOMEDIR/$user
rm -f /var/spool/mail/$user
rm -f /var/spool/cron/$user
rm -f /var/spool/cron/crontabs/$user
rm -rf $USER_DATA

# Updating admin counter
if [ "$user" != 'admin' ]; then
    decrease_user_value 'admin' '$U_USERS'
fi


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

# Logging
log_history "deleted system user $user" '' 'admin'
log_event "$OK" "$ARGUMENTS"

exit
