#! /bin/sh

# Flag that a reboot is needed to boot the newly installed kernel, following the convention used by
# update-notifier and unattended-upgrades, so that tools like update-notifier or monitoring checks
# notice it. Reconfiguring the currently running kernel does not require a reboot.
#
# Named to sort last, as the kernel packages stop at the first failing hook. This way the flag
# is only set once the boot loader hooks succeeded.

version="$1"

[ -n "$version" ] || exit 0
[ "$version" != "$(uname -r)" ] || exit 0
[ -d /run ] || exit 0

# A container or chroot never boots the kernel installed in it, and uname reports the host's
# kernel there.
if ischroot; then
	exit 0
fi
if command -V systemd-detect-virt >/dev/null 2>&1 &&
	systemd-detect-virt --quiet --container; then
	exit 0
fi

pkg="${DPKG_MAINTSCRIPT_PACKAGE:-proxmox-kernel-$version}"

# Only a hint, so failing to write it must not fail the kernel installation.
printf "*** System restart required ***\n" > /run/reboot-required
if ! grep -qxF "$pkg" /run/reboot-required.pkgs 2>/dev/null; then
	printf "%s\n" "$pkg" >> /run/reboot-required.pkgs
fi

exit 0
