#! /bin/sh

# $Id: grub-install 47566 2018-05-29 12:45:21Z wsl $
# $URL: https://svn.uvt.nl/its-id/trunk/package/grub-efi-rootfstype-workaround/grub-install $

"$0.distrib" "$@"

set -e

fstype=$(stat -f --format=%T /boot)

case $fstype in '')
	echo "Unable to determine fs type for /boot" >&2
	exit 1
esac

insmod="insmod \${cmdpath}/$fstype.mod"
module=/usr/lib/grub/x86_64-efi/$fstype.mod
grubdir=/boot/efi/EFI/ubuntu
grubcfg=$grubdir/grub.cfg
grubmod=$grubdir/xfs.mod

read -r line <$grubcfg
case $line in "$insmod")
	exit 0
esac

if [ ! -e "$module" ]
then
	echo "Unable to find the grub module for fs type '$fstype'" >&2
	exit 1
fi

cp $module $grubmod.new
mv $grubmod.new $grubmod

cat - $grubcfg <<EOT >$grubcfg.new
$insmod
EOT
mv $grubcfg.new $grubcfg
