#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0-only
# This file is part of mkinitcpio.

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

[[ "${KERNEL_INSTALL_INITRD_GENERATOR:-mkinitcpio}" == 'mkinitcpio' ]] || exit 0

[[ "$COMMAND" == "add" ]] || exit 0

GENERATOR_CMD=(mkinitcpio --nopost -k "$KERNEL_IMAGE")
BLS_ARGS=(-g "$KERNEL_INSTALL_STAGING_AREA/initrd")

case "$KERNEL_INSTALL_LAYOUT" in
    uki)
        if [[ "$KERNEL_INSTALL_UKI_GENERATOR" != 'mkinitcpio' ]]; then
            GENERATOR_CMD+=("${BLS_ARGS[@]}")
        else
            GENERATOR_CMD+=(--kernelimage "$KERNEL_IMAGE")

            GENERATOR_CMD+=(-U "$KERNEL_INSTALL_STAGING_AREA/uki.efi" )
        fi
        ;;
    bls)
        GENERATOR_CMD+=("${BLS_ARGS[@]}")
        ;;
    *)
        exit 0
        ;;
esac

(( KERNEL_INSTALL_VERBOSE )) && printf "+ %s\n" "${GENERATOR_CMD[*]}"
"${GENERATOR_CMD[@]}"

# vim: set ft=sh ts=4 sw=4 et:
