# SUSE Migration activation menu entry based on isofile

# Source grub config library
. /usr/share/grub2/grub-mkconfig_lib

CLASS="--class memtest86 --class gnu --class tools"

if [ -z "${GRUB_DISTRIBUTOR}" ] ; then
    OS=Migration
else
    OS="${GRUB_DISTRIBUTOR} Migration"
    CLASS="--class $(
        echo "${GRUB_DISTRIBUTOR}" | tr '[:upper:]' '[:lower:]' | cut -d' ' -f1
    ) ${CLASS}"
fi

migration_iso=$(echo /usr/share/migration-image/*-Migration.*.iso)

root_device=$(
    lsblk -p -n -r -o NAME,MOUNTPOINT | grep -E "/$" | uniq | cut -f1 -d" "
)

# check if location of migration_iso is not on the root_device
image_fs_device=${root_device}
index=4
for path in "/usr/share" "/usr";do
    if mountpoint -q ${path};then
        image_fs_device=$(
            lsblk -p -n -r -o NAME,MOUNTPOINT |\
            grep -E "${path}$" | uniq | cut -f1 -d" "
        )
        migration_iso=/$(echo "${migration_iso}" | cut -f"${index}"- -d/)
        break
    fi
    index=$((index -1))
done

image_fs_uuid=$(blkid -s UUID -o value "${image_fs_device}")
image_fs_type=$(blkid -s TYPE -o value "${image_fs_device}")

if [[ ${image_fs_type} =~ ^ext[[:digit:]] ]] ; then
   image_fs_type="ext2"
fi

# init boot options needed for live boot
boot_options="rd.live.image root=live:CDLABEL=CDROM"

# add auto activation of devices in soft raid mode
if mdadm --detail "${root_device}" &>/dev/null; then
    boot_options="${boot_options} rd.auto"
fi

# add relevant boot options used on the host to migrate
for host_boot_option in $(xargs -n1 < /proc/cmdline);do
    case "${host_boot_option}" in
        root=*)
            continue
        ;;
        quiet)
            continue
        ;;
    esac
    boot_options="${boot_options} ${host_boot_option}"
done

if grub_file_is_not_garbage "${migration_iso}"; then
    kernel="(loop)/boot/x86_64/loader/linux"
    initrd="(loop)/boot/x86_64/loader/initrd"
    boot_device_id="$(grub_get_device_id "${GRUB_DEVICE_BOOT}")"
    printf "menuentry '%s' %s \${menuentry_id_option} '%s' {\n" \
        "${OS}" "${CLASS}" "Migration-${boot_device_id}"
    if btrfs subvolume show /boot/grub2/x86_64-efi &>/dev/null;then
        printf "    btrfs-mount-subvol / /boot/grub2/x86_64-efi %s\n" \
            "/@/boot/grub2/x86_64-efi"
        printf "    insmod loopback\n"
    fi
    printf "    insmod %s\n" "${image_fs_type}"
    printf "    search --no-floppy --fs-uuid --set=root %s\n" "${image_fs_uuid}"
    printf "    set isofile='%s'\n" \
        "${migration_iso}"
    printf "    set linux=linux\n"
    printf "    set initrd=initrd\n"
    printf "    if [ \"\${grub_platform}\" = \"efi\" ]; then\n"
    printf "        set linux=linuxefi\n"
    printf "        set initrd=initrdefi\n"
    printf "    fi\n"
    printf "    loopback loop (\$root)\$isofile\n"
    printf "    \$linux %s iso-scan/filename=\$isofile %s\n" \
        "${kernel}" "${boot_options}"
    printf "    \$initrd %s\n" \
        "${initrd}"
    printf "}\n"

    printf "set default='%s'\n" "${OS}"
    printf "set timeout=1\n"
fi
