# 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() {
	# linux-mod-r1 runs emake *inside* ${S}, and make only ever looks for
	# GNUmakefile/makefile/Makefile -- never Kbuild. So the repo's Makefile
	# wins, and it resolves the kernel from the running one:
	#   KVER ?= $(shell uname -r); KDIR ?= /lib/modules/$(KVER)
	# That builds against whatever is booted rather than the selected sources,
	# which only shows up once the two diverge (module vermagic mismatch, and
	# a silently wrong module when the ABI happens to match).
	# Drive the kernel's own kbuild instead: it does prefer Kbuild over
	# Makefile, and takes KERNELRELEASE from the tree actually being built
	# against, so this stays correct for any kernel the user selects.
	local modargs=( -C "${KV_OUT_DIR}" M="${S}" )
	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."
}