#!/bin/bash
# info: delete user notification
# options: USER NOTIFICATION
#
# The function deletes user notification.


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

# Argument definition
user=$1
nid=$2

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


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

check_args '2' "$#" 'USER NOTIFICATION'
is_format_valid 'user' 'nid'
is_object_valid 'user' 'USER' "$user"


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

# Deleting notification
sed -i "/NID='$nid' /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


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

# 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