#!/bin/sh
. /usr/share/os-prober/common.sh
set -e

partition="$1"
bootpart="$2"
mpoint="$3"
type="$4"

found_item=0

entry_result () {
	if [ "$ignore_item" = 0 ] && \
	   [ -n "$kernel" ] && \
	   [ -e "$mpoint/$kernel" ]; then
		result "$rootpart:$bootpart:$title:$kernel:$initrd:$parameters"
		found_item=1
	fi
	kernel=""
	parameters=""
	initrd=""
	title=""
	ignore_item=0
}

parse_grub_menu () {
	mpoint="$1"
	rootpart="$2"
	bootpart="$3"
	
	kernel=""
	parameters=""
	initrd=""
	title=""
	ignore_item=0

	# grub.cfg is written in shell like language that can contain
	# arbitrary quoting. We need to extract second word according to
	# normal quoting rules. Unfortunately, sticking ``eval $line'' for
	# every line will try to interpret it according to shell
	# grammar and error out for something as simple as "xxx; then".
	# So the following is using it only on known cases.
	# FIXME: it will fail if ``menuentry'' is not the first word on line.
	# case patterns below include SPACE and TAB.
	while read -r line; do
		debug "parsing: $line"
		line="$(printf "%s" "$line" | sed -e 's/^[[:space:]]*//')"
		case "$line" in
			menuentry[" 	"]*)
				entry_result
				set -f
				eval "set -- $line"
				set +f
				title="$2"
				if [ -z "$title" ]; then
					ignore_item=1
				# Currently GRUB2 puts translated strings
				# in grub.cfg, so checking for verbatim
				# (on /dev/.*) will fail if target grub.cfg
				# was created in non-English locale
				elif echo "$title" | grep -q '(.* /dev/[^)]*)$'; then
					log "Skipping entry '$title':"
					log "appears to be an automatic reference taken from another menu.lst"
					ignore_item=1
				fi
			;;
			linux[" 	"]*)
				# And here we do NOT want to strip off
				# existing quting, which will be transferred
				# verbatim in new grub.cfg
				set -f
				set -- $line
				set +f
				# Hack alert: sed off any (hdn,n) but
				# assume the kernel is on the same
				# partition.
				kernel="$(echo "$2" | sed 's/(.*)//')"
				shift 2
				parameters="$@"
				# Systems with a separate /boot will not have
				# the path to the kernel in grub.cfg.
				if [ "$partition" != "$bootpart" ]; then
					kernel="/boot$kernel"
				fi
			;;
			initrd[" 	"]*)
				# And here we do NOT want to strip off
				# existing quting, which will be transferred
				# verbatim in new grub.cfg
				set -f
				set -- $line
				set +f
				initrd="$(echo "$2" | sed 's/(.*)//')"
				# Initrd same.
				if [ "$partition" != "$bootpart" ]; then
					initrd="/boot$initrd"
				fi
			;;
			"}")
				entry_result
			;;
		esac
	done

	entry_result
}

grubcfg=
if [ -e "$mpoint/boot/grub2-efi/grub.cfg" ]; then
	grubcfg="$mpoint/boot/grub2-efi/grub.cfg"
elif [ -e "$mpoint/boot/grub2/grub.cfg" ]; then
	grubcfg="$mpoint/boot/grub2/grub.cfg"
elif [ -e "$mpoint/boot/grub/grub.cfg" ]; then
	grubcfg="$mpoint/boot/grub/grub.cfg"
fi

if [ -n "$grubcfg" ] && \
   ([ ! -e "$mpoint/boot/grub/menu.lst" ] || \
    [ "$grubcfg" -nt "$mpoint/boot/grub/menu.lst" ]); then
	debug "parsing $grubcfg"
	parse_grub_menu "$mpoint" "$partition" "$bootpart" < "$grubcfg"
fi

if [ "$found_item" = 0 ]; then
	exit 1
else
	exit 0
fi
