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

# Autogenerated by pycargoebuild 0.13.2

EAPI=8

CRATES="
"

declare -A GIT_CRATES=(
	[android-wakelock]='https://github.com/rustdesk-org/android-wakelock;d0292e5a367e627c4fa6f1ca6bdfad005dca7d90;android-wakelock-%commit%'
	[arboard]='https://github.com/rustdesk-org/arboard;85be1218668ff218a7b170c9d424fde73e069914;arboard-%commit%'
	[cacao]='https://github.com/clslaid/cacao;05e1536b0b43aaae308ec72c0eed703e875b7b95;cacao-%commit%'
	[cidre-macros]='https://github.com/yury/cidre;f05c4288f9870c9fab53272ddafd6ec01c7b2dbf;cidre-%commit%/cidre-macros'
	[cidre]='https://github.com/yury/cidre;f05c4288f9870c9fab53272ddafd6ec01c7b2dbf;cidre-%commit%/cidre'
	[clipboard-master]='https://github.com/rustdesk-org/clipboard-master;ddc39f00a6211959489ae683aa6ae6eedf03a809;clipboard-master-%commit%'
	[confy]='https://github.com/rustdesk-org/confy;83db9ec19a2f97e9718aef69e4fc5611bb382479;confy-%commit%'
	[core-foundation-sys]='https://github.com/madsmtm/core-foundation-rs;7d593d016175755e492a92ef89edca68ac3bd5cd;core-foundation-rs-%commit%/core-foundation-sys'
	[core-foundation]='https://github.com/madsmtm/core-foundation-rs;7d593d016175755e492a92ef89edca68ac3bd5cd;core-foundation-rs-%commit%/core-foundation'
	[core-graphics-types]='https://github.com/madsmtm/core-foundation-rs;7d593d016175755e492a92ef89edca68ac3bd5cd;core-foundation-rs-%commit%/core-graphics-types'
	[core-graphics]='https://github.com/madsmtm/core-foundation-rs;7d593d016175755e492a92ef89edca68ac3bd5cd;core-foundation-rs-%commit%/core-graphics'
	[cpal]='https://github.com/rustdesk-org/cpal;6b374bcaed076750ca8fce6da518ab39b882e14a;cpal-%commit%'
	[default_net]='https://github.com/rustdesk-org/default_net;78f8f70cd85151a3a2c4a3230d80d5272703c02e;default_net-%commit%'
	[evdev]='https://github.com/rustdesk-org/evdev;cec616e37790293d2cd2aa54a96601ed6b1b35a9;evdev-%commit%'
	[filedescriptor]='https://github.com/rustdesk-org/wezterm;80174f8009f41565f0fa8c66dab90d4f9211ae16;wezterm-%commit%/filedescriptor'
	[hwcodec]='https://github.com/rustdesk-org/hwcodec;398e5a8938dd8768ade0fcdc27ea80e8b4b38738;hwcodec-%commit%'
	[impersonate_system]='https://github.com/rustdesk-org/impersonate-system;2f429010a5a10b1fe5eceb553c6672fd53d20167;impersonate-system-%commit%'
	[kcp-sys]='https://github.com/rustdesk-org/kcp-sys;32a6c09fc6223f54aea83981a6aa8995931d29be;kcp-sys-%commit%'
	[keepawake]='https://github.com/rustdesk-org/keepawake-rs;64d568586dd16551d02120e19668d2b0fec8e3c9;keepawake-rs-%commit%'
	[machine-uid]='https://github.com/rustdesk-org/machine-uid;381ff579c1dc3a6c54db9dfec47c44bcb0246542;machine-uid-%commit%'
	[magnum-opus]='https://github.com/rustdesk-org/magnum-opus;5cd2bf989c148662fa3a2d9d539a71d71fd1d256;magnum-opus-%commit%'
	[nokhwa-bindings-linux]='https://github.com/rustdesk-org/nokhwa;c2f74662b6ce117f7f94301693fdfadc0b1ec91a;nokhwa-%commit%/nokhwa-bindings-linux'
	[nokhwa-bindings-macos]='https://github.com/rustdesk-org/nokhwa;c2f74662b6ce117f7f94301693fdfadc0b1ec91a;nokhwa-%commit%/nokhwa-bindings-macos'
	[nokhwa-bindings-windows]='https://github.com/rustdesk-org/nokhwa;c2f74662b6ce117f7f94301693fdfadc0b1ec91a;nokhwa-%commit%/nokhwa-bindings-windows'
	[nokhwa-core]='https://github.com/rustdesk-org/nokhwa;c2f74662b6ce117f7f94301693fdfadc0b1ec91a;nokhwa-%commit%/nokhwa-core'
	[nokhwa]='https://github.com/rustdesk-org/nokhwa;c2f74662b6ce117f7f94301693fdfadc0b1ec91a;nokhwa-%commit%'
	[pam-sys]='https://github.com/rustdesk-org/pam-sys;3337c9bb9a9c68d7497ec8c93cad2368c26091b7;pam-sys-%commit%'
	[pam]='https://github.com/rustdesk-org/pam;7bfd25510202cd269292cbdd7c71f3977a6fd762;pam-%commit%'
	[parity-tokio-ipc]='https://github.com/rustdesk-org/parity-tokio-ipc;d0ae39bffe5d5a3e8d82a1b6bcb1ca5a9b2f1c01;parity-tokio-ipc-%commit%'
	[portable-pty]='https://github.com/rustdesk-org/wezterm;80174f8009f41565f0fa8c66dab90d4f9211ae16;wezterm-%commit%/pty'
	[rdev]='https://github.com/rustdesk-org/rdev;f9b60b1dd0f3300a1b797d7a74c116683cd232c8;rdev-%commit%'
	[rust-pulsectl]='https://github.com/rustdesk-org/pulsectl;aa34dde499aa912a3abc5289cc0b547bd07dd6e2;pulsectl-%commit%'
	[sciter-rs]='https://github.com/rustdesk-org/rust-sciter;5322f3a755a0e6bf999fbc60d1efc35246c0f821;rust-sciter-%commit%'
	[sysinfo]='https://github.com/rustdesk-org/sysinfo;90b1705d909a4902dbbbdea37ee64db17841077d;sysinfo-%commit%'
	[tao-macros]='https://github.com/rustdesk-org/tao;288c219cb0527e509590c2b2d8e7072aa9feb2d3;tao-%commit%/tao-macros'
	[tao]='https://github.com/rustdesk-org/tao;288c219cb0527e509590c2b2d8e7072aa9feb2d3;tao-%commit%'
	[tfc]='https://github.com/rustdesk-org/The-Fat-Controller;78bb80a8e596e4c14ae57c8448f5fca75f91f2b0;The-Fat-Controller-%commit%'
	[tokio-socks]='https://github.com/rustdesk-org/tokio-socks;bdb9aa3de5bac41602d0742b8ef6bbc6bfebd127;tokio-socks-%commit%'
	[tray-icon]='https://github.com/tauri-apps/tray-icon;0a5835b0e6828e37a1f781de9c2d671ae7a939e6;tray-icon-%commit%'
	[wallpaper]='https://github.com/rustdesk-org/wallpaper.rs;ce4a0cd3f58327c7cc44d15a63706fb0c022bacf;wallpaper.rs-%commit%'
	[webm-sys]='https://github.com/rustdesk-org/rust-webm;d2c4d3ac133c7b0e4c0f656da710b48391981e64;rust-webm-%commit%/src/sys'
	[webm]='https://github.com/rustdesk-org/rust-webm;d2c4d3ac133c7b0e4c0f656da710b48391981e64;rust-webm-%commit%'
	[x11-clipboard]='https://github.com/clslaid/x11-clipboard;5fc2e73bc01ada3681159b34cf3ea8f0d14cd904;x11-clipboard-%commit%'
	[x11]='https://github.com/bjornsnoen/x11-rs;c2e9bfaa7b196938f8700245564d8ac5d447786a;x11-rs-%commit%/x11'
)

# LLVM 22 IS DELIBERATELY ABSENT. Tested 2026-08-08 -- it does not work, and the
# blocker is upstream, not packaging. Adding 22 here and building with
# USE="llvm_slot_22" gets 64 errors in `scrap`:
#
#   error[E0609]: no field `g_w` on type `vpx_codec_enc_cfg`
#       = note: available fields are: `_address`
#   error[E0609]: no field `g_w` on type `common::aom::aom_codec_enc_cfg`
#       = note: available field is: `_address`
#   error: could not compile `scrap` (lib) due to 64 previous errors
#
# `_address` is the ONLY field bindgen emits for a type it gave up parsing, so
# those bindings came back opaque: libs/scrap asks for bindgen 0.65 (0.65.1,
# May 2023), and that bindgen cannot parse the libvpx/libaom headers through
# libclang 22. Nothing in rustdesk's own code is at fault, and no ebuild change
# fixes it -- scrap has to move to a current bindgen upstream first.
#
# Caveat on that measurement: RUST_NEEDS_LLVM=1 means the slot choice also
# picks the Rust, so the two builds differed in both (llvm 21 -> rust-bin
# 1.94.0, PASS; llvm 22 -> rust 1.97.1, FAIL). The variable is not isolable
# without rebuilding Rust. It does not change the conclusion: `_address` is
# text bindgen WRITES at build-script time using libclang, and rustc only
# reports the missing fields afterwards -- the failing layer is bindgen/
# libclang, and only LLVM moved there.
#
# Consequence for a host that pins LLVM_SLOT=22 in make.conf: this package
# needs `net-misc/rustdesk llvm_slot_21` in package.use, and that is not a
# temporary workaround. Re-test on the next bump; the fix arrives with scrap.
LLVM_COMPAT=( 18 19 20 21 )
RUST_MIN_VER="1.81.0"
RUST_NEEDS_LLVM=1
inherit cargo desktop llvm-r1 systemd xdg

DESCRIPTION="An open-source remote desktop, and alternative to TeamViewer"
HOMEPAGE="https://rustdesk.com/"
# fix:
# 1. rust-webm-*/src/sys/libwebm is a empty directory
# 2. gcc15 build: https://github.com/microcai/gentoo-zh/issues/7234
_LIBWEBM_COMMIT="3b630045052e1e4d563207ab9e3be8d137c26067"
# grep -i vcpkg .github/workflows/flutter-build.yml
_VCPKG_TAG="2025.08.27"
# last rustdesk-vcpkg release available for this vcpkg tag
_VCPKG_PV="1.4.7"
# fix: hwcodec-${_HWCODEC_COMMIT}"/externals is a empty directory
# git clone https://github.com/rustdesk-org/hwcodec
# git ls-tree HEAD externals
_HWCODEC_EXTERNALS_COMMIT="8903740a1f47884906a6e347ad3d8d56304d9771"
# fix: libs/hbb_common is a empty directory
# It is a git submodule, and GitHub's tag tarball ships submodule directories
# EMPTY, so the commit has to be pinned here by hand -- which means it goes
# stale silently on a bump. It did: until 2026-08-08 this pinned 48c37de3, a
# commit that matches NO release (1.4.7 wants df6badca, 1.4.8 387603f4), and
# `cargo build` died with 58 errors of the form "no field/method X on
# hbb_common::...". The package had never been buildable at 1.4.9; the broken
# crates Manifest above hid it, because nothing got past the fetch.
# ALWAYS re-derive this against the tag, never carry it over:
#   git ls-tree ${PV} libs/hbb_common
#   curl -s "https://api.github.com/repos/rustdesk/rustdesk/contents/libs/hbb_common?ref=${PV}" | jq -r .sha
_HBB_COMMON_COMMIT="7e1c392c62d39c364127307cd408421dd5f8cfb0"
# fix: kcp-sys-*/kcp is a empty directory
# git clone https://github.com/rustdesk-org/kcp-sys
# git ls-tree HEAD kcp
_KCP_COMMIT="7f9805887b0909c52c825925f123e7a84da37167"
# THE CRATES BUNDLE, AND HOW TO READ A CHECKSUM MISMATCH ON IT
#
# CRATES= is empty: every crates.io dependency -- 1003 of them -- arrives inside
# ${P}-crates.tar.xz from gentoo-zh-drafts, a third-party aggregation repo.
# Only the 44 git dependencies go through GIT_CRATES.
#
# 2026-08-08: that DIST entry was re-pinned from 141240432 to 141218012 bytes.
# Do NOT read a mismatch here as "upstream silently regenerated the bundle" --
# that reading cost this package a month of being unbuildable. The asset has
# never been replaced (GitHub API: created=2026-07-11T13:03:58Z,
# updated=…:04Z, six seconds apart). What happened is that this ebuild landed
# on 2026-07-06, FIVE DAYS BEFORE the asset was published, so `pkgdev manifest`
# hit a 404 and hashed a homonymous leftover in DISTDIR instead. The Manifest
# was fiction; upstream was right.
#   Diagnostic, before blaming upstream on the next bump -- compare the date of
#   the commit that wrote the DIST line against the asset's created_at:
#     git log --format='%h %ai' -S'<distfile>' -- net-misc/rustdesk/Manifest
#     curl -s https://api.github.com/repos/gentoo-zh-drafts/rustdesk/releases/tags/${PV} \
#       | jq -r '.assets[] | "\(.name) \(.size) \(.created_at) \(.updated_at)"'
#   Commit predates created_at => fabricated DIST, not a replaced asset.
#
# The re-pin was verified, not trusted. The bundle is a `cargo vendor` tree, so
# every crate carries .cargo-checksum.json whose .package is the sha256 of its
# original .crate -- the same value Cargo.lock pins in its `checksum` field, and
# Cargo.lock comes from ${P}.tar.gz, whose Manifest entry was never broken.
# Result: 1003/1003 match, 0 missing, 0 mismatches, plus one extra
# (bindgen-0.72.1) that Cargo.lock never references and cargo cannot select.
# Not proven: that the extracted files correspond to the original .crate, since
# the bundle ships trees rather than .crate archives.
#
# That 1003/1003 also means the bundle is nothing but unmodified crates.io
# content, i.e. it buys nothing crates.io does not -- it only adds failure modes
# (404, republish, the fabricated DIST above). Replacing it with a real CRATES=
# block generated by pycargoebuild would be mechanical, and is the durable fix.
SRC_URI="
	https://github.com/rustdesk/rustdesk/archive/refs/tags/${PV}.tar.gz
		-> ${P}.tar.gz
	https://github.com/gentoo-zh-drafts/rustdesk-vcpkg/releases/download/${_VCPKG_PV}/${PN}-${_VCPKG_PV}-vcpkg-${_VCPKG_TAG}-lite.tar.gz
		-> ${PN}-vcpkg-${_VCPKG_TAG}-lite.tar.gz
	https://github.com/webmproject/libwebm/archive/${_LIBWEBM_COMMIT}.tar.gz
		-> libwebm-${_LIBWEBM_COMMIT}.tar.gz
	https://github.com/skywind3000/kcp/archive/${_KCP_COMMIT}.tar.gz
		-> kcp-${_KCP_COMMIT}.tar.gz
	https://github.com/rustdesk/hbb_common/archive/${_HBB_COMMON_COMMIT}.tar.gz
		-> hbb_common-${_HBB_COMMON_COMMIT}.tar.gz
	https://github.com/rustdesk-org/externals/archive/${_HWCODEC_EXTERNALS_COMMIT}.tar.gz
		-> hwcodec-externals-${_HWCODEC_EXTERNALS_COMMIT}.tar.gz
	https://raw.githubusercontent.com/c-smile/sciter-sdk/master/bin.lnx/x64/libsciter-gtk.so
		-> ${PN}-libsciter-gtk.so
	https://github.com/gentoo-zh-drafts/${PN}/releases/download/${PV}/${P}-crates.tar.xz
	${CARGO_CRATE_URIS}
"

LICENSE="AGPL-3"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD-2 BSD Boost-1.0
	CC0-1.0 CDLA-Permissive-2.0 GPL-3+ IJG ISC MIT MIT-0 MPL-2.0
	Unicode-DFS-2016 Unlicense WTFPL-2 ZLIB
"
SLOT="0"
KEYWORDS="~amd64"

# `systemd` gates the unit ONLY. The OpenRC init script is installed
# unconditionally -- see src_install.
IUSE="wayland +hwaccel systemd"

RDEPEND="
	media-libs/alsa-lib
	x11-libs/gtk+:3
	x11-libs/libxcb
	x11-libs/libXfixes
	media-libs/libpulse
	x11-misc/xdotool
	media-libs/libva[X]
	wayland? ( media-video/pipewire[gstreamer] )
	hwaccel? ( x11-libs/libvdpau )
"
BDEPEND="
	dev-lang/nasm
	dev-lang/yasm
	media-libs/alsa-lib
	media-libs/libpulse
	dev-build/cmake
	dev-build/ninja
	media-libs/gstreamer
	media-libs/gst-plugins-base
	$(llvm_gen_dep '
		llvm-core/clang:${LLVM_SLOT}
		llvm-core/llvm:${LLVM_SLOT}
	')
"

QA_PRESTRIPPED="
	/usr/share/${PN}/${PN}
	/usr/share/${PN}/libsciter-gtk.so
"

pkg_setup() {
	llvm-r1_pkg_setup
	rust_pkg_setup
}

src_prepare() {
	default
	cd "${S}"/.. || die
	eapply "${FILESDIR}"/rust-sciter.patch

	rm -rf "${S}"/libs/hbb_common || die
	ln -s "${WORKDIR}/hbb_common-${_HBB_COMMON_COMMIT}" "${S}"/libs/hbb_common || die

	local _RUSTWEBM_COMMIT=`echo "${GIT_CRATES[webm]}" | awk -F';' '{print $2}'`
	rm -rf "${WORKDIR}/rust-webm-${_RUSTWEBM_COMMIT}"/src/sys/libwebm || die
	ln -s "${WORKDIR}/libwebm-${_LIBWEBM_COMMIT}" "${WORKDIR}/rust-webm-${_RUSTWEBM_COMMIT}"/src/sys/libwebm || die

	local _HWCODEC_COMMIT=`echo "${GIT_CRATES[hwcodec]}" | awk -F';' '{print $2}'`
	rm -rf "${WORKDIR}/hwcodec-${_HWCODEC_COMMIT}"/externals || die
	ln -s "${WORKDIR}/externals-${_HWCODEC_EXTERNALS_COMMIT}" "${WORKDIR}/hwcodec-${_HWCODEC_COMMIT}"/externals || die

	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() {
	if use hwaccel ;then
		local myfeatures=(hwcodec)
	fi

	cargo_src_configure
}

src_compile() {
	VCPKG_ROOT="$WORKDIR"/vcpkg cargo_src_compile
}

src_install() {
	local rustdesk_dir="/usr/share/${PN}"

	exeinto "${rustdesk_dir}"
	insinto "${rustdesk_dir}"
	doexe $(cargo_target_dir)/rustdesk
	newins "${DISTDIR}/${PN}-libsciter-gtk.so" libsciter-gtk.so
	rm src/ui/*.rs || die
	newbin "${FILESDIR}/rustdesk.sh" rustdesk
	insinto "${rustdesk_dir}/src"
	doins -r src/ui

	newicon -s 32 res/32x32.png rustdesk.png
	newicon -s 128 res/128x128.png rustdesk.png
	newicon -s 256 res/128x128@2x.png rustdesk.png

	domenu "${FILESDIR}"/rustdesk{,-link}.desktop

	# OpenRC service, installed unconditionally: it costs a systemd user
	# nothing, while gating it would leave an OpenRC user with no way to run
	# the daemon this package exists to provide. The file is byte-identical
	# to net-misc/rustdesk-bin/files/rustdesk.initd -- FILESDIR is
	# per-package, so it has to be duplicated rather than shared.
	newinitd "${FILESDIR}"/rustdesk.initd rustdesk

	if use systemd; then
		systemd_dounit "${FILESDIR}"/rustdesk.service
	fi

	einstalldocs
}