#!/bin/bash
# info: suspend web domains
# options: USER [RESTART]
#
# The function of suspending all user's sites. 


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

# Argument definition
user=$1
restart=$2

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


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

check_args '1' "$#" 'USER [RESTART]'
validate_format 'user'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
is_object_valid 'user' 'USER' "$user"


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

# Starting suspend loop
for domain in $(search_objects 'web' 'SUSPENDED' "no" 'DOMAIN'); do
    $BIN/v-suspend-web-domain "$user" "$domain" 'no'
done

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

# Restarting web server
if [ "$restart" != 'no' ]; then
    $BIN/v-restart-web
    check_result $? "Web restart failed" >/dev/null

    if [ ! -z "$PROXY_SYSTEM" ]; then
        $BIN/v-restart-proxy
        check_result $? "Proxy restart failed" >/dev/null
    fi
fi

# Logging
log_event "$OK" "$EVENT"

exit
