c_josie_overlay

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

EAPI=8

inherit desktop systemd toolchain-funcs

DESCRIPTION="Live memory-controller telemetry and memory benchmark for AMD and Intel"
HOMEPAGE="https://github.com/MadGoatHaz/RamSleuth"
SRC_URI="https://github.com/MadGoatHaz/RamSleuth/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"
S="${WORKDIR}/RamSleuth-${PV}"

LICENSE="MIT amd-dkms? ( GPL-2 ) intel-dkms? ( GPL-2 )"
SLOT="0"
KEYWORDS="~amd64"

IUSE="+openrc systemd amd-dkms intel-dkms"
REQUIRED_USE="|| ( openrc systemd )"

COMMON_DEPEND="
	dev-libs/wayland
	media-libs/mesa
	x11-libs/libX11
	x11-libs/libXcursor
	x11-libs/libXi
	x11-libs/libXinerama
	x11-libs/libXrandr
	x11-libs/libxkbcommon
"
DEPEND="${COMMON_DEPEND}
	dev-libs/wayland-protocols
"
RDEPEND="${COMMON_DEPEND}
	acct-group/ramsleuth
	systemd? ( sys-apps/systemd sys-auth/polkit )
	!dev-util/ramsleuth-bin
"
BDEPEND="
	dev-lang/rust
	virtual/pkgconfig
"

# RamSleuth's Rust workspace has 400+ crates. There is no vendored crate
# tarball in the release source, so cargo must fetch the registry during the
# build. This is intentional for this personal overlay; the bin package is the
# offline/reproducible alternative.
RESTRICT="network-sandbox"

# Rust binaries do not consume CFLAGS/LDFLAGS in the usual way.
QA_FLAGS_IGNORED="
	usr/bin/ramsleuth
	usr/bin/ramsleuth-bench
	usr/bin/ramsleuth-client
	usr/bin/ramsleuth-daemon
	usr/bin/ramsleuth-telemetry
	usr/bin/ramsleuth-tui
"

src_compile() {
	export CARGO_HOME="${T}/cargo-home"
	mkdir -p "${CARGO_HOME}" || die
	tc-export AR CC CXX PKG_CONFIG

	cargo build --release --locked --workspace || die "cargo build failed"
}

src_install() {
	local bin size src

	for bin in ramsleuth-daemon ramsleuth-client ramsleuth-tui \
		ramsleuth ramsleuth-bench ramsleuth-telemetry; do
		dobin "target/release/${bin}"
	done

	domenu packaging/shared/RamSleuth.desktop

	for size in 16 24 32 48 64 128 256 512; do
		src="assets/icons/hicolor/${size}/apps/ramsleuth.png"
		insinto "/usr/share/icons/hicolor/${size}/apps"
		newins "${src}" ramsleuth.png
		newins "${src}" RamSleuth.png
		insinto "/usr/share/icons/hicolor/${size}x${size}/apps"
		newins "${src}" ramsleuth.png
		newins "${src}" RamSleuth.png
	done

	insinto /usr/share/pixmaps
	newins assets/icons/hicolor/48/apps/ramsleuth.png ramsleuth.png
	newins assets/icons/hicolor/48/apps/ramsleuth.png RamSleuth.png

	insinto "/usr/share/licenses/${PN}"
	doins LICENSE

	if use openrc; then
		newinitd "${FILESDIR}/ramsleuth.initd" ramsleuth
		newconfd "${FILESDIR}/ramsleuth.confd" ramsleuth
	fi

	if use systemd; then
		systemd_dounit systemd/ramsleuth.service
		insinto /usr/lib/systemd/system-preset
		doins packaging/shared/ramsleuth.preset

		exeinto /usr/bin
		newexe scripts/ramsleuth-setup.sh ramsleuth-setup

		insinto /usr/share/polkit-1/actions
		doins packaging/polkit/90-ramsleuth-setup.policy
	fi

	if use amd-dkms; then
		exeinto /usr/bin
		newexe scripts/install-ryzen-smu-dkms.sh ramsleuth-install-ryzen-smu-dkms

		insinto /usr/share/ryzen-smu-dkms
		doins packaging/ryzen-smu-dkms/dkms.conf

		insinto /usr/share/ryzen-smu-dkms/vendor
		doins packaging/ryzen-smu-dkms/vendor/SUMS.sha256 \
			packaging/ryzen-smu-dkms/vendor/NOTICE.md

		insinto /usr/share/ryzen-smu-dkms/vendor/ryzen-smu
		doins packaging/ryzen-smu-dkms/vendor/ryzen-smu/*
	fi

	if use intel-dkms; then
		exeinto /usr/bin
		newexe scripts/install-intel-dkms.sh ramsleuth-install-intel-dkms

		insinto /usr/share/ramsleuth-intel-dkms/src
		doins kernel/ramsleuth-intel/*
	fi

	if use amd-dkms || use intel-dkms; then
		insinto "/usr/share/licenses/${PN}"
		doins "${FILESDIR}/GPL-2"
	fi

	dodoc README.md CHANGELOG.md Docs/*.md
}

pkg_postinst() {
	if use openrc; then
		elog "RamSleuth's OpenRC service is installed but not enabled."
		elog "Start it and enable it at boot with:"
		elog "  rc-service ramsleuth start"
		elog "  rc-update add ramsleuth default"
		elog "Add your user to the ramsleuth group (then log out and back in):"
		elog "  usermod -aG ramsleuth <user>"
	fi

	if use systemd; then
		elog "RamSleuth's systemd unit and preset are installed."
		elog "Enable and start it with:"
		elog "  systemctl enable --now ramsleuth"
		elog "The GUI one-click setup helper is installed as ramsleuth-setup."
	fi

	if use amd-dkms; then
		elog "Optional AMD ryzen_smu DKMS module (GPL-2) installed."
		elog "Build and load it with:"
		elog "  ramsleuth-install-ryzen-smu-dkms"
		elog "It needs matching kernel headers and DKMS; without it AMD"
		elog "subtimings report N/A (DriverMissing) but the app still runs."
	fi

	if use intel-dkms; then
		elog "Optional Intel ramsleuth_intel DKMS module (GPL-2) installed."
		elog "Build and load it with:"
		elog "  ramsleuth-install-intel-dkms"
		elog "It needs matching kernel headers and DKMS; on Alder Lake and"
		elog "newer it exposes less than the built-in /dev/mem path."
	fi

	elog "The daemon socket is /run/ramsleuth/ramsleuth.sock."
	elog "Try: ramsleuth-client dump, ramsleuth-tui, or ramsleuth"
}