#!/bin/bash
# info: delete web domain statistics
# options: USER DOMAIN
#
# example: v-delete-web-domain-stats user02 h1.example.com
#
# This function of deleting site's system of statistics. Its type is
# automatically chooses from client's configuration file.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

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

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# shellcheck source=/usr/local/hestia/func/domain.sh
source $HESTIA/func/domain.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

# Additional argument formatting
format_domain
format_domain_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_valid 'web' 'DOMAIN' "$domain"
is_object_value_exist 'web' 'DOMAIN' "$domain" '$STATS'

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Defining statistic type
get_domain_values 'web'

# Deleting dir content
rm -rf "$HOMEDIR/$user/web/$domain/stats"

# Deleting config
rm -f "/etc/awstats/$STATS.$domain_idn.conf"
rm -f "$HOMEDIR/$user/conf/web/$domain/$STATS.conf"

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

# Deleting pipe command
sed -i "/ $domain$/d" $HESTIA/data/queue/webstats.pipe

# Update config
update_object_value 'web' 'DOMAIN' "$domain" '$STATS' ''

# Logging
$BIN/v-log-action "$user" "Info" "Web" "Web traffic analyzer disabled (Domain: $domain)."
log_event "$OK" "$ARGUMENTS"

exit