#!/bin/sh
. /usr/share/debconf/confmodule

log() {
	logger -t grub-installer "$@"
}

findfs () {
	mount | grep "on /target${1%/} " | cut -d' ' -f1
}

findfstype () {
	mount | grep "on /target${1%/} " | cut -d' ' -f5
}

bootfs=$(findfs /boot)
[ -n "$bootfs" ] || bootfs="$(findfs /)"

if [ -z "$bootfs" ]; then
	# Probably not yet mounted.
	exit 0
fi

# Check for the control file to work around lvdisplay refusal to work with
# certian lvm device names.
if lvdisplay "$bootfs" >/dev/null 2>&1 || [ -e "$(dirname $bootfs)/control" ]; then
	log "/boot is a lvm volume ($bootfs), cannot install grub"
	exit 1
fi

bootfstype=$(findfstype /boot)
[ -n "$bootfstype" ] || bootfstype="$(findfstype /)"

if [ "$bootfstype" = "xfs" ]; then
	log "/boot on xfs, unsafe to install grub"
	# Check this first to allow for preseeding forcing grub to xfs.
	db_get grub-installer/install_to_xfs
	if [ "$RET" != true ]; then
		# grub behaves erratically on xfs. If priority is such that the
		# main menu is not being displayed, remove grub from the menu.
		# If main menu is displayed, grub will be left on it so expert
		# users can choose to use it.
		db_get debconf/priority
		if [ "$RET" != low ] && [ "$RET" != medium ]; then
			exit 1
		fi
	fi
fi

db_get grub-installer/skip
if [ "$RET" = true ]; then
	exit 1
fi

exit 0
