#!/bin/bash

# To be called from melawy-reboot-required.hook.

# Implementation using libnotify.

GetUsers() { /usr/bin/users | sed 's| |\n|g' | sort | uniq ; }

_other_reboot_required() {
    local msg="Reboot is recommended due to the upgrade of core system package(s)."
    local title="Reboot recommended!"

    # at TTY
    echo "==> INFO: $msg" >&2

    # notification
    local user userid cmd

    for user in $(GetUsers) ; do
        userid=$(/usr/bin/id -u $user)
        cmd=(
            DISPLAY=:0
            DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$userid/bus
            /usr/bin/notify-send
            --icon=system-reboot
            --urgency=critical
            "'$title'"
            "'$msg'"
        )
        /usr/bin/su $user -c "${cmd[*]}"
    done
}
_other_reboot_required
unset -f _other_reboot_required