# By			: Nguyen Tu.
# Website		: https://www.aioboot.com/en/partimg-mapper/.
# Last modified	: 05.03.2018.
# Description	: Find and boot into partitions from PartIMG Mapper.
#				: Search Grub2 & Syslinux configfile.

echo "${scanpartmessage}"
set saved_root=$root
set k="_"
for dev in (*); do
	regexp -s device '\((.*)\)' $dev
	if [ "$saved_root" == "$device" ]; then continue; fi
	if ! searchindevice "$device" "on"; then continue; fi
	probe -s fstype -f $device
	if [ -z "$fstype" ] -o [ "$fstype" == "procfs" ]; then continue; fi
	probe -s vollabel -l $device
	probe -s voluuid -u $device
	if [ -z "$vollabel" ]; then set vollabel="${noname}"; fi
	if test -f ($device)/Grub2.cfg; then
		GetHotkey
		menuentry "[${k}] ${device} - ${vollabel} - ${fstype} - ${voluuid}" "$device" --hotkey="${k}" {
			set root=$2
			configfile /Grub2.cfg
		}
	elif test -f ($device)/boot/loader -a -f ($device)/boot/boot1.efi; then
		GetHotkey
		menuentry "[${k}] FreeBSD ${on} (${device}) - ${vollabel} - ${fstype} - ${voluuid}" "$device" --hotkey="${k}" {
			set root=$2
			if [ -n "$efi" ]; then
				chainloader /boot/boot1.efi
			else
				kfreebsd /boot/loader
			fi
		}
	elif test -f ($device)/@/boot/loader -a -f ($device)/@/boot/boot1.efi; then
		GetHotkey
		menuentry "[${k}] FreeBSD ${on} (${device}) - ${vollabel} - ${fstype} - ${voluuid}" "$device" --hotkey="${k}" {
			set root=$2
			if [ -n "$efi" ]; then
				chainloader /@/boot/boot1.efi
			else
				kfreebsd /@/boot/loader
			fi
		}
	elif test -f ($device)/netbsd; then
		GetHotkey
		menuentry "[${k}] NetBSD ${on} (${device}) - ${vollabel} - ${fstype} - ${voluuid}" "$device" --hotkey="${k}" {
			set root=$2
			knetbsd /netbsd
			knetbsd_module_elf /*/installation/miniroot/miniroot.kmod
		}
	elif test -f ($device)/bsd; then
		GetHotkey
		menuentry "[${k}] OpenBSD ${on} (${device}) - ${vollabel} - ${fstype} - ${voluuid}" "$device" --hotkey="${k}" {
			set root=$2
			kopenbsd /bsd
		}
	elif test -f ($device)/bootstrap.ufs.gz -a -f ($device)/easyre.ufs.uzip -a -f ($device)/launchcode -a -f ($device)/boot/kernel/kernel.gz; then
		GetHotkey
		menuentry "[${k}] Easy Recovery Essentials ${on} (${device}) - ${vollabel} - ${fstype} - ${voluuid}" "$device" --hotkey="${k}" {
			set root=$2
			kfreebsd		/boot/kernel/kernel.gz
			kfreebsd_module	/bootstrap.ufs.gz type=md_image
			
			set kFreeBSD.init_path=/rescue/init
			set kFreeBSD.init_shell=/rescue/sh
			#	set kFreeBSD.init_chroot=/.baseroot/
			set kFreeBSD.init_script=/baseroot.rc
			set kFreeBSD.vfs.root.mountfrom=ufs:/dev/md0
			set kFreeBSD.verbose_loading="YES"
			set kFreeBSD.kern.coredump="0"
			set kFreeBSD.hw.psm.synaptics_support="1"
			#	experimental USB3 timeout error workarounds
			set kFreeBSD.hw.usb.xhci.xhci_port_route="-1"
			set kFreeBSD.hw.usb.xhci.msi="0"
			set kFreeBSD.hw.usb.xhci.use_polling="1"

			set kFreeBSD.console="comconsole"
			set kFreeBSD.debug.debugger_on_panic="1"
			set kFreeBSD.hint.uart.0.flags="0x90"
			set kFreeBSD.easyre.vga="0"
		}
	fi
	if test -f ($device)/boot/kernel.i386-pc -a -f ($device)/boot/initfs.i386-pc -a -f ($device)/rescue/livecd.squashfs -a \
	-f ($device)/boot/kernel.x86_64-efi -a -f ($device)/boot/initfs.x86_64-efi; then
		GetHotkey
		menuentry "[${k}] Bitdefender ${on} (${device}) - ${vollabel} - ${fstype} - ${voluuid}" "$device" "$saved_root" --hotkey="${k}" {
			set root=$2
			configfile ($3)/AIO/grub/menuoff/bitdefenderpartimg.cfg
		}
	elif test -f ($device)/boot/grub/i386-pc/cfg/kav_menu.cfg; then
		GetHotkey
		menuentry "[${k}] Kaspersky ${on} (${device}) - ${vollabel} - ${fstype} - ${voluuid}" "$device" "$saved_root" --hotkey="${k}" {
			set root=$2
			configfile ($3)/AIO/grub/menuoff/kasperskypartimg.cfg
		}
	fi
	GetConfigFile "$device" "$vollabel" "$fstype" "$voluuid"
	unset fstype
	unset vollabel
	unset voluuid
done
set root=$saved_root
source ${prefix}/ReturnMain.cfg