#!/bin/sh
set -e

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"

if [ "x$pkgdatadir" = x ]; then
    pkgdatadir="${datarootdir}/grub"
fi

. "$pkgdatadir/grub-mkconfig_lib"

CLASS="--class cix --class gnu-linux --class gnu --class os"

if [ ! -r /etc/cix/grub-config.env ]; then
    exit 0
fi

. /etc/cix/grub-config.env

if [ -z "${CIX_GRUB_KERNEL_GLOB:-}" ]; then
    exit 0
fi

case ${GRUB_DEVICE} in
    /dev/loop/*|/dev/loop[0-9])
        GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
    ;;
esac

: ${GRUB_DISABLE_LINUX_PARTUUID:=true}

if ( [ "x${GRUB_DEVICE_UUID}" = "x" ] && [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] ) \
    || ( [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] && [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ] ) \
    || ( ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" && ! test -e "/dev/disk/by-partuuid/${GRUB_DEVICE_PARTUUID}" ) \
    || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
    LINUX_ROOT_DEVICE=${GRUB_DEVICE}
elif [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ]; then
    LINUX_ROOT_DEVICE=PARTUUID=${GRUB_DEVICE_PARTUUID}
else
    LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi

case x"$GRUB_FS" in
    xbtrfs)
        rootsubvol=`make_system_path_relative_to_its_root /`
        rootsubvol="${rootsubvol#/}"
        if [ "x${rootsubvol}" != x ]; then
            GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
        fi
    ;;
    xzfs)
        rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || zdb -l ${GRUB_DEVICE} | awk -F \' '/ name/ { print $2 }'`
        bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
        LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
    ;;
esac

target_kernel=$(
    find /boot -maxdepth 1 -type f -path "${CIX_GRUB_KERNEL_GLOB}" -printf '%p\n' 2>/dev/null \
        | sort -V \
        | tail -n 1
)

if [ -z "${target_kernel}" ]; then
    exit 0
fi

basename=`basename "${target_kernel}"`
dirname=`dirname "${target_kernel}"`
rel_dirname=`make_system_path_relative_to_its_root "${dirname}"`
version=`echo "${basename}" | sed -e "s,^[^0-9]*-,,g"`
alt_version=`echo "${version}" | sed -e "s,\.old$,,g"`
linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"

initrd=
for i in "initrd.img-${version}" "initrd-${version}.img" \
         "initrd-${alt_version}.img.old" "initrd-${version}.gz" \
         "initrd-${alt_version}.gz.old" "initrd-${version}" \
         "initramfs-${version}.img" "initramfs-${alt_version}.img.old" \
         "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
         "initrd-${alt_version}" "initramfs-${alt_version}.img"; do
    if test -e "${dirname}/${i}" ; then
        initrd="${i}"
        break
    fi
done

if test -z "${initrd}" ; then
    if [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ]; then
        linux_root_device_thisversion=${GRUB_DEVICE}
    else
        linux_root_device_thisversion=PARTUUID=${GRUB_DEVICE_PARTUUID}
    fi
fi

boot_grub_device="${GRUB_DEVICE}"
if [ "x${dirname}" != x/ ] && [ -n "${GRUB_DEVICE_BOOT:-}" ]; then
    boot_grub_device="${GRUB_DEVICE_BOOT}"
fi

prepare_cache="$(prepare_grub_to_access_device ${boot_grub_device} | grub_add_tab)"
boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
title="CIX Linux ${version}"
acpi_title="${title} acpi"
evbdt_title="${title} evb devicetree"
oriondt_title="${title} orion devicetree"

acpi_linux_args="${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} ${CIX_ACPI_GRUB_TARGET_CMDLINE_EXTRA:-}"
linux_args="$(printf '%s\n' "${acpi_linux_args}" | awk '{$1=$1; print}')"

echo "menuentry '$(echo "${acpi_title}" | grub_quote)' ${CLASS} \$menuentry_id_option 'cix-${version}-${boot_device_id}' {"
save_default_entry | grub_add_tab
printf '%s\n' "${prepare_cache}"
echo "	linux	${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${linux_args}"
if test -n "${initrd}" ; then
    echo "	initrd	${rel_dirname}/${initrd}"
fi
echo "}"

dt_linux_args="${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} ${CIX_DT_GRUB_TARGET_CMDLINE_EXTRA:-}"
linux_args="$(printf '%s\n' "${dt_linux_args}" | awk '{$1=$1; print}')"

echo "menuentry '$(echo "${evbdt_title}" | grub_quote)' ${CLASS} \$menuentry_id_option 'cix-${version}-${boot_device_id}' {"
save_default_entry | grub_add_tab
printf '%s\n' "${prepare_cache}"
[ -r /usr/lib/linux-image-$version/cix/sky1-evb.dtb ] && cp /usr/lib/linux-image-$version/cix/sky1-evb.dtb /boot/efi && echo "    devicetree \$config_directory/../../sky1-evb.dtb"
echo "  linux   ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${linux_args}"
if test -n "${initrd}" ; then
    echo "      initrd  ${rel_dirname}/${initrd}"
fi
echo "}"

echo "menuentry '$(echo "${oriondt_title}" | grub_quote)' ${CLASS} \$menuentry_id_option 'cix-${version}-${boot_device_id}' {"
save_default_entry | grub_add_tab
printf '%s\n' "${prepare_cache}"
[ -r /usr/lib/linux-image-$version/cix/sky1-orion-o6.dtb ]  && cp /usr/lib/linux-image-$version/cix/sky1-orion-o6.dtb /boot/efi && echo "    devicetree \$config_directory/../../sky1-orion-o6.dtb"
echo "  linux   ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${linux_args}"
if test -n "${initrd}" ; then
    echo "      initrd  ${rel_dirname}/${initrd}"
fi
echo "}"
