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

EAPI=8

PYTHON_COMPAT=( python3_{11..14} )

CRATES="
        anstream@0.6.18
        anstyle-parse@0.2.6
        anstyle-query@1.1.2
        anstyle-wincon@3.0.6
        anstyle@1.0.10
        ash@0.38.0+1.3.281
        autocfg@1.4.0
        bitflags@2.6.0
        cfg-if@1.0.0
        cfg_aliases@0.2.1
        clap@4.5.21
        clap_builder@4.5.21
        clap_lex@0.7.3
        colorchoice@1.0.3
        getrandom@0.2.15
        is_terminal_polyfill@1.70.1
        libc@0.2.177
        libloading@0.8.5
        log@0.4.22
        memoffset@0.9.1
        nix@0.30.1
        pkg-config@0.3.31
        strsim@0.11.1
        utf8parse@0.2.2
        wasi@0.11.0+wasi-snapshot-preview1
        windows-sys@0.59.0
        windows-targets@0.52.6
        windows_aarch64_gnullvm@0.52.6
        windows_aarch64_msvc@0.52.6
        windows_i686_gnu@0.52.6
        windows_i686_gnullvm@0.52.6
        windows_i686_msvc@0.52.6
        windows_x86_64_gnu@0.52.6
        windows_x86_64_gnullvm@0.52.6
        windows_x86_64_msvc@0.52.6
"

inherit cargo meson python-any-r1

DESCRIPTION="Transparent network proxy for Wayland compositors"
HOMEPAGE="https://gitlab.freedesktop.org/mstoeckl/waypipe"

if [[ ${PV} == 9999 ]]; then
	inherit git-r3
	EGIT_REPO_URI="https://gitlab.freedesktop.org/mstoeckl/waypipe"
else
	SRC_URI="https://gitlab.freedesktop.org/mstoeckl/waypipe/-/archive/v${PV}/${PN}-v${PV}.tar.bz2"
	S="${WORKDIR}"/${PN}-v${PV}
	KEYWORDS="amd64 ~arm arm64 ~x86"
fi

LICENSE="MIT"
SLOT="0"

WAYPIPE_FLAG_MAP_X86=( avx2:with_avx2 avx512f:with_avx512f sse3:with_sse3 )
WAYPIPE_FLAG_MAP_ARM=( neon:with_neon_opts )
WAYPIPE_FLAG_MAP=(
	"${WAYPIPE_FLAG_MAP_X86[@]/#/cpu_flags_x86_}"
	"${WAYPIPE_FLAG_MAP_ARM[@]/#/cpu_flags_arm_}"
)

IUSE="dmabuf ffmpeg lz4 systemtap test vaapi zstd ${WAYPIPE_FLAG_MAP[@]%:*}"
REQUIRED_USE="vaapi? ( ffmpeg )"
RESTRICT="!test? ( test )"

DEPEND="
	dmabuf? (
		media-libs/mesa[gbm(+),vaapi?,wayland]
		x11-libs/libdrm
	)
	lz4? ( app-arch/lz4 )
	systemtap? ( dev-debug/systemtap )
	vaapi? ( media-libs/libva[drm(+),wayland] )
	ffmpeg? ( media-video/ffmpeg:=[x264,vaapi?] )
	zstd? ( app-arch/zstd )
"
RDEPEND="${DEPEND}"
BDEPEND="
	${PYTHON_DEPS}
	app-text/scdoc
	virtual/pkgconfig
	test? ( dev-libs/weston[examples,headless,remoting,screen-sharing,wayland-compositor] )
"
src_unpack() {
	rust_pkg_setup
	git-r3_src_unpack
	$cargo_src_unpack
	cargo_live_src_unpack
}


src_configure() {
	local emesonargs=(
		-Dman-pages=enabled
		$(meson_use systemtap with_systemtap)
		$(meson_feature dmabuf with_dmabuf)
		$(meson_feature ffmpeg with_video)
		$(meson_feature lz4 with_lz4)
		$(meson_feature vaapi with_vaapi)
		$(meson_feature zstd with_zstd)
	)
	local fl
	for fl in "${WAYPIPE_FLAG_MAP[@]}"; do
		emesonargs+=( $(meson_use "${fl%:*}" "${fl#*:}") )
	done
	cargo_src_configure
	meson_src_configure
}