#!/bin/sh

# daun v0.4
# Desktop-agnostic upgrade notifier - needrestart hook script
# Copyright (c) 2026 Raphaël Halimi <raphael.halimi@gmail.com>

# Source library
. /usr/lib/daun/functions


#
# Checks
#

# Check needrestart configuration
[ "$NR_NOTIFYD_DISABLE_NOTIFY_SEND" = '1' ] && die "disabled in global config"

# Check if we should/can notify
check_notify $NR_UID || die

# Report
print_message "Notify user '$NR_USERNAME'"

#
# Main
#

# Get names of obsolete processes
OBSOLETE="$(ps -o exe= $(grep -o '\[[^[]*\]' | tr '[],' ' ') | sort -u)"

# Set locale
set_locale $NR_UID

# Notify user if a session restart is required
notify "$NR_UID" "session" || exit 1

# Per-program notifications
for PROG in $PROGS ; do
  if printf %s "$OBSOLETE" | grep -q -i "$PROG" ; then
    notify "$NR_UID" "program" "$PROG"
  fi
done
