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

EAPI=8

inherit desktop systemd tmpfiles unpacker xdg

DESCRIPTION="NetEase UU Remote desktop client"
HOMEPAGE="https://uuyc.163.com/"
SRC_URI="https://a56.gdl.netease.com/nrd-${PN}_${PV}-1_amd64.deb"
S="${WORKDIR}"

# bundled: Qt 5, OpenSSL 1.1, abseil, Crashpad, curl, fmt, fuse3, GCC runtime,
# libjpeg-turbo, xz, minizip, zlib, pipewire, libpng, protobuf, sentry, spdlog,
# libtiff, tmux
LICENSE="
	uuyc
	0BSD Apache-2.0 BSD curl GPL-2 GPL-3 gcc-runtime-library-exception-3.1 IJG ISC
	LGPL-2.1 LGPL-3 libpng2 libtiff MIT openssl ZLIB
"
SLOT="0"
KEYWORDS="-* ~amd64"
RESTRICT="bindist mirror strip"

RDEPEND="
	>=sys-libs/glibc-2.27
	app-arch/zstd
	dev-libs/glib:2
	media-libs/fontconfig
	media-libs/freetype
	media-libs/libglvnd
	media-libs/libpulse
	media-libs/vulkan-loader
	media-video/pipewire[systemd]
	net-libs/webkit-gtk:4.1
	sys-apps/dbus
	sys-apps/systemd
	sys-fs/fuse:3[suid]
	sys-libs/pam
	x11-libs/cairo
	x11-libs/libdrm
	x11-libs/libX11
	x11-libs/libxcb
	x11-libs/libXext
	x11-libs/libXfixes
	x11-libs/libXi
	x11-libs/libxkbcommon[X]
	x11-libs/libXrandr
	x11-libs/libXrender
	x11-libs/libXres
	x11-libs/libXtst
	x11-libs/xcb-util-image
	x11-libs/xcb-util-keysyms
	x11-libs/xcb-util-renderutil
	x11-libs/xcb-util-wm
"
BDEPEND="media-gfx/icoutils"

QA_PREBUILT="opt/uuyc/*"

src_prepare() {
	default
	# the desktop entry points at a Windows .ico
	sed -i 's|^Icon=.*|Icon=uuyc|' usr/share/applications/uuyc.desktop || die
	# the bundled libfuse3 runs this path; use the system setuid helper
	rm opt/uuyc/libexec/fusermount3 || die
}

src_install() {
	insinto /opt
	doins -r opt/uuyc
	fperms 0755 /opt/uuyc/bin/{conpty_bridge,crashpad_handler,StreamerCodecDetector} \
		/opt/uuyc/bin/{Upgrade,uuyc,uuyc-cli,uuyc_healthd,uuyc_server,uuyc_service} \
		/opt/uuyc/bin/{uuyc_webview_host,terminal-launcher/xdg-terminal-exec} \
		/opt/uuyc/libexec/tmux
	dosym -r /usr/bin/fusermount3 /opt/uuyc/libexec/fusermount3

	dobin usr/bin/uuyc usr/bin/uuyc-cli

	exeinto /usr/lib/uuyc
	doexe usr/lib/uuyc/uuyc-gui-session-start
	insinto /usr/lib/uuyc
	doins usr/lib/uuyc/install_config.ini

	systemd_dounit lib/systemd/system/uuyc.{service,socket}
	systemd_douserunit usr/lib/systemd/user/uuyc-gui.service
	newtmpfiles "${FILESDIR}"/uuyc.tmpfiles uuyc.conf

	insinto /usr/share/gnome-shell/extensions
	doins -r usr/share/gnome-shell/extensions/drag-monitor@uuyc.netease.com

	icotool -x -o "${T}" usr/share/icons/hicolor/256x256/apps/uuyc.ico || die
	local size
	for size in 16 24 32 48 64 72 128 256; do
		newicon -s ${size} "${T}"/uuyc_*_${size}x${size}x32.png uuyc.png
	done
	domenu usr/share/applications/uuyc.desktop
}

pkg_postinst() {
	xdg_pkg_postinst
	tmpfiles_process uuyc.conf

	if [[ -z ${REPLACING_VERSIONS} ]]; then
		elog "The client reaches its service through /run/uuyc-control.sock:"
		elog "  systemctl enable --now uuyc.socket"
	fi
}