#!/bin/bash
# info: delete user notification
# options: USER NOTIFICATION
#
# example: v-delete-user-notification admin 1
#
# This function deletes user notification.

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

# Argument definition
user=$1
id=$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
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '2' "$#" 'USER NOTIFICATION'
if [ "$id" = "all" ]; then
	is_format_valid 'user'
else
	is_format_valid 'user' 'id'
fi
is_object_valid 'user' 'USER' "$user"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

if [ "$id" = "all" ]; then
	notice='no'
	rm $USER_DATA/notifications.conf
	touch $USER_DATA/notifications.conf
else
	# Deleting notification
	sed -i "/NID='$id' /d" $USER_DATA/notifications.conf 2> /dev/null
	# Checking last notification
	if [ -e "$USER_DATA/notifications.conf" ]; then
		if [ -z "$(grep NID= $USER_DATA/notifications.conf)" ]; then
			notice='no'
		fi
		if [ -z "$(grep "ACK='no'" $USER_DATA/notifications.conf)" ]; then
			notice='no'
		fi
	else
		notice='no'
	fi
fi
#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Updating notification counter
if [ "$notice" = 'no' ]; then
	if [ -z "$(grep NOTIFICATIONS $USER_DATA/user.conf)" ]; then
		sed -i "s/^TIME/NOTIFICATIONS='no'\nTIME/g" $USER_DATA/user.conf
	else
		update_user_value "$user" '$NOTIFICATIONS' "no"
	fi
fi

# Logging
log_event "$OK" "$ARGUMENTS"

exit