# Copyright 2026 ReK2 - Chris F.N. <rek2@hispagatos.org>
# https://rek2.hispagatos.org
# Distributed under the terms of the GNU General Public License v3

EAPI=8

RUST_MIN_VER="1.96.0"
CRATES=""

inherit cargo git-r3 systemd

DESCRIPTION="Rust implementation of the Reticulum Network Stack"
HOMEPAGE="https://github.com/lelloman/rns-rs"
EGIT_REPO_URI="https://github.com/lelloman/rns-rs.git"

LICENSE="Reticulum"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD BSD-2 CC0-1.0 ISC
	LGPL-2.1+ MIT Unicode-3.0 Unlicense ZLIB
"
SLOT="0"
KEYWORDS=""
IUSE="systemd"

RDEPEND="
	!dev-python/rns
	acct-group/reticulum
	acct-user/reticulum
"

QA_FLAGS_IGNORED="usr/bin/*"

src_unpack() {
	git-r3_src_unpack
	cargo_live_src_unpack
}

src_install() {
	local bin
	for bin in git-remote-rns rncp rngcs rngit rnid rnpath rnprobe \
		rns-ctl rns-server rnsd rnsh rnstatus rntun rnx; do
		dobin "$(cargo_target_dir)/${bin}" || die
	done

	newconfd "${FILESDIR}"/rnsd.confd rnsd
	newinitd "${FILESDIR}"/rnsd.initd rnsd
	if use systemd; then
		systemd_dounit "${FILESDIR}"/rnsd.service
		systemd_douserunit "${FILESDIR}"/user/rnsd.service
	fi

	insinto /etc/reticulum
	doins "${FILESDIR}"/config
	fowners reticulum:reticulum /etc/reticulum /etc/reticulum/config

	einstalldocs
}

pkg_postinst() {
	if use systemd && [[ -d /run/systemd/system ]]; then
		ebegin "Reloading the systemd manager configuration"
		systemctl daemon-reload
		eend $?
	fi

	elog "rnsd can run in two ways - pick one, do not enable both:"
	elog ""
	elog "Per-user (recommended):"
	elog "  cp /etc/reticulum/config ~/.reticulum/config   # mkdir -p ~/.reticulum first"
	elog "  systemctl --user daemon-reload"
	elog "  systemctl --user enable --now rnsd"
	elog "  runs as you, config in ~/.reticulum, no root needed"
	elog ""
	elog "System-wide (headless/always-on/IoT nodes):"
	elog "  systemctl enable --now rnsd"
	elog "  runs as the reticulum user, config in /etc/reticulum/config"
	elog ""
	elog "OpenRC systems: rc-update add rnsd default"
	elog ""
	elog "Unit files: /usr/lib/systemd/system/rnsd.service (system)"
	elog "            /usr/lib/systemd/user/rnsd.service (user)"
}