# Copyright 2020-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# Auto-Generated by cargo-ebuild 0.2.0

EAPI=8

RUST_MIN_VER="1.71.1" # actually 1.70

CRATES=" "

inherit cargo

MY_PV=${PV/_/-}

DESCRIPTION="An RPKI relying party software"
HOMEPAGE="https://github.com/NLnetLabs/routinator"
MY_GITHUB_AUTHOR="NLnetLabs"
SRC_URI="
	https://github.com/${MY_GITHUB_AUTHOR}/${PN}/archive/refs/tags/v${MY_PV}.tar.gz -> ${P}.tar.gz
	https://herbizarre.swordarmor.fr/garbage/${P}-vendor.tar.xz
"
S="${WORKDIR}/${PN}-${MY_PV}"
LICENSE="BSD"
SLOT="0"
KEYWORDS="~amd64"
IUSE="aspa rta"

DEPEND="
	acct-group/routinator
	acct-user/routinator
"
RDEPEND="${DEPEND}"

src_unpack() {
	default
	cargo_gen_config
	ln -s "${WORKDIR}/vendor/" "${WORKDIR}/${P}/vendor" || die
	sed -i "${ECARGO_HOME}/config.toml" -e '/source.crates-io/d'  || die
	sed -i "${ECARGO_HOME}/config.toml" -e '/replace-with = "gentoo"/d'  || die
	sed -i "${ECARGO_HOME}/config.toml" -e '/local-registry = "\/nonexistent"/d'  || die
	cat "${WORKDIR}/vendor/vendor-config.toml" >> "${ECARGO_HOME}/config.toml" || die
}

src_configure() {
	local myfeatures=(
		$(usev aspa)
		$(usev rta)
	)

	cargo_src_configure
}

src_install() {
	newinitd "${FILESDIR}/${PN}-initd" ${PN}

	cargo_src_install

	doman doc/routinator.1

	insinto /etc
	doins "${FILESDIR}/${PN}.conf"
}

pkg_postinst() {
	einfo ""
	einfo "ARIN TAL is disabled by default because the ARIN Relying Party"
	einfo "Agreement must be accepted beforehead. Read"
	einfo "https://www.arin.net/resources/manage/rpki/rpa.pdf and if you agree"
	einfo "with it, run"
	einfo ""
	einfo "  su -s /bin/sh -c '${EROOT}/usr/bin/routinator --base-dir=/var/lib/routinator init -f --accept-arin-rpa' routinator"
	einfo ""
	einfo "as root to enable it."
	einfo "If you changed values in /etc/conf.d/routinator please update it"
	einfo "accordinadly in the command line."
	einfo ""

	# https://github.com/NLnetLabs/routinator/pull/510
	# https://github.com/NLnetLabs/routinator/pull/543
	if [ "${PV}" = "0.9.0" ]; then
		su -l routinator -s /bin/sh -c \
			"${EROOT}/usr/bin/routinator --base-dir=/var/lib/routinator init"
	fi
}