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

EAPI=8

inherit optfeature toolchain-funcs xdg fcaps

DESCRIPTION="A monitor of resources"
HOMEPAGE="https://github.com/aristocratos/btop"
SRC_URI="https://github.com/aristocratos/btop/archive/v${PV}.tar.gz -> ${P}.gh.tar.gz"

LICENSE="Apache-2.0 MIT"
SLOT="0"
KEYWORDS="amd64 ~arm arm64 ~loong ~m68k ~mips ~ppc ppc64 ~riscv ~sparc x86"

IUSE="test"
RESTRICT="!test? ( test )"

# КОРИГИРАН СИНТАКСИС: Премахнати са вътрешните кавички и запетаи.
# Флаговете и пътят трябва да са отделни аргументи: [capabilities] [file]
FILECAPS=(
    cap_sys_ptrace,cap_sys_rawio=ep usr/bin/btop
)

BDEPEND="
    app-text/lowdown
"

pkg_setup() {
    if [[ "${MERGE_TYPE}" != "binary" ]]; then
        if tc-is-clang ; then
            if [[ "$(clang-major-version)" -lt 19 ]]; then
                die "sys-process/btop requires >=llvm-core/clang-19.0.0 to build."
            fi
        elif tc-is-gcc ; then
            if [[ "$(gcc-major-version)" -lt 14 ]]; then
                die "sys-process/btop requires >=sys-devel/gcc-14.0.0 to build."
            fi
        else
            die "$(tc-getCXX) is not a supported compiler. Please use >=sys-devel/gcc-14.0.0 or >=llvm-core/clang-19.0.0 instead."
        fi
    fi
}

src_compile() {
    emake CXX="$(tc-getCXX)"
}

src_install() {
    emake DESTDIR="${D}" PREFIX="/usr" install
    emake DESTDIR="${D}" PREFIX="/usr" setcap

    dodoc README.md CHANGELOG.md
}

pkg_postinst() {
    xdg_pkg_postinst

    optfeature "GPU monitoring support (Radeon GPUs)" dev-util/rocm-smi
    optfeature "GPU monitoring support (NVIDIA GPUs)" x11-drivers/nvidia-drivers

    # Това сега ще прочете правилните аргументи от масива без да гърми
    fcaps_pkg_postinst
}