#!/bin/bash
# info: delete http auth user
# options: USER DOMAIN AUTH_USER [RESTART]
#
# example: v-delete-web-domain-httpauth admin example.com alice
#
# This function is used for deleting http auth user

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

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

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

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

check_args '3' "$#" 'USER DOMAIN AUTH_USER [RESTART]'
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_password_valid
get_domain_values 'web'
if [ -z "$(echo "$AUTH_USER" | tr : '\n' | grep ^$auth_user$)" ]; then
	echo "Error: auth user $auth_user doesn't exist"
	log_event "$E_NOTEXIST" "$ARGUMENTS"
	exit "$E_NOTEXIST"
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

htpasswd="$HOMEDIR/$user/conf/web/$domain/htpasswd"
# Deleting auth user
sed -i "/^$auth_user:/d" $htpasswd

# Deleting password protection
if [ "$(echo "$AUTH_USER" | tr : '\n' | wc -l)" -le 1 ]; then
	if [ "$WEB_SYSTEM" = "nginx" ] || [ "$PROXY_SYSTEM" = "nginx" ]; then
		htaccess="$HOMEDIR/$user/conf/web/$domain/nginx.conf_htaccess"
		shtaccess="$HOMEDIR/$user/conf/web/$domain/nginx.ssl.conf_htaccess"
	else
		htaccess="$HOMEDIR/$user/conf/web/$domain/apache2.conf_htaccess"
		shtaccess="$HOMEDIR/$user/conf/web/$domain/apache2.ssl.conf_htaccess"
	fi
	rm -f $htaccess $htpasswd $shtaccess
	restart_required='yes'
fi

# Restarting web server
if [ "$restart" != 'no' ] && [ "$restart_required" = 'yes' ]; then
	$BIN/v-restart-web
	if [ -n "$PROXY_SYSTEM" ]; then
		$BIN/v-restart-proxy
	fi
fi

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

# Rebuilding FTP variables
position=$(echo $AUTH_USER | tr ':' '\n' | grep -n '' | grep ":$auth_user$" \
	| cut -f 1 -d:)
auth_user=$(echo $AUTH_USER | tr ':' '\n' | grep -n '' | grep -v "^$position:" \
	| cut -f 2 -d : | sed -e "/^$/d" | sed -e ':a;N;$!ba;s/\n/:/g')
auth_hash=$(echo $AUTH_HASH | tr ':' '\n' | grep -n '' | grep -v "^$position:" \
	| cut -f 2 -d : | sed -e ':a;N;$!ba;s/\n/:/g')

# Update config
update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_USER' "$auth_user"
update_object_value 'web' 'DOMAIN' "$domain" '$AUTH_HASH' "$auth_hash"

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

exit
