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

EAPI=8

PYTHON_COMPAT=( python3_{12..15} )
PYTHON_REQ_USE="sqlite"

inherit flag-o-matic linux-info meson python-single-r1 systemd

DESCRIPTION="Reliability, Availability and Serviceability logging tool"
HOMEPAGE="https://github.com/mchehab/rasdaemon"
SRC_URI="https://github.com/mchehab/rasdaemon/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"

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

# Meson feature options exposed as RASDAEMON_FEATURES. The ARM and RISC-V
# ones and cpu-fault-isolation (it only consumes ARM and RISC-V events) do
# nothing on the architectures in KEYWORDS.
RASDAEMON_DEFAULT_FEATURES=(
	aer devlink diskerror erst extlog mce memory-ce-pfa memory-failure
	memory-row-ce-pfa pcie-edpc
)
# Off by default: ABRT is not packaged in Gentoo, upstream marks the generic BMC
# reporter and the signal event as experimental, the signal event records
# unrelated signals while the service starts, and without CXL devices the cxl
# handler only logs a missing tracepoint for each of its events at startup.
RASDAEMON_OPTIONAL_FEATURES=( abrt-report bmc-generic cxl signal )
IUSE="
	${RASDAEMON_DEFAULT_FEATURES[@]/#/+rasdaemon_features_}
	${RASDAEMON_OPTIONAL_FEATURES[@]/#/rasdaemon_features_}
	selinux test
"
REQUIRED_USE="
	${PYTHON_REQUIRED_USE}
	rasdaemon_features_bmc-generic? ( rasdaemon_features_aer )
	rasdaemon_features_erst? ( rasdaemon_features_mce )
	rasdaemon_features_pcie-edpc? ( rasdaemon_features_aer )
"
RESTRICT="!test? ( test )"

DEPEND="
	dev-db/sqlite
	dev-libs/libtraceevent
	elibc_musl? ( sys-libs/argp-standalone )
	rasdaemon_features_aer? ( >=sys-apps/pciutils-3.10.0 )
	test? ( dev-util/cmocka )
"
RDEPEND="
	${PYTHON_DEPS}
	dev-db/sqlite
	dev-libs/libtraceevent
	$(python_gen_cond_dep '
		dev-python/sqlalchemy[sqlite,${PYTHON_USEDEP}]
	')
	sys-apps/dmidecode
	rasdaemon_features_aer? ( >=sys-apps/pciutils-3.10.0 )
	rasdaemon_features_bmc-generic? ( sys-apps/ipmitool )
	selinux? ( sec-policy/selinux-rasdaemon )
"
BDEPEND="
	${PYTHON_DEPS}
	$(python_gen_cond_dep '
		dev-python/sqlalchemy[sqlite,${PYTHON_USEDEP}]
	')
"

PATCHES=(
	"${FILESDIR}/${PN}-1.0.0-meson-install.patch"
	"${FILESDIR}/${PN}-1.0.0-services.patch"
)

pkg_setup() {
	python-single-r1_pkg_setup
	linux-info_pkg_setup
	local CONFIG_CHECK="~RAS ~TRACING"
	use rasdaemon_features_aer && CONFIG_CHECK+=" ~PCIEAER"
	use rasdaemon_features_bmc-generic && CONFIG_CHECK+=" ~IPMI_SI ~IPMI_DEVICE_INTERFACE"
	use rasdaemon_features_cxl && CONFIG_CHECK+=" ~CXL_BUS"
	use rasdaemon_features_devlink && CONFIG_CHECK+=" ~NET_DEVLINK"
	use rasdaemon_features_erst && CONFIG_CHECK+=" ~ACPI_APEI ~PSTORE"
	use rasdaemon_features_extlog && CONFIG_CHECK+=" ~ACPI_EXTLOG ~DEBUG_FS"
	use rasdaemon_features_mce && CONFIG_CHECK+=" ~X86_MCE"
	use rasdaemon_features_pcie-edpc && CONFIG_CHECK+=" ~PCIE_DPC"
	if use rasdaemon_features_memory-ce-pfa || use rasdaemon_features_memory-row-ce-pfa; then
		CONFIG_CHECK+=" ~MEMORY_FAILURE ~MEMORY_HOTPLUG"
	elif use rasdaemon_features_memory-failure; then
		CONFIG_CHECK+=" ~MEMORY_FAILURE"
	fi
	local WARNING_DEBUG_FS="CONFIG_DEBUG_FS is not set: ACPI EXTLOG needs debugfs ras/daemon_active."
	local WARNING_MEMORY_HOTPLUG="CONFIG_MEMORY_HOTPLUG is not set: page and row offlining need its sysfs files."
	check_extra_config
}

src_prepare() {
	default
	python_fix_shebang util/ras-mc-ctl.py
}

src_configure() {
	use elibc_musl && append-libs -largp

	# Every feature without a USE flag stays off, including new ones a version
	# bump brings, instead of being built because it has no dependencies.
	local emesonargs=(
		--localstatedir="${EPREFIX}/var"
		-Dauto_features=disabled
		-Denable-arch=auto
		-Dsqlite3=enabled
		-Dmysql=disabled
		-Dpostgresql=disabled
		-Ddocs=disabled
		-Dsystemd-unit-dir="$(systemd_get_systemunitdir)"
		-Dsqlite3-database="${EPREFIX}/var/lib/${PN}/ras-mc_event.db"
		$(meson_feature test tests)
	)
	local feature
	for feature in "${RASDAEMON_DEFAULT_FEATURES[@]}" "${RASDAEMON_OPTIONAL_FEATURES[@]}"; do
		emesonargs+=( $(meson_feature "rasdaemon_features_${feature}" "${feature}") )
	done
	meson_src_configure
}

src_install() {
	meson_src_install
	python_optimize "${ED}/usr/share/${PN}"
	keepdir "/var/lib/${PN}"

	newinitd "${FILESDIR}/rasdaemon.openrc" rasdaemon
	newinitd "${FILESDIR}/ras-mc-ctl.openrc" ras-mc-ctl
	newconfd "${FILESDIR}/rasdaemon.confd" rasdaemon
}

pkg_postinst() {
	elog "ras-mc-ctl 1.0 uses subcommands: dimm --register-labels,"
	elog "database --summary and database --errors. See ras-mc-ctl --help."
	elog "Daemon settings are in /etc/sysconfig/rasdaemon."
	elog "The service mounts tracefs at /sys/kernel/tracing."
	if use rasdaemon_features_extlog; then
		elog "Keep debugfs available for ACPI EXTLOG events (ras/daemon_active)."
	fi
}