#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. "/minioslib"

#------------------------------------------------------------------------------
# Kernel Installation Section
#------------------------------------------------------------------------------

if [ "${KERNEL_BPO}" = "true" ]; then
    # Use backports target.
    BPO_TARGET="${DISTRIBUTION}-backports"
    if [ "${KERNEL_AUFS}" = "true" ]; then
        KERNEL_VERSION="6.10"
    fi
else
    BPO_TARGET=""
    KERNEL_VERSION="6.1"
fi

if [ "${DISTRIBUTION_TYPE}" = "debian" ]; then
    if [ "${KERNEL_AUFS}" = "true" ] && { [ "${DISTRIBUTION_PHASE}" = "current" ] || [ "${DISTRIBUTION_PHASE}" = "future" ]; }; then
        KERNEL_IMAGE="linux-image-${KERNEL_VERSION}-mos-${KERNEL_ARCH}"
    else
        if [ "${KERNEL_TYPE}" = "default" ]; then
            KERNEL_IMAGE="linux-image-${KERNEL_ARCH}"
        else
            KERNEL_IMAGE="linux-image-${KERNEL_TYPE}-${KERNEL_ARCH}"
        fi
    fi
elif [ "${DISTRIBUTION_TYPE}" = "ubuntu" ]; then
    KERNEL_IMAGE="linux-image-${KERNEL_VERSION}-mos-${KERNEL_ARCH}"
fi

# Create a temporary package list file for condinapt.
TMP_PKGLIST=$(mktemp /tmp/kernel-package.list.XXXXXX)

if [ "${KERNEL_TYPE}" != "none" ]; then
    run_with_spinner "Updating package lists" apt-get update --allow-releaseinfo-change
    run_with_spinner "Installing initramfs-tools" apt-get install -y --no-install-recommends initramfs-tools
    chmod -x /etc/kernel/postinst.d/initramfs-tools
    chmod -x /etc/kernel/postrm.d/initramfs-tools

    if [ -d "/rootcopy-install" ]; then
        # Check for a local .deb package in /rootcopy-install.
        DEB=$(cd /rootcopy-install && ls linux-image-*${KERNEL_ARCH}.deb 2>/dev/null || true | head -n 1)
        if [ -n "${DEB}" ]; then
            # Use the local .deb file.
            DEB_PATH="/rootcopy-install/${DEB}"
            echo "${DEB_PATH}" >"${TMP_PKGLIST}"
        else
            # No local .deb found; use repository package.
            if [ -n "${BPO_TARGET}" ]; then
                # Append target release using the '@' syntax.
                echo "${KERNEL_IMAGE}@${BPO_TARGET}" >"${TMP_PKGLIST}"
            else
                echo "${KERNEL_IMAGE}" >"${TMP_PKGLIST}"
            fi
        fi
    else
        # No /rootcopy-install directory; install from repository.
        if [ -n "${BPO_TARGET}" ]; then
            echo "${KERNEL_IMAGE}@${BPO_TARGET}" >"${TMP_PKGLIST}"
        else
            echo "${KERNEL_IMAGE}" >"${TMP_PKGLIST}"
        fi
    fi

    # Call the external condinapt script.
    /condinapt -l "${TMP_PKGLIST}" -c "/minios_build.conf" -m "/condinapt.map"
    if [ $? -ne 0 ]; then
        echo "Failed to install packages."
        exit 1
    fi

    # If installing from the repository, hold the kernel package.
    if [ ! -d "/rootcopy-install" ] || [ -z "${DEB}" ]; then
        apt-mark hold "${KERNEL_IMAGE}" >/dev/null
    fi
fi

# Cleanup temporary package list file.
rm -f "${TMP_PKGLIST}"
