#!/bin/bash
# info: delete web/dns/mail domain
# options: USER DOMAIN
#
# The function deletes web/dns/mail domain.


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

# Argument definition
user=$1
domain=$2
restart="${3-yes}"

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


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

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


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

# Working on Web domain
if [ ! -z "$WEB_SYSTEM" ]; then
    str=$(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
    if [  ! -z "$str" ]; then
        domain_found='yes'
        $BIN/v-delete-web-domain $user $domain 'no'
        check_result $? "can't suspend web" > /dev/null
    fi
fi

# Working on DNS domain
if [ ! -z "$DNS_SYSTEM" ]; then
    str=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
    if [  ! -z "$str" ]; then
        domain_found='yes'
        $BIN/v-delete-dns-domain $user $domain 'no'
        check_result $? "can't suspend dns" > /dev/null
    fi
fi

# Working on Mail domain
if [ ! -z "$MAIL_SYSTEM" ]; then
    str=$(grep "DOMAIN='$domain'" $USER_DATA/mail.conf)
    if [  ! -z "$str" ]; then
        domain_found='yes'
        $BIN/v-delete-mail-domain $user $domain
        check_result $? "can't suspend mail" > /dev/null
    fi
fi

# Checking domain search result
if [ -z "$domain_found" ]; then
    check_result $E_NOTEXISTS "domain $domain doesn't exist"
fi

# Restarting services
$BIN/v-restart-web $restart
check_result $? "can't restart web" > /dev/null

$BIN/v-restart-proxy $restart
check_result $? "can't restart proxy" > /dev/null

$BIN/v-restart-dns $restart
check_result $? "can't restart dns" > /dev/null


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

exit