#!/bin/bash -e

source /usr/lib/grub/grub-mkconfig_lib

HOOKS=/usr/share/ubuntu/hooks/ubuntu-recovery/grub
[ -d "$HOOKS" ] && \
for i in `ls $HOOKS 2>/dev/null || true`; do
    source "$HOOKS/$i"
done

echo "Generating '#RECOVERY_TEXT#' entry ..." >&2

cat << EOF
menuentry "#RECOVERY_TEXT#"${OPTS} {
        search --no-floppy --hint '(hd0,#PARTITION#)' --set --fs-uuid #UUID#
        if [ -s /boot/grub/common.cfg ]; then
            source /boot/grub/common.cfg
        else
            set options="boot=casper automatic-ubiquity noprompt quiet splash"
        fi
        
        if [ "\${grub_platform}" != "efi" ]; then
            if [ -f \${prefix}/nomodesetlist.txt ]; then
                if hwmatch \${prefix}/nomodesetlist.txt 3; then
                    if [ \${match} != 0 ]; then
                        set options="nomodeset \$options"
                    fi
                fi
            fi
        fi

        #Support starting from a loopback mount (Only support ubuntu.iso for filename)
        if [ -f /ubuntu.iso ]; then
            loopback loop /ubuntu.iso
            set root=(loop)
            set options="\$options iso-scan/filename=/ubuntu.iso"
        fi
        if [ -n "\${lang}" ]; then
            set options="\$options locale=\$lang"
        fi

        linux   /casper/vmlinuz ubuntu-recovery/recovery_type=hdd \$options #EXTRA#
        initrd	/casper/initrd
}
EOF
