# Load ISO configuration files
#
insmod regexp

set isoconfdir="${isoconfig}.d"
export isoconfdir

if [ ! -d "${isoconfdir}" ]; then
	echo "Cannot read ${isoconfdir}"
	exit
fi

# Loop over both FAT32 and ExFAT filesystems
isouuids="EAB6-E0E9 0BB2-9158"

for isouuid in $isouuids
do
	echo ""
	echo "Searching isoroot on UUID=${isouuid}"

	search --no-floppy --fs-uuid --set=isodev $isouuid
	set isopath="/isos"

	set isoroot="(${isodev})${isopath}"

	if [ ! -d "${isoroot}" ]; then
		echo "Ignoring invalid iso root at ${isoroot}"
		sleep 1
		continue
	fi

	# split isoroot again
	echo "Found iso root at ${isoroot}"

	regexp \
		--set 1:isodev \
		--set 2:isopath \
		"^(\(.*\))?(.*)\$" "${isoroot}"

	echo "isodev: ${isodev}"
	echo "isopath: ${isopath}"

	# Export the variables
	export isoroot
	export isopath
	export isodev

	# Read appropriate iso configs
	echo "Reading ${isoconfdir}"
	for conf in ${isoconfdir}/*.cfg; do
		if [ ! -f "$conf" ]; then
			echo "No valid configuration to read"
			break;
		fi

		#echo "Sourcing $conf "
		source "${conf}"
	done

	sleep 1
done

# let the echos be readable
echo "Waiting for 5 seconds..."
sleep 5

menuentry ' ' { true }

menuentry "[Esc] Back to Menu" {
	configfile "${prefix}/grub.cfg"
}
