#!/bin/bash
# Update only the kernel already referenced by this installation's Limine entry.
set -euo pipefail
fail() { printf '%s\n' "$*" >&2; exit 1; }
(( EUID == 0 )) || fail 'Run this command with sudo.'
record=/etc/tuxenix-bootloader
[[ -f $record && ! -L $record ]] || fail 'No regular Tuxenix bootloader record was found.'
value() { sed -n "s/^$1=//p" "$record"; }
[[ $(value bootloader) == limine ]] || fail 'Only the recorded Tuxenix Limine installation is supported.'
guid=$(value root_partuuid)
[[ $guid =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ ]] || fail 'Invalid root partition identity.'
device=$(findmnt -n -o SOURCE /)
[[ $(blkid -s PARTUUID -o value "$device") == "$guid" ]] || fail 'The bootloader record belongs to a different root partition.'
relative=$(value kernel_efi_path)
[[ $relative =~ ^/EFI/Tuxenix(-[0-9a-f-]{36})?/vmlinuz-[0-9A-Za-z.+-]+-tuxenix$ ]] || fail 'Unsupported recorded kernel path.'
[[ $(findmnt -n -o FSTYPE --mountpoint /boot/efi) == vfat ]] || fail 'Mount this installation’s EFI partition at /boot/efi first.'
directory="/boot/efi${relative%/*}"
for path in /boot /boot/efi /boot/efi/EFI "$directory"; do
    [[ -d $path && ! -L $path ]] || fail "Unsafe EFI directory: $path"
done
marker="$directory/tuxenix-install.json"
[[ -f $marker && ! -L $marker ]] || fail 'The EFI directory has no regular Tuxenix ownership marker.'
grep -Eq '"owner"[[:space:]]*:[[:space:]]*"Tuxenix"' "$marker" || fail 'EFI directory is not owned by Tuxenix.'
grep -Eq "\"root_partuuid\"[[:space:]]*:[[:space:]]*\"$guid\"" "$marker" || fail 'EFI directory belongs to a different installation.'
kernels=(/boot/vmlinuz-*-tuxenix-multilib)
[[ ${#kernels[@]} == 1 && -f ${kernels[0]} ]] || fail 'Install linux-tuxenix-multilib; exactly one matching kernel must be present in /boot.'
kernel=${kernels[0]}
configuration="/boot/config-${kernel##*/vmlinuz-}"
[[ -f $kernel && ! -L $kernel && -f $configuration && ! -L $configuration ]] || fail 'Install the matching Tuxenix kernel package first.'
grep -qx 'CONFIG_USER_NS=y' "$configuration" || fail 'The installed kernel does not enable user namespaces.'
grep -qx 'CONFIG_IA32_EMULATION=y' "$configuration" || fail 'The installed kernel does not enable i686 execution.'
destination="/boot/efi$relative"
[[ -f $destination && ! -L $destination ]] || fail 'The recorded EFI kernel is missing or unsafe.'
[[ -f $directory/limine.conf && ! -L $directory/limine.conf ]] || fail 'The owned Limine configuration is missing.'
sed 's/^[[:space:]]*//;s/[[:space:]]*$//' "$directory/limine.conf" |
    grep -Fxq "path: boot():$relative" || fail 'Limine does not reference the recorded kernel.'
if cmp -s "$kernel" "$destination"; then
    echo 'The EFI kernel already matches the installed kernel.'
    exit 0
fi
backup="$destination.before-multilib"
[[ ! -L $backup ]] || fail 'Unsafe kernel backup path.'
if [[ ! -e $backup ]]; then
    cp -- "$destination" "$backup"
    cmp -s "$destination" "$backup" || fail 'Kernel backup verification failed.'
    sync -f "$backup"
fi
[[ -f $backup ]] || fail 'Kernel backup is not a regular file.'
temporary=$(mktemp "$directory/.kernel-update.XXXXXXXX")
trap 'rm -f -- "$temporary"' EXIT
cp -- "$kernel" "$temporary"
cmp -s "$kernel" "$temporary" || fail 'Staged kernel verification failed.'
sync -f "$temporary"
mv -T -- "$temporary" "$destination"
sync -f "$directory"
cmp -s "$kernel" "$destination" || fail 'EFI kernel readback failed.'
echo "Updated $destination; the previous kernel is retained at $backup."
echo 'Reboot, then run graphics-check from your normal desktop account.'
