#!/bin/sh -efu
### This file is covered by the GNU General Public License
### version 2 or later.
###
### Copyright (C) 2019  ALT Linux Team
### Author: Leonid Krivoshein <klark@altlinux.org>

menuentry() {
	echo "${prefix}$1"
	entry=1
}

submenu() {
	prefix="${prefix}${1}>"
}

endmenu() {
	if [ $entry -ne 0 ]; then
		entry=0
		return
	fi
	if [ -z "$prefix" ]; then
		return
	fi
	prefix="${prefix%>}"
	case "$prefix" in
	*">"*)	prefix="${prefix%>*}";;
	*)	prefix=;;
	esac
}


grubmenu="$(mktemp -q "${TMPDIR-/tmp}/grubmenu-XXXXXXXX.tmp")"

cat "$1" | sed \
	-e 's/^[[:space:]]*//g' \
	-e 's/[[:space:]]*$//g' \
	-e 's/^}$/endmenu/g' \
	-e 's/[[:space:]]*{$//g' |
grep -E '^(menuentry |submenu |endmenu)' > "$grubmenu"

prefix=
entry=0
menuentry_id_option=

. "$grubmenu"
rm -f "$grubmenu"

