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

# Autogenerated by pycargoebuild 0.16.0

EAPI=8

CRATES="
"

declare -A GIT_CRATES=(
	[http_req]='https://github.com/EasyTier/http_req;b10aa9fc0db3067cc3d2174683a87250b80a1ea9;http_req-%commit%'
	[kcp-sys]='https://github.com/EasyTier/kcp-sys;94964794caaed5d388463137da59b97499619e5f;kcp-sys-%commit%'
	[service-manager]='https://github.com/EasyTier/service-manager-rs;5eb28f7a686858eea4f4933534ed989d3b71dc2a;service-manager-rs-%commit%'
	[smoltcp]='https://github.com/smoltcp-rs/smoltcp;0a926767a68bc88d5512afefa7529c5ecdade4ea;smoltcp-%commit%'
	[thunk-rs]='https://github.com/easytier/thunk;cbbeec75a66b7b3cf0824ae890d9d06bcfb9d1f3;thunk-%commit%/thunk-rs'
	[tun-easytier]='https://github.com/EasyTier/rust-tun;12378839e7985283df0e4fb536b7137230356db5;rust-tun-%commit%'
	[windivert-sys]='https://github.com/EasyTier/windivert-rust;adcc56d1550f7b5377ec2b3429f413ee24a77375;windivert-rust-%commit%/windivert-sys'
	[windivert]='https://github.com/EasyTier/windivert-rust;adcc56d1550f7b5377ec2b3429f413ee24a77375;windivert-rust-%commit%/windivert'
)

RUST_MIN_VER="1.95"

inherit cargo systemd

DESCRIPTION="A full meshed p2p VPN"
HOMEPAGE="https://github.com/EasyTier/EasyTier"
SRC_URI="
	https://github.com/EasyTier/EasyTier/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	https://github.com/gentoo-zh-drafts/EasyTier/releases/download/v${PV}/EasyTier-${PV}-crates.tar.xz
	${CARGO_CRATE_URIS}
"
# FIX: missing kcp-sys-*/kcp
# curl https://api.github.com/repos/EasyTier/kcp-sys/contents/kcp | jq -r '.sha'
_KCP_COMMIT="32da082e529a26730aea3eb19922f80634ee6dea"
SRC_URI+=" https://github.com/skywind3000/kcp/archive/${_KCP_COMMIT}.tar.gz -> kcp-${_KCP_COMMIT}.tar.gz"

S="${WORKDIR}/EasyTier-${PV}"
LICENSE="LGPL-3"
# Dependent crate licenses
LICENSE+="
	0BSD Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD-2 BSD Boost-1.0
	CC0-1.0 CDLA-Permissive-2.0 ISC LGPL-3+ MIT MPL-2.0 Unicode-3.0
	WTFPL-2 ZLIB
"
SLOT="0"
KEYWORDS="~amd64"
BDEPEND="
	dev-libs/protobuf
	sys-devel/mold
"
RESTRICT="test" # fails with network-sandbox

src_prepare() {
	default
	# fix: kcp-sys-*/kcp is a empty directory
	local _KCPSYS_COMMIT=`echo "${GIT_CRATES[kcp-sys]}" | awk -F';' '{print $2}'`
	rm -rf "${WORKDIR}/kcp-sys-${_KCPSYS_COMMIT}"/kcp || die
	ln -s "${WORKDIR}/kcp-${_KCP_COMMIT}" "${WORKDIR}/kcp-sys-${_KCPSYS_COMMIT}"/kcp || die
}

src_configure() {
	cargo_src_configure --package=easytier
}

src_install() {
	keepdir /etc/easytier
	insinto /etc/easytier
	doins "${FILESDIR}/default.conf"

	systemd_newunit "${FILESDIR}/easytier_at.service" easytier@.service

	dobin "$(cargo_target_dir)/easytier-cli"
	dobin "$(cargo_target_dir)/easytier-core"

	einstalldocs
}