# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

# Modified by	: Nguyen Tu.
# Website		: https://www.aioboot.com.
# Last modified	: 05.03.2018.
# Description:
	# Support for Android-x86 detection.
	# Supports booting Windows in UEFI mode.
	# Improved script.

function get_efis {
	blockdev="$1"
	blockfs="$2"
	efis=""
	for efifile in \
	  ($blockdev)/efi/*/*.efi \
	  ($blockdev)/efi/*/*/*.efi \
	  ($blockdev)/*.efi \
	  ($blockdev)/*/*.efi \
	  ; do
		if [ -f "$efifile" ] ; then
			efis="$efis $efifile"
		fi
	done
	if [ -n "$efis" ]; then
		return 0
	fi
	return 1
}

function osx_entry_add {
set device="$1"
set uuid="$2"
set kerncmd="$3"
set arch="$4"
set args="$5"
set desc="$6"
menuentry "Mac OS X/Darwin $arch-bit ${on} ($device) - $desc" $device $uuid $kerncmd $args {
    set root="$2"
    set uuid="$3"
    set kerncmd="$4"
    set args="$5"
	
    insmod all_video
    set do_resume=0
	if [ /var/vm/sleepimage -nt10 / ]; then
		if xnu_resume /var/vm/sleepimage; then
			set do_resume=1
		fi
	fi
	if [ $do_resume = 0 ]; then
		xnu_uuid ${uuid} uuid
		if [ -f /Extra/DSDT.aml ]; then
			acpi -e /Extra/DSDT.aml
		fi
		if [ /kernelcache -nt /System/Library/Extensions ]; then
			$kerncmd /kernelcache boot-uuid=${uuid} rd=*uuid $args
		else
			$kerncmd /mach_kernel boot-uuid=${uuid} rd=*uuid $args
			if [ /System/Library/Extensions.mkext -nt /System/Library/Extensions ]; then
				xnu_mkext /System/Library/Extensions.mkext
			else
				xnu_kextdir /System/Library/Extensions
			fi
		fi
		if [ -f /Extra/Extensions.mkext ]; then
			xnu_mkext /Extra/Extensions.mkext
		fi
		if [ -d /Extra/Extensions ]; then
			xnu_kextdir /Extra/Extensions
		fi
		if [ -f /Extra/devprop.bin ]; then
			xnu_devprop_load /Extra/devprop.bin
		fi
		if [ -f /Extra/splash.jpg ]; then
			insmod jpeg
			xnu_splash /Extra/splash.jpg
		fi
		if [ -f /Extra/splash.png ]; then
			insmod png
			xnu_splash /Extra/splash.png
		fi
		if [ -f /Extra/splash.tga ]; then
			insmod tga
			xnu_splash /Extra/splash.tga
		fi
		fi
}
}
function oshdd {
    set saved_root=$root
    for dev in (*,*); do
		# $device: parenthesis removed from $dev
		regexp -s device '\((.*)\)' $dev
		# Discard floppy and cdrom devices in search
		# But search them if their search has been enforced

		if ! searchindevice "$device"; then continue; fi

	    # $fstype: filesystem type identified
		
	    probe -s fstype -f $dev
		if [ -z "$fstype" ] -o [ "$fstype" == "procfs" ]; then continue; fi
	    # uuid: filesystem UUID
	    probe -s uuid -u $dev
		
		if [ -n "$efi" ] -a [ "$fstype" == "fat" ]; then
			if [ -f "(${device})/efi/microsoft/boot/bootmgfw.efi" ]; then
				menuentry "Windows 7/8.1/10 ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/microsoft/boot/bootmgfw.efi
				}
			fi
			if [ -f "(${device})/efi/ubuntu/grubx64.efi" ]; then
				menuentry "Ubuntu ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/ubuntu/grubx64.efi
				}
			fi
			if [ -f "(${device})/efi/ubuntu/shimx64.efi" ]; then
				menuentry "Ubuntu [Secure Boot] ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/ubuntu/shimx64.efi
				}
			fi
			if [ -f "(${device})/efi/linuxmint/grubx64.efi" ]; then
				menuentry "Linux Mint ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/linuxmint/grubx64.efi
				}
			fi
			if [ -f "(${device})/efi/arch_grub/grubx64.efi" ]; then
				menuentry "Arch Linux ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/arch_grub/grubx64.efi
				}
			fi
			if [ -f "(${device})/efi/debian/grubx64.efi" ]; then
				menuentry "Debian ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/debian/grubx64.efi
				}
			fi
			if [ -f "(${device})/efi/centos/grubx64.efi" ]; then
				menuentry "Centos ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/centos/grubx64.efi
				}
			fi
			if [ -f "(${device})/efi/redhat/grubx64.efi" ]; then
				menuentry "RedHat ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/redhat/grubx64.efi
				}
			fi
			if [ -f "(${device})/efi/redhat/shim.efi" ]; then
				menuentry "RedHat [Secure Boot] ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/redhat/shim.efi
				}
			fi
			if [ -f "(${device})/efi/gentoo/grubx64.efi" ]; then
				menuentry "Gentoo ${on} ($device)" "$device" {
					root="$2"
					chainloader /efi/gentoo/grubx64.efi
				}
			fi
			submenu "${bootefifile} ${on} ($device)" "$device" "$efis" "$fstype" {
				efis="$3"
				device="$2"
				if get_efis $device $4; then
				for efifile in $efis; do
					menuentry "${efifile} ${on} (${device})" "$efifile" "$device" {
						root="$3"
						chainloader "$2"
					}
				done
				fi
			}
		fi
		
		if [ -n "$pc" ]; then
			probe -p -s tscheme $device
			if [ "$tscheme" == "msdos" ]; then
				if test \
				   '(' -f ($device)/bootmgr -a -f ($device)/boot/bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/boot/Bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/boot/BCD ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/Boot/bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/Boot/Bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/Boot/BCD ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/BOOT/bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/BOOT/Bcd ')' \
				   -o \
				   '(' -f ($device)/bootmgr -a -f ($device)/BOOT/BCD ')' \
				; then
					menuentry "Windows 7/8.1/10/Vista... ${on} ($device)" $device {
						set root="$2"
						chainloader +1
					}
				elif test \
				   '(' -f ($device)/ntldr -a -e ($device)/ntdetect.com -a -f ($device)/boot.ini ')' \
				   -o \
				   '(' -f ($device)/ntldr -a -e ($device)/ntdetect.com -a -f ($device)/BOOT.INI ')' \
				   -o \
				   '(' -f ($device)/ntldr -a -e ($device)/NTDETECT.COM -a -f ($device)/boot.ini ')' \
				   -o \
				   '(' -f ($device)/ntldr -a -e ($device)/NTDETECT.COM -a -f ($device)/BOOT.INI ')' \
				   -o \
				   '(' -f ($device)/NTLDR -a -e ($device)/ntdetect.com -a -f ($device)/boot.ini ')' \
				   -o \
				   '(' -f ($device)/NTLDR -a -e ($device)/ntdetect.com -a -f ($device)/BOOT.INI ')' \
				   -o \
				   '(' -f ($device)/NTLDR -a -e ($device)/NTDETECT.COM -a -f ($device)/boot.ini ')' \
				   -o \
				   '(' -f ($device)/NTLDR -a -e ($device)/NTDETECT.COM -a -f ($device)/BOOT.INI ')' \
				; then
					menuentry "Windows NT/2000/XP ${on} ($device)" $device {
						set root="$2"
						regexp -s devnum 'hd([0-9]+)' $root
						if test "$devnum" != "0"; then
						drivemap -s hd0 $root
						fi
						chainloader +1
					}
				elif test -f ($device)/windows/win.com; then
					menuentry "Windows 98/ME ${on} ($device)" $device {
						set root="$2"
						regexp -s devnum 'hd([0-9]+)' $root
						if test "$devnum" != "0"; then
						drivemap -s hd0 $root
						fi
						chainloader +1
					}
				elif test -f ($device)/io.sys -a -f ($device)/command.com; then
					menuentry "MS-DOS ${on} ($device)" $device {
						set root="$2"
						regexp -s devnum 'hd([0-9]+)' $root
						if test "$devnum" != "0"; then
						drivemap -s hd0 $root
						fi
						chainloader +1
					}
				elif test -f ($device)/kernel.sys; then
					menuentry "FreeDOS ${on} ($device)" $device {
						set root="$2"
						regexp -s type '([fh])d[0-9]+' $root
						regexp -s devnum '[fh]d([0-9]+)' $root
						if test $type = 'h' -a "$devnum" != "0"; then
						drivemap -s hd0 $root
						fi
						chainloader +1
					}
				fi
			fi
			unset tscheme
		fi
		
	    if test "$fstype" = ufs1 -o "$fstype" = ufs2 -a \
		-e ($device)/boot/kernel/kernel -a \
		-e ($device)/boot/device.hints; then

			freebsd_ufs_variants $device $fstype $uuid

	    elif test "$fstype" = zfs -a \
		-e ($device)/@/boot/kernel/kernel -a \
		-e ($device)/@/boot/device.hints; then

			freebsd_zfs_variants $device

	    elif test "$fstype" = hfsplus -a -f ($device)/mach_kernel -a -d ($device)/Users; then
			osx_entry_add $device $uuid "xnu_kernel" "32" "-v" "Verbose Mode"
			osx_entry_add $device $uuid "xnu_kernel" "32" "-x" "Safe Mode"
			osx_entry_add $device $uuid "xnu_kernel" "32" "-s" "Single User"
			osx_entry_add $device $uuid "xnu_kernel" "32" "-f" "Ignore Caches"
			osx_entry_add $device $uuid "xnu_kernel64" "64" "-v" "Verbose Mode"
			osx_entry_add $device $uuid "xnu_kernel64" "64" "-x" "Safe Mode"
			osx_entry_add $device $uuid "xnu_kernel64" "64" "-s" "Single User"
			osx_entry_add $device $uuid "xnu_kernel64" "64" "-f" "Ignore Caches"

	    else
			set root=$device
			for file in /boot/vmlinuz-* /boot/linux-* /boot/kernel-genkernel-*; do
				if test -f $file; then
				regexp -s version '/boot/vmlinuz-(.*)' $file
				regexp -s version '/boot/linux-(.*)' $file
				regexp -s version '/boot/kernel-genkernel-(.*)' $file

				menuentry "Linux $file ${on} ($device)" $device $uuid $file $version {
					set root="$2"
					set uuid="$3"
					set kernel="$4"
					set version="$5"

					linux $kernel root=UUID=$uuid ro
					if test -f /boot/initrd-$version.img; then
					initrd /boot/initrd-$version.img
					elif test -f /boot/initrd.img-$version; then
					initrd /boot/initrd.img-$version
					elif test -f /boot/initrd-$version; then
					initrd /boot/initrd-$version
					elif test -f /boot/initramfs-genkernel-$version; then
					initrd /boot/initramfs-genkernel-$version
					fi
				}

				menuentry "Linux $file (single) ${on} ($device)" $device $uuid $file $version {
					set root="$2"
					set uuid="$3"
					set kernel="$4"
					set version="$5"

					linux $kernel root=UUID=$uuid ro single
					if test -f /boot/initrd-$version.img; then
					initrd /boot/initrd-$version.img
					elif test -f /boot/initrd.img-$version; then
					initrd /boot/initrd.img-$version
					elif test -f /boot/initrd-$version; then
					initrd /boot/initrd-$version
					elif test -f /boot/initramfs-genkernel-$version; then
					initrd /boot/initramfs-genkernel-$version
					fi
				}
				fi
			done
			for file in /android-*/system/build.prop; do
				if ! test -f $file; then continue; fi
				regexp -s rversion '/android-(.*)/system/build.prop' $file
				set adroidos="x86"
				set stype="x86"
				set bit="32-bit"
				if test -f ($device)/android-${rversion}/system/etc/preferred-apps/remixos.xml; then
					set adroidos="remixos"
				fi
				if test -d ($device)/android-${rversion}/system/lib64; then
					set stype="x86_64"
					set bit="64-bit"
				fi
				if test \
				'(' -f ($device)/android-${rversion}/initrd.img -a -f ($device)/android-${rversion}/kernel -a -f ($device)/android-${rversion}/ramdisk.img  ')' \
				; then
					if [ "$adroidos" == "remixos" ]; then
						menuentry "Remix OS ${bit} ${on} ($device) - ${version} ${rversion}" $device $rversion $stype {
							set root="$2"
							set rversion="$3"
							set stype="$4"
							linux	/android-${rversion}/kernel vga=791 quiet root=/dev/ram0 androidboot.hardware=remix_${stype} androidboot.selinux=permissive DATA= SRC=/android-${rversion}
							initrd	/android-${rversion}/initrd.img
						}
						menuentry "Remix OS ${bit} (${debugmode}) ${on} ($device) - ${version} ${rversion}" $device $rversion $stype {
							set root="$2"
							set rversion="$3"
							set stype="$4"
							linux	/android-${rversion}/kernel vga=788 root=/dev/ram0 androidboot.hardware=remix_${stype} androidboot.selinux=permissive DATA= DEBUG=2 SRC=/android-${rversion}
							initrd	/android-${rversion}/initrd.img
						}
						menuentry "Remix OS ${bit} (${vesa}) ${on} ($device) - ${version} ${rversion}" $device $rversion $stype {
							set root="$2"
							set rversion="$3"
							set stype="$4"
							linux	/android-${rversion}/kernel vga=788 nomodeset root=/dev/ram0 androidboot.hardware=remix_${stype} androidboot.selinux=permissive DATA= DEBUG=2 SRC=/android-${rversion}
							initrd	/android-${rversion}/initrd.img
						}
						menuentry "Remix OS ${bit} (Debug video=LVDS-1:d) ${on} ($device) - ${version} ${rversion}" $device $rversion $stype {
							set root="$2"
							set rversion="$3"
							set stype="$4"
							linux	/android-${rversion}/kernel vga=791 video=LVDS-1:d root=/dev/ram0 androidboot.hardware=remix_${stype} androidboot.selinux=permissive DATA= DEBUG=2 SRC=/android-${rversion}
							initrd	/android-${rversion}/initrd.img
						}
					elif [ "$adroidos" == "x86" ]; then
						menuentry "Android-x86 ${bit} ${on} ($device) - ${version} ${rversion}" $device $rversion $stype {
							set root="$2"
							set rversion="$3"
							set stype="$4"
							linux	/android-${rversion}/kernel vga=791 quiet root=/dev/ram0 androidboot.hardware=android_${stype} SRC=/android-${rversion}
							initrd	/android-${rversion}/initrd.img
						}
						menuentry "Android-x86 ${bit} (${debugmode}) ${on} ($device) - ${version} ${rversion}" $device $rversion $stype {
							set root="$2"
							set rversion="$3"
							set stype="$4"
							linux	/android-${rversion}/kernel vga=788 root=/dev/ram0 androidboot.hardware=android_${stype} DEBUG=2 SRC=/android-${rversion}
							initrd	/android-${rversion}/initrd.img
						}
						menuentry "Android-x86 ${bit} (${vesa}) ${on} ($device) - ${version} ${rversion}" $device $rversion $stype {
							set root="$2"
							set rversion="$3"
							set stype="$4"
							linux	/android-${rversion}/kernel vga=788 nomodeset root=/dev/ram0 androidboot.hardware=android_${stype} DEBUG=2 SRC=/android-${rversion}
							initrd	/android-${rversion}/initrd.img
						}
						menuentry "Android-x86 ${bit} (Debug video=LVDS-1:d) ${on} ($device) - ${version} ${rversion}" $device $rversion $stype {
							set root="$2"
							set rversion="$3"
							set stype="$4"
							linux	/android-${rversion}/kernel vga=791 video=LVDS-1:d root=/dev/ram0 androidboot.hardware=android_${stype} DEBUG=2 SRC=/android-${rversion}
							initrd	/android-${rversion}/initrd.img
						}
					fi
				fi
			done
		fi
		unset fstype
    done
    set root=$saved_root
}
echo "${scanningos}"
oshdd
source ${prefix}/ReturnMain.cfg