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

# Autogenerated by pycargoebuild 0.15.0

EAPI=8

# vendored openssl-0.10.57 and openssl-sys-0.9.93 don't support openssl-4
CRATES="
	openssl@0.10.82
	openssl-sys@0.9.118
"

inherit cargo systemd

DESCRIPTION="A reverse proxy for NAT traversal"
HOMEPAGE="https://github.com/rathole-org/rathole"
SRC_URI="
	https://codeload.github.com/rathole-org/rathole/tar.gz/v${PV} -> ${P}.tar.gz
	https://minio.dream-universe.org/posts/distfiles/${P}-crates.tar.xz
	${CARGO_CRATE_URIS}
"

LICENSE="Apache-2.0"
# Dependent crate licenses
LICENSE+="
	0BSD Apache-2.0 BSD ISC MIT Unicode-DFS-2016
	|| ( Artistic-2 CC0-1.0 )
"
SLOT="0"
KEYWORDS="~amd64 ~arm64"

DEPEND="dev-libs/openssl:="
RDEPEND="${DEPEND}"
BDEPEND="virtual/pkgconfig"

PATCHES=("${FILESDIR}/${P}-fix-rust-1.80.patch")

src_prepare() {
	default

	local crate sum
	for crate in openssl-0.10.82 openssl-sys-0.9.118; do
		sum=$(sha256sum "${DISTDIR}"/${crate}.crate | cut -d' ' -f1)
		sed -i -e "/^name = \"${crate%-*}\"$/,/^checksum/{" \
			-e "s/^version = .*/version = \"${crate##*-}\"/" \
			-e "s/^checksum = .*/checksum = \"${sum}\"/" \
			-e "}" Cargo.lock || die
	done
	# openssl-0.10.82 no longer depends on once_cell
	sed -i -e '/^name = "openssl"$/,/^]/{/"once_cell",/d}' Cargo.lock || die
}

src_install(){
	keepdir /etc/rathole
	dobin $(cargo_target_dir)/rathole
	newinitd "${FILESDIR}/ratholes.initd" ratholes
	newinitd "${FILESDIR}/ratholec.initd" ratholec

	systemd_dounit examples/systemd/ratholes@.service
	systemd_dounit examples/systemd/ratholes.service
	systemd_dounit examples/systemd/ratholec@.service
	systemd_dounit examples/systemd/ratholec.service
	systemd_dounit examples/systemd/rathole@.service
}