# Inspired by GLIM and Multiboot USB

set timeout=-1

insmod all_video
set gfxpayload=keep

# ISOs configuration
set isoconfig="/boot/isos.cfg"
export isoconfig

# Persistent folder
set persistent="${prefix}/persistent"
export persistent

# Enable GUI
terminal_output gfxterm

# Get the ROOTUUID of this USB stick
probe -u $root --set=rootuuid
set imgdevpath="/dev/disk/by-uuid/$rootuuid"
export imgdevpath rootuuid

# Enable the Pager
set pager=1

# Get theming
if [ -f "${prefix}/mytheme.cfg" ]; then
	if [ -f "${prefix}/theming.cfg" ]; then
		# load theme_name
		source "${prefix}/mytheme.cfg"
		
		# set theme
		source "${prefix}/theming.cfg"
	fi
fi

# Start the menuentries
##############

menuentry "[c] CPU info " --hotkey=c {
	echo "This CPU:"
	echo " * ${grub_cpu}"
	echo " * ${grub_platform}"
	if cpuid -l ; then
		echo " * supports 64-bit."
	else
		echo " * does not support 64-bit."
	fi

	if cpuid -p ; then
		echo " * supports PAE."
	else
		echo " * does not support PAE."
	fi
	sleep --interruptible 10
}

if [ -f "/boot/tinycore/tinycore.cfg" ]; then
	menuentry "[t] Tiny Core" --hotkey=t {
		configfile /boot/tinycore/tinycore.cfg
	}
fi

menuentry "[l] Local Boot" --hotkey=l {
	insmod chain

	chainloader +1
}


if [ -f "${isoconfig}" ]; then
	menuentry ' ' { true }

	menuentry "[o] Inspect Isos >" --hotkey=o --id=isos {
		echo "Loading various ISO configurations"
		configfile "${isoconfig}"
	}

	menuentry ' ' { true }
fi

submenu "[g] GRUB2 options -> " --hotkey=g {
	menuentry "Show Drives" {
		ls  -l
		sleep --interruptible 9999
	}

	menuentry "Enable LVM support" {
		insmod lvm
	}

	menuentry "Enable RAID support" {
		insmod dm_nv
		insmod mdraid09_be
		insmod mdraid09
		insmod mdradi1x
		insmod raid5rec
		insmod raid6rec
	}

	menuentry "Enable PATA support" {
		insmod ata
		update_paths
	}

	menuentry "Mount encrypted volumnes (LUKS and geli)" {
		insmod luks
		insmod geli
		cryptomount -a
	}

	menuentry "Enable Serial Terminal" {
		serial
		terminal_input --append serial
		terminal_output --apend serial
	}

	menuentry "Enable USB support *experimental*" {
		insmod ohci
		insmod uhci
		insmod usbms
		update_paths
	}
}

if [ ${grub_platform} == "efi" ]; then
	# place UEFI-only menu entries here
	menuentry "Firmware Setup " --class efi {
		fwsetup
	}

	menuentry "UEFI Shell" --class efi {
		insmod fat
		insmod chain
		search --no-floppy --set=root --file /shellx64.efi
		chainloader /shellx64.efi
	}
fi


menuentry ' ' { // Empty Line
	true
}

menuentry "[i] Invaders" --hotkey=i --class=game --class=invaders {
	echo "Starting Invaders"
	multiboot "${persistent}/invaders/invaders"
	boot
}

menuentry "[b] netboot.xyz" --hotkey=b --class=netboot {
	echo "Using netboot.xyz"
	if [ ${grub_platform} == "efi" ]; then
		chainloader "${persistent}/netboot.xyz/netboot.xyz.efi"
	else
		linux16 "${persistent}/netboot.xyz/netboot.xyz.lkrn"
	fi
}

menuentry "[d] read Grub2 Documentation" --hotkey=d --class=docs {
	docs="${persistent}/docs"
	export docs

	configfile "${docs}/show-docs.cfg"
}

menuentry "[m] MemTest86+" --hotkey=m {
	linux16 "${persistent}/memtest86/memtest86+-5.31b.bin"
}

menuentry ' ' { // Empty Line
	true
}

menuentry "[s] Shutdown" --class shutdown --hotkey=s {
	echo "System shutting down..."
	halt
}

menuentry "[r] Reboot" --class shutdown --hotkey=r {
	echo "System rebooting..."
	reboot
}
