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

EAPI=8

# The vendored dependency set includes crates (e.g. iroh, used only by the
# not-yet-packaged cluster plugin) whose MSRV is 1.91.
RUST_MIN_VER="1.91"

inherit cargo shell-completion systemd

DESCRIPTION="Scan logs and take action, a faster successor to fail2ban"
HOMEPAGE="https://reaction.ppom.me https://framagit.org/ppom/reaction"
SRC_URI="
	https://framagit.org/ppom/reaction/-/archive/v${PV}/reaction-v${PV}.tar.gz -> ${P}.tar.gz
	https://cafarelli.fr/gentoo/${P}-crates.tar.xz
"
S="${WORKDIR}/${PN}-v${PV}"

LICENSE="AGPL-3"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD-2 BSD
	CDLA-Permissive-2.0 GPL-2 ISC MIT Unicode-3.0 Unlicense ZLIB
"
SLOT="0"
KEYWORDS="~amd64"
IUSE="munin"

# reaction shells out to the firewall tools selected in its configuration
# (iptables, nftables, ipset, ...), so there is no hard runtime dependency.
# The munin plugin needs munin-node and jq at runtime.
RDEPEND="munin? ( net-analyzer/munin app-misc/jq )"

# The test suite needs network access and live firewall privileges,
# neither of which is available in the sandbox.
RESTRICT="test"

src_configure() {
	# Build only the core daemon. The optional plugins (nftables, ipset,
	# cluster, virtual) are not packaged yet.
	cargo_src_configure --bin reaction
}

src_install() {
	cargo_src_install

	local tdir
	tdir=$(cargo_target_dir)

	# Man pages and shell completions are generated by build.rs, but only
	# during a release build.
	if [[ -f ${tdir}/reaction.1 ]]; then
		doman "${tdir}"/reaction*.1
	fi
	if [[ -f ${tdir}/reaction.bash ]]; then
		newbashcomp "${tdir}"/reaction.bash reaction
	fi
	if [[ -f ${tdir}/reaction.fish ]]; then
		dofishcomp "${tdir}"/reaction.fish
	fi
	if [[ -f ${tdir}/_reaction ]]; then
		dozshcomp "${tdir}"/_reaction
	fi

	newinitd "${FILESDIR}"/reaction.initd reaction
	newconfd "${FILESDIR}"/reaction.confd reaction

	insinto /etc/logrotate.d
	newins "${FILESDIR}"/reaction.logrotated reaction

	systemd_dounit packaging/reaction.service
	systemd_dounit packaging/system-reaction.slice

	if use munin; then
		exeinto /usr/libexec/munin/plugins
		newexe "${FILESDIR}"/reaction.munin reaction
		insinto /etc/munin/plugin-conf.d
		newins "${FILESDIR}"/reaction.munin-conf reaction
	fi

	keepdir /etc/reaction
	keepdir /var/lib/reaction

	dodoc README.md
	docinto examples
	dodoc config/example.yml config/example.jsonnet
}