# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8
K_BASE_VER="6.12"
K_FROM_GIT="yes"
K_GENPATCHES_VER="6"
K_NODRYRUN="yes"
K_PREPATCHED="yes"
# K_SECURITY_UNSUPPORTED="yes"

K_WANT_GENPATCHES="base extras experimental"

ETYPE="sources"

# SRC_URI="${GENPATCHES_URI}"

DESCRIPTION="Full XanMod sources including the Gentoo patchset"
HOMEPAGE="https://xanmod.org/"

if [[ ${PV} == 9999 ]]; then
    inherit git-r3
    EGIT_REPO_URI="https://gitlab.com/xanmod/linux.git"

    # if [[ -n ${EGIT_OVERRIDE_BRANCH_XANMOD_LINUX} ]]; then
    #     CKV=$(git ls-remote --tags ${EGIT_REPO_URI} | awk '{print $2}' | sed 's|refs/tags/||' | sort -V | tail -n 1)
    # elif [[ -n ${EGIT_OVERRIDE_COMMIT_XANMOD_LINUX} ]]; then
    #     CKV=${EGIT_OVERRIDE_COMMIT_XANMOD_LINUX}
    # else
    #     CKV=$(git ls-remote --tags ${EGIT_REPO_URI} | awk '{print $2}' | sed 's|refs/tags/||' | sort -V | tail -n 1)
    # fi
    # pre_unpack
fi

CKV=${EGIT_OVERRIDE_COMMIT_XANMOD_LINUX}

if [[ "${CKV}" == "" ]]; then
    CKV=${K_BASE_VER}
fi

inherit kernel-2

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

# Dependencies
RDEPEND="
    sys-kernel/linux-headers
    sys-kernel/linux-firmware
"

OKV="xanmod1"

S="${WORKDIR}/linux-${CKV}"

# pre_unpack() {
#     if [[ -n ${EGIT_OVERRIDE_BRANCH_XANMOD_LINUX} ]]; then
#         CKV=$(git ls-remote --tags ${EGIT_REPO_URI} | awk '{print $2}' | sed 's|refs/tags/||' | sort -V | tail -n 1)
#     elif [[ -n ${EGIT_OVERRIDE_COMMIT_XANMOD_LINUX} ]]; then
#         CKV=${EGIT_OVERRIDE_COMMIT_XANMOD_LINUX}
#     else
#         CKV=$(git ls-remote --tags ${EGIT_REPO_URI} | awk '{print $2}' | sed 's|refs/tags/||' | sort -V | tail -n 1)
#     fi
# }

src_unpack() {
    UNIPATCH_STRICTORDER=1
    
    if [[ "${EGIT_OVERRIDE_COMMIT_XANMOD_LINUX}" == "" ]]; then
        CKV=$(git ls-remote --tags ${EGIT_REPO_URI} | awk '{print $2}' | sed 's|refs/tags/||' | sort -V | tail -n 1)
        S="${WORKDIR}/linux-${CKV}"
    fi

    git-r3_src_unpack
    mv "xanmod-sources-9999" "linux-${CKV}"

    if [[ "${EGIT_OVERRIDE_BRANCH_XANMOD_LINUX}" != "" ]]; then
        cd "linux-${CKV}"
        git checkout ${CKV}

        ewarn "Using tag ${CKV} instead of branch ${EGIT_OVERRIDE_BRANCH_XANMOD_LINUX}"
    fi
}

src_prepare() {
    default_src_prepare
}

# src_configure() {
#     kernel_config
# }

# src_compile() {
#     kernel_compile
# }

src_install() {
    kernel-2_src_install
}

# pkg_postinst() {
#     # Post-installation instructions
#     einfo "XanMod kernel ${PV} has been installed."
#     einfo "Remember to update your bootloader configuration."
# }
