#!/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 defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain_idn=$(idn -t --quiet -a "$domain")
restart=$3

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


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

check_args '2' "$#" 'USER DOMAIN'
validate_format '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                             #
#----------------------------------------------------------#

# Get template name
get_domain_values 'web'
tpl_file="$WEBTPL/$WEB_SYSTEM/$TPL.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
ip=$(get_real_ip $IP)

# Deleting domain
del_web_config

# Checking aliases
if [ ! -z "$ALIAS" ]; then
    aliases=$(echo $ALIAS | tr ',' '\n' | wc -l )
else
    aliases=0
fi

# Checking SSL
if [ "$SSL" = 'yes' ]; then
    tpl_file="$WEBTPL/$WEB_SYSTEM/$TPL.stpl"
    conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
    del_web_config

    # Deleting SSL certificates
    rm -f $HOMEDIR/$user/conf/web/ssl.$domain.*
    rm -f $USER_DATA/ssl/$domain.*
fi

# Checking proxy
if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
    tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.tpl"
    conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
    del_web_config

    if [ "$SSL" = 'yes' ]; then
        tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
        conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
        del_web_config
    fi
fi

# Checking 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 directory
rm -rf $HOMEDIR/$user/web/$domain

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


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

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

# Checking last SSL domain
conf="/etc/$WEB_SYSTEM/conf.d/vesta.conf"
ssl_dom=$(grep "SSL='yes'" $USER_DATA/web.conf | wc -l)
if [ "$ssl_dom" -eq '0' ]; then
    sed -i "/.*\/$user\/.*s$WEB_SYSTEM.conf/d" $conf
    rm -f $HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf
fi

# Checking last domain
domains=$(wc -l $USER_DATA/web.conf | cut -f1 -d ' ')
if [ "$domains" -eq '0' ]; then
    sed -i "/.*\/$user\/.*$WEB_SYSTEM.conf/d" $conf
    rm -f $HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf
fi

# Proxy
if [ ! -z "$PROXY_SYSTEM" ]; then
    # Checking last SSL proxy
    conf="/etc/$PROXY_SYSTEM/conf.d/vesta.conf"
    last_sproxy=$(grep "SSL='yes'" $USER_DATA/web.conf)
    if [ -z "$last_sproxy" ]; then
        sed -i "/.*\/$user\/.*s$PROXY_SYSTEM.conf/d" $conf
        rm -f $HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf
    fi

    # Checking last proxy
    last_proxy=$(grep -v "PROXY=''" $USER_DATA/web.conf)
    if [ -z "$last_proxy" ]; then
        sed -i "/.*\/$user\/.*$PROXY_SYSTEM.conf/d" $conf
        rm -f $HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf
    fi
fi

# Decrease counters
decrease_ip_value "$ip"
decrease_user_value "$user" '$U_WEB_DOMAINS'
decrease_user_value "$user" '$U_WEB_ALIASES' "$aliases"
if [ "$SSL" = 'yes' ]; then
    decrease_user_value "$user" '$U_WEB_SSL'
fi

# Restart web server
if [ "$restart" != 'no' ]; then
    $BIN/v-restart-web
    if [ $? -ne 0 ]; then
        exit $E_RESTART
    fi

    $BIN/v-restart-proxy
    if [ $? -ne 0 ]; then
        exit $E_RESTART
    fi
fi

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

exit
