#!/bin/bash
# info: disable webdomain stats  authentication support
# options: USER DOMAIN [RESTART]
#
# The function removes authentication of statistics system. If the script is
# called without naming a certain user, all users will be removed. After
# deleting all of them statistics will be accessible for view without an
# authentication.


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

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

# Includes
source $HESTIA/func/main.sh
source $HESTIA/func/domain.sh
source $HESTIA/conf/hestia.conf


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

check_args '2' "$#" 'USER DOMAIN [RESTART]'
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"
is_object_value_exist 'web' 'DOMAIN' "$domain" '$STATS_USER'

# Perform verification if read-only mode is enabled
check_hestia_demo_mode


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

# Defining statistic dir
rm -f $HOMEDIR/$user/web/$domain/stats/.htpasswd
rm -f $HOMEDIR/$user/web/$domain/stats/.htaccess

# Deleting nginx auth config
if [ "$WEB_SYSTEM" = 'nginx' ]; then
    conf_dir="$HOMEDIR/$user/conf/web"
    rm -f $conf_dir/$domain.auth 2>/dev/null
    $BIN/v-restart-web $restart
    check_result $? "Web restart failed" >/dev/null
fi


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

# Update config
update_object_value 'web' 'DOMAIN' "$domain" '$STATS_USER' ''
update_object_value 'web' 'DOMAIN' "$domain" '$STATS_CRYPT' ''


# Logging
log_history "disabled web stats password protection for $domain"
log_event "$OK" "$ARGUMENTS"

exit
