#!/usr/bin/sh
#
# Kernel hook for efibootmgr.
#
# Arguments passed to this script: $1 pkgname, $2 version.
#
PKGNAME="$1"
VERSION="$2"

. "${ROOTDIR}/etc/default/efibootmgr-kernel-hook"
if [ "x${MODIFY_EFI_ENTRIES}" != x1 ]; then
	exit 0
fi

OPTIONS="${OPTIONS} initrd=/initramfs-${VERSION}.img"

args=""
if [ "x${DISK}" != x ]; then
	args="-d $DISK"
fi

# get major version, e.g. "4.8" for "linux4.8"
major_version=$(echo $PKGNAME | cut -c 6-)

# look for previous entry for this major kernel version
existing_entry=$(efibootmgr | grep "Void Linux with kernel ${major_version}")

# get the boot order
# this is required because when in the next step the existing entry is removed,
# it is also removed from the order so it needs to be restored later
bootorder=$(efibootmgr |grep "BootOrder: " |cut -c 12-)

# if existing, remove it
if [ "$existing_entry" != "" ]; then
  /etc/kernel.d/post-remove/50-efibootmgr $PKGNAME
fi

# create the new entry
efibootmgr -qc $args -L "Void Linux with kernel ${major_version}" -l /vmlinuz-${VERSION} -u "${OPTIONS}"

# restore the boot order
efibootmgr -qo $bootorder
