#!/usr/bin/bash
cd /
# Re-install GRUB if possible.
grubefi() {
	# Is /boot/efi mounted? then EFIMOUNTED is the path to the EFI partition, else null.
	EFIMOUNTED=$(findmnt -no source /boot/efi)
	# If /boot/efi is not mounted, mount it
	[ ! "$EFIMOUNTED" ] && mount /boot/efi
	unset EFI
	if [ -d /sys/firmware/efi ]; then
		# Is /sys/firmware/efi/efivars mounted?
		EFIVARSMOUNTED=$(findmnt /sys/firmware/efi/efivars)
		# If /sys/firmware/efi/efivars is not mounted, mount it.
		[ ! "$EFIVARSMOUNTED" ] &&  mount -t efivarfs none /sys/firmware/efi/efivars
		EFI=yes
	fi
	# we will replace the last installed Slint OS loader in the ESP, if any.
	# There should be one, but you never know.
	find /boot/efi/EFI -name "slint*" -exec  ls -1t -d {} +|sed "s;.*/;;"|head -n1 > "$BOOTLOADERID"
	blid=slint-$(cat "$SLINTVERSION")
	[ -s "$BOOTLOADERID" ] && blid=$(cat "$BOOTLOADERID")
	if [ "$EFI" ]; then
		echo grub-install --bootloader-id="$blid" /dev/"$DRIVENAME"
		grub-install --bootloader-id="$blid" /dev/"$DRIVENAME"
		echo "$?" > "$RETEFI"
	else
	echo grub-install --bootloader-id="blid" --target=x86_64-efi --no-nvram /dev/"$DRIVENAME"
		grub-install --bootloader-id="$BOOTLOADERID" --target=x86_64-efi --no-nvram /dev/"$DRIVENAME"
		echo "$?" > "$RETEFI"
	fi
}
grublegacy() {
	# We reinstall GRUB in Legacy mode unless the root partition be in a GPT
	# and there be no BIOS boot partition in the drive of the root partition.
	PARTTYPE=$(lsblk -lno pttype "$DRIVEPATH"|head -n 1)
	# How many BIOS Boot partitions in this drive?
	BIOSBOOTCOUNT=$(lsblk -lno name,parttypename "$DRIVEPATH"|grep -c 'BIOS boot$')
	[ "$PARTTYPE" = "gpt" ] && [ "$BIOSBOOTCOUNT" -eq 0 ] && return
	echo grub-install --target=i386-pc "$DRIVEPATH"
	grub-install --target=i386-pc "$DRIVEPATH"
	echo "$?" >"$RETLEGACY"
}
# Main
LANG=C
RETEFI=$(mktemp)
RETLEGACY=$(mktemp)
BOOTLOADERID=$(mktemp)
SLINTVERSION=$(mktemp)
unset blid
cut -d" " -f 2 /etc/slint-version >"$SLINTVERSION"
# Grub needs to be reinstalled in the drive of the root partition, if installed there.
# If grub is not installed in this drive, bail out, as another OS loader than ours is used
[ ! -f /boot/grub/grub.cfg ] && exit
[ ! -d /boot/grub/x86_64-efi ] && [ ! -d /boot/grub/i386-pc ] && exit
# We will reinstall GRUB in drive of the root partition
# Find the path of the root partition, the name and path of its drive. 
ROOTPART=$(findmnt -lno source /|sed 's;[[].*;;')
DRIVENAME=$(lsblk -lno pkname "$ROOTPART")
DRIVEPATH=/dev/$DRIVENAME
# Is /boot/efi listed in /etc/fstab?
EFILISTED=$(grep /boot/efi /etc/fstab)
echo 0>"$RETEFI"
echo 0>"$RETLEGACY"
# We only reinstall. A Slint system has had GRUB installed during its installation
# then /boot/grub/x86_64-efi and/or /boot/grub/i386-pcs should exist.
[ "$EFILISTED" ] && [ -d /boot/grub/x86_64-efi ] && grubefi
[ -d /boot/grub/i386-pc ] && grublegacy 
if [ ! "$(cat "$RETEFI")" = 0 ]; then
	echo "!!!WARNING!!! installing GRUB in EFI mode failed!"
	echo "Please find the issue and solve it before rebooting!"
fi
if [ ! "$(cat "$RETLEGACY")" = 0 ] && [ ! -d /sys/firmware/efi ] ; then
	echo "!!!WARNING!!! installing GRUB in Legacy mode failed!"
	echo "Please find the issue and solve it before rebooting!"
fi
[ "$(cat "$RETEFI")" = 0 ] && [ "$(cat "$RETLEGACY")" = 0 ] && /usr/sbin/update-grub
rm   "$RETEFI" "$RETLEGACY" "$BOOTLOADERID" "$SLINTVERSION"
