#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Tell the user when the kernels and the NVIDIA driver do not match.
#
# - The running kernel has no nvidia.ko, an NVIDIA display controller
#   is present, and another installed kernel has the driver. The
#   desktop runs on the firmware framebuffer with software rendering.
#   This happens when the kernel was chosen by hand, or before
#   90-atropos-nvidia.snapper was on the running system. Shown once
#   per boot.
# - A newer installed kernel has no nvidia.ko yet, and the running one
#   has it. Slowroll published the kernel before its KMP, and
#   90-atropos-nvidia.snapper kept this kernel as the boot default.
#   Shown once per newer kernel.
#
# Runs at login as a user service.

set -u

modules=/usr/lib/modules
state_dir="${XDG_STATE_HOME:-$HOME/.local/state}/atropos-nvidia"
state="$state_dir/kernel-notified"

nvidia_display()
{
	local d
	for d in /sys/bus/pci/devices/*; do
		[ "$(cat "$d/vendor" 2>/dev/null)" = 0x10de ] || continue
		case "$(cat "$d/class" 2>/dev/null)" in
		0x0300* | 0x0302*) return 0 ;;
		esac
	done
	return 1
}

has_nvidia()
{
	[ -n "$(find "$modules/$1" \( -name nvidia.ko -o -name 'nvidia.ko.*' \) -print -quit 2>/dev/null)" ]
}

# Installed kernels, oldest first. A directory without vmlinuz is what a
# KMP leaves behind for a removed kernel.
kernels()
{
	local f
	for f in "$modules"/*/vmlinuz; do
		[ -e "$f" ] || continue
		f="${f%/vmlinuz}"
		echo "${f##*/}"
	done | sort -V
}

newer_than()
{
	[ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | tail -n 1)" = "$1" ]
}

notify()
{
	command -v gdbus >/dev/null || return 1
	# plasmashell owns the name, and it can start after this service
	gdbus wait --session --timeout 120 org.freedesktop.Notifications >/dev/null 2>&1 || return 1
	gdbus call --session \
		--dest org.freedesktop.Notifications \
		--object-path /org/freedesktop/Notifications \
		--method org.freedesktop.Notifications.Notify \
		AtropOS "@u 0" "$1" "$2" "$3" "@as []" "@a{sv} {}" "@i $4" >/dev/null
}

nvidia_display || exit 0

running="$(uname -r)"
good=
newer=
while read -r k; do
	if has_nvidia "$k"; then
		good="$k"
	elif newer_than "$k" "$running"; then
		newer="$k"
	fi
done < <(kernels)

# The driver is not installed for any kernel
[ -n "$good" ] || exit 0

if [ ! -d /sys/module/nvidia ] && ! has_nvidia "$running"; then
	key="missing $(cat /proc/sys/kernel/random/boot_id)"
	icon=dialog-warning
	title="No NVIDIA driver for this kernel"
	body="Linux $running has no NVIDIA driver, so the desktop uses software rendering. Restart, hold Space while the computer starts, and choose the boot menu entry that ends in $good."
	expire=0
elif [ -n "$newer" ]; then
	key="waiting $newer"
	icon=video-display
	title="Kernel update waiting for the NVIDIA driver"
	body="Linux $newer is installed, but its NVIDIA driver has not been published yet. This session runs Linux $running. The newer kernel starts once a driver update arrives."
	expire=-1
else
	exit 0
fi

[ "$(cat "$state" 2>/dev/null)" != "$key" ] || exit 0
notify "$icon" "$title" "$body" "$expire" || exit 0
mkdir -p "$state_dir" && printf '%s\n' "$key" > "$state"
