#!/bin/bash

# Avoid unnecessary reboots: don't notify if an updated package is
# - not currently running (e.g. alternative kernel)
# - not in use (e.g. alternative driver)

IsRunningKernel() {
    if [ -z "$running_kernel" ] ; then
        running_kernel=$(eos_running_kernel)
        [ "$running_kernel" ] || ERROR "unable to determine running kernel"
    fi
    test "$1" = "$running_kernel"
}

DoNotify() {
    DEBUG "${FUNCNAME[0]} begin."
    # echo "[$(/usr/bin/date "+%x %X")] Reboot recommendation triggered by updating package(s): ${list_of_targets[*]}" >> "$logfile"
    INFO "Reboot recommendation triggered by updating package(s): ${list_of_targets[*]}"
    # chmod o-rwx "$logfile"
    systemctl enable --now eos-reboot-required.timer 2>/dev/null
    DEBUG "${FUNCNAME[0]} end."
}

ERROR()  { echo "==> $progname: ${FUNCNAME[0],,}: $1" >> "$logfile"; exit 0; }
DEBUG()  { echo "==> $progname: ${FUNCNAME[0],,}: $1" >> "$logfile"; }
INFO()   { echo "==> $progname: ${FUNCNAME[0],,}: $1" >> "$logfile"; }

Main() {
    local -r progname=${0##*/}
    local -r logfile="/var/log/$progname.log"
    local -r common_scripts_file="/usr/share/endeavouros/scripts/eos-script-lib-yad"

    rm -f "$logfile"
    DEBUG "$progname begin."

    [ "$EUID" = "0" ]                           || ERROR "elevated privileges required."
    [ -r "$common_scripts_file" ]               || ERROR "failed to read file $common_scripts_file."
    # shellcheck disable=SC1090
    source "$common_scripts_file" --limit-icons || ERROR "failed to source file $common_scripts_file"

    eos_is_in_chroot && return 0
    DEBUG "${FUNCNAME[0]}: not chrooting."

    DEBUG "${FUNCNAME[0]}: variable EOS_REBOOT_RECOMMENDING == '$EOS_REBOOT_RECOMMENDING'"
    [ "$EOS_REBOOT_RECOMMENDING" = "no" ] && return 0

    local targets       # list of updated package names from the hook (stdin)
    local target
    local running_kernel=""
    local list_of_targets=()
    targets=$(/usr/bin/cat)

    # do not notify if the updated package is not in use
    for target in $targets ; do
        case "$target" in
            linux | linux-lts | linux-zen | linux-hardened | linux-rt | linux-rt-lts)
                # Note: only official kernels are checked.
                IsRunningKernel "$target" && list_of_targets+=("$target")
                ;;
            nvidia-open)
                IsRunningKernel linux && list_of_targets+=("$target")
                ;;
            nvidia-open-lts)
                IsRunningKernel linux-lts && list_of_targets+=("$target")
                ;;
            amd-ucode)
                [ "$(device-info --cpu)" = "AuthenticAMD" ] && list_of_targets+=("$target")
                ;;
            intel-ucode)
                [ "$(device-info --cpu)" = "GenuineIntel" ] && list_of_targets+=("$target")
                ;;
            btrfs-progs)
                # Notify only if btrfs is in use
                /usr/bin/df -hT | awk '{print $2}' | grep -qw btrfs && list_of_targets+=("$target")
                ;;
            wayland | egl-wayland)
                case "$XDG_SESSION_TYPE" in
                    x11) ;;
                    *) list_of_targets+=("$target") ;;
                esac
                ;;
            *)
                list_of_targets+=("$target")
                ;;
        esac
    done
    if [ -z "${list_of_targets[0]}" ] ; then
        DEBUG "${FUNCNAME[0]}: no target found to trigger reboot notification."
        DEBUG "${FUNCNAME[0]}: target list: ${list_of_targets[*]}."
        return 0    # nothing to notify
    fi
    DoNotify
    DEBUG "$progname end."
}

Main "$@"
