# Copyright 2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=7

DESCRIPTION="Custom-built amd64 kernel binpkg from gentoo-sources"
HOMEPAGE="https://www.kernel.org"
SRC_URI=""

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="amd64 ~amd64"
IUSE="initramfs"

DEPEND=""
RDEPEND="
        ${DEPEND}
        !sys-kernel/gentoo-kernel:${SLOT}
        !sys-kernel/vanilla-kernel:${SLOT}
        !sys-kernel/vanilla-kernel-bin:${SLOT}
        sys-boot/grub
        initramfs? ( sys-kernel/dracut )
        "
BDEPEND="=sys-kernel/gentoo-sources-${PV}"

QA_PREBUILT='*'

S=${WORKDIR}
SRCDIR="/usr/src/linux-${PV}-gentoo"
BUILDDIR="${T}/build-${PV}"

# We set up a temp builddir, not /usr/src/linux
# But we do run "make prepare" in the kernel source tree, so that
# other packages can build with it. We do NOT set the kernel symlink.
src_prepare() {
        default
        cp -r ${SRCDIR}/ ${BUILDDIR}
}

# If we have a config file for the kernel (package) version ${PV}
# that we are building for, we copy that to the temp builddir.
# Else we try to find and use a kernel config from an earlier build.
# Note that all emerge-specific flags from make.conf are also set.
src_configure() {
        unset ARCH
        if [ -e "${FILESDIR}/config-${PV}-gentoo" ]; then
                cp ${FILESDIR}/config-${PV}-gentoo ${BUILDDIR}/.config
                cd $BUILDDIR 
                emake olddefconfig && return
        elif [ -d ${FILESDIR}/ ]; then
                latest=`ls ${FILESDIR}/config-* | sort -V | tail -1`
                if [ -e ${latest} ]; then
                        cp ${latest} ${BUILDDIR}/.config
                        cd $BUILDDIR 
                        emake olddefconfig && return
                fi
        fi
        # fallback
        die "Failed to configure kernel source"
}

# Compile normally.
# Note that all emerge-specific flags from make.conf are also set.
src_compile() {
        cd $BUILDDIR
        emake
        emake INSTALL_MOD_PATH="${T}" modules_install && return
        # fallback
        die "Failed to compile kernel source"
}

# We copy only the kernel, System.map and .config to /boot.
# There is NO INITRD here! It is generated by dracut in the postinst step if
# the "initramfs" USE flag is set.
# Then copy the modules.
# Note: The kernel sources are not required on the target system, so the various symlinks in /lib/modules/$version
#       that usually point to /usr/src/linux are not set here. Uncomment below to change this.
src_install() {
        dodir /boot
        insinto /boot
        newins "${BUILDDIR}/arch/x86/boot/bzImage" vmlinuz-${PV}-gentoo
        newins "${BUILDDIR}/.config" config-${PV}-gentoo
        newins "${BUILDDIR}/System.map" System.map-${PV}-gentoo

        rm -f ${T}/lib/modules/${PV}-gentoo/build
        rm -f ${T}/lib/modules/${PV}-gentoo/source
#       ln -s ${SRCDIR} ${T}/lib/modules/${PV}-gentoo/build
#       ln -s ${SRCDIR} ${T}/lib/modules/${PV}-gentoo/source
        dodir "/lib/modules/${PV}-gentoo"
        insinto "/lib/modules"
        doins -r "${T}/lib/modules/${PV}-gentoo"
}

# Generate grub config for new kernel and module maps.
pkg_postinst() {
        if use initramfs ; then
                elog "Creating initramfs" 
                dracut --hostonly --force --kver ${PV}-gentoo
        fi
        elog "Kernel files were installed successfully. Running grub-mkconfig -o /boot/grub/grub.cfg"
        grub-mkconfig -o /boot/grub/grub.cfg
        depmod -a ${PV}-gentoo
}
