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

EAPI=8

inherit linux-mod-r1

# Upstream carries a single stale tag (v0.1.0) and does the real work on master,
# so track a pinned commit instead. This is also the tree that feeds the in-kernel
# driver: board support lands here first and is forwarded to hwmon afterwards
# (e.g. ROG STRIX B850-E GAMING WIFI arrived here before Linux 7.2).
COMMIT="503d0d3d3858f463973f2cfce4a3aa0173567500"

DESCRIPTION="Out-of-tree HWMON driver for ASUS motherboard embedded controller sensors"
HOMEPAGE="https://github.com/zeule/asus-ec-sensors"
SRC_URI="https://github.com/zeule/${PN}/archive/${COMMIT}.tar.gz -> ${P}.tar.gz"
S="${WORKDIR}/${PN}-${COMMIT}"

# SPDX-License-Identifier: GPL-2.0+
LICENSE="GPL-2+"
SLOT="0"
# The driver talks to the ACPI embedded controller of x86 ASUS motherboards;
# no ARM64 hardware is in scope, hence no ~arm64 here.
KEYWORDS="~amd64"

# Needs the hwmon core to register against, plus ACPI for the EC accessors and
# DMI for the per-board register maps.
CONFIG_CHECK="~HWMON ~ACPI ~DMI"
# Below 5.15 upstream needs its -DMILLI compat define, which the plain Kbuild
# path here does not set. Do not pretend to support those kernels.
MODULES_KERNEL_MIN="5.15"

src_compile() {
	# kbuild prefers the repo's Kbuild (obj-m := asus-ec-sensors.o) over its
	# Makefile, so the DKMS/standalone wrapper in the latter is bypassed.
	local modlist=( asus-ec-sensors=updates )
	linux-mod-r1_src_compile
}

src_install() {
	linux-mod-r1_src_install

	# Linux 7.2+ ships an in-tree asus-ec-sensors as well. Without an explicit
	# override depmod may resolve to the in-tree copy, which lags this tree by
	# a release or more in board coverage.
	local depmod_conf="${T}/${PN}.conf"
	{
		echo "# Generated by ${CATEGORY}/${PF}: prefer the out-of-tree driver"
		echo "override asus-ec-sensors * updates"
	} > "${depmod_conf}" || die
	insinto /lib/depmod.d
	doins "${depmod_conf}"
}

pkg_postinst() {
	linux-mod-r1_pkg_postinst

	elog "ASUS EC sensors driver installed (60 boards supported)."
	elog ""
	elog "To load it now:"
	elog "    modprobe -r asus_ec_sensors; modprobe asus_ec_sensors && sensors"
	elog ""
	elog "The module autoloads on boot via its DMI table. If your board is not"
	elog "listed, modprobe returns -ENODEV ('No such device') -- that is a"
	elog "missing board entry, not a broken install. Board additions are"
	elog "accepted upstream at ${HOMEPAGE}"
	elog ""
	elog "With USE=dist-kernel this is rebuilt automatically on kernel upgrades."
}