#!/bin/bash

COMMAND=${1:?}
KERNEL_VERSION=${2:?}
#shellcheck disable=SC2034
BOOT_DIR_ABS=${3}
KERNEL_IMAGE=${4}

# If the initrd was provided on the kernel command line, we shouldn't generate our own.
if [[ "${COMMAND}" != "add" || "${#}" -gt 4 ]]; then
    exit 0
fi

# Do not attempt to create initramfs if the supplied image is already a UKI
if [[ "${KERNEL_INSTALL_IMAGE_TYPE}" = "uki" ]]; then
    exit 0
fi

# Skip kernel installation if zfs has not been built yet
if (grep ' / ' /proc/mounts | grep -q zfs) && ! modinfo zfs -k "${KERNEL_VERSION}" &>/dev/null
then
     [[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo \
        "root is zfs, but the zfs module is missing, please build the zfs module and retry"
    exit 77
fi

INITRAMFS_GEN_ARGS=(
    # if uefi=yes is used, dracut needs to locate the kernel image
    --kernel-image "${KERNEL_IMAGE}"
    --force
)

[[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && INITRAMFS_GEN_ARGS+=( --verbose )

if [[ ${KERNEL_INSTALL_INITRD_GENERATOR} == dracut ]]; then
    if [[ ${KERNEL_INSTALL_UKI_GENERATOR} == dracut ]]; then
        # We are chosen to generate the UKI as well as initrd
        IMAGE=uki.efi
        INITRAMFS_GEN_ARGS+=( --uefi )
    else
        # We are the initrd generator
        IMAGE=initrd
        INITRAMFS_GEN_ARGS+=( --no-uefi )
    fi
else
    [[ ${KERNEL_INSTALL_VERBOSE} == 1 ]] && echo \
        "initrd_generator=${KERNEL_INSTALL_INITRD_GENERATOR}, not running dracut"
    exit 0
fi

INITRAMFS_GEN_ARGS+=(
    # positional arguments
    "${KERNEL_INSTALL_STAGING_AREA}/${IMAGE}" "${KERNEL_VERSION}"
)

dracut "${INITRAMFS_GEN_ARGS[@]}" || exit 1
