#!/bin/bash
# info: delete web domain
# options: USER DOMAIN
#
# The call of function leads to the removal of domain and all its components
# (statistics, folders contents, ssl certificates, etc.). This operation is
# not fully supported by "undo" function, so the data recovery is possible
# only with a help of reserve copy.


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

# Argument definition
user=$1
domain=$2
domain_idn=$2
restart=$3

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

# Additional argument formatting
format_domain
format_domain_idn
# TODO: $domain_idn not used in this script - maybe $domain should be converted to $doman_idn ?


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

check_args '2' "$#" 'USER DOMAIN'
is_format_valid 'user' 'domain'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_unsuspended 'web' 'DOMAIN' "$domain"


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

# Deleting web backend
if [ ! -z "$WEB_BACKEND" ]; then
    $BIN/v-delete-web-domain-backend $user $domain $restart
fi

# Parsing domain values
get_domain_values 'web'
local_ip=$(get_real_ip $IP)

# Deleting domain from web.conf
sed -i "/DOMAIN='$domain'/ d" $USER_DATA/web.conf

# Deleting vhost configuration
del_web_config "$WEB_SYSTEM" "$TPL.tpl"

# Deleting SSL configuration and certificates
if [ "$SSL" = 'yes' ]; then
    del_web_config "$WEB_SYSTEM" "$TPL.stpl"
    rm -f $HOMEDIR/$user/conf/web/ssl.$domain.*
    rm -f $USER_DATA/ssl/$domain.*
fi

# Deleting proxy
if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
    del_web_config "$PROXY_SYSTEM" "$PROXY.tpl"
    if [ "$SSL" = 'yes' ]; then
        del_web_config "$PROXY_SYSTEM" "$PROXY.stpl"
    fi
    if [ -e "/etc/$PROXY_SYSTEM/conf.d/01_caching_pool.conf" ]; then
        sed -i "/=$domain:/d" /etc/$PROXY_SYSTEM/conf.d/01_caching_pool.conf
    fi
fi

# Deleting web stats
if [ ! -z "$STATS" ] && [ "$STATS" != 'no' ]; then
    sed -i "/ $domain$/d" $VESTA/data/queue/webstats.pipe
    rm -f $HOMEDIR/$user/conf/web/$STATS.$domain.conf
    rm -f /etc/awstats/$STATS.$domain.conf
fi

# Deleting ftp users
if [ ! -z "$FTP_USER" ]; then
    for ftp_user in ${FTP_USER//:/ }; do
        /usr/sbin/userdel $ftp_user >> /dev/null 2>&1
        if [ "$?" != 0 ]; then
            sed -i "/^$ftp_user:/d" /etc/passwd
            sed -i "/^$ftp_user:/d" /etc/shadow
        fi
    done
fi

# Deleting logs
rm -f /var/log/$WEB_SYSTEM/domains/$domain.log*
rm -f /var/log/$WEB_SYSTEM/domains/$domain.bytes
rm -f /var/log/$WEB_SYSTEM/domains/$domain.error*

# Deleting directory
rm -rf $HOMEDIR/$user/web/$domain


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

# Decreasing user counters
decrease_ip_value "$local_ip"
decrease_user_value "$user" '$U_WEB_DOMAINS'
if [ "$SSL" = 'yes' ]; then
    decrease_user_value "$user" '$U_WEB_SSL'
fi
if [ ! -z "$ALIAS" ]; then
    aliases=$(echo $ALIAS | tr ',' '\n' | wc -l )
    decrease_user_value "$user" '$U_WEB_ALIASES' "$aliases"
fi

# Restarting web server
$BIN/v-restart-web $restart
check_result $? "Web restart failed" >/dev/null

# Restartinh proxy server
$BIN/v-restart-proxy $restart
check_result $? "Proxy restart failed" >/dev/null

# Logging
log_history "deleted web domain $domain"
log_event "$OK" "$ARGUMENTS"

exit
