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

EAPI=8

CHROMIUM_LANGS="
	af am ar bg bn ca cs da de el en-GB en-US es es-419 et fa fi fil fr gu he
	hi hr hu id it ja kn ko lt lv ml mr ms nb nl pl pt-BR pt-PT ro ru sk sl sr
	sv sw ta te th tr uk ur vi zh-CN zh-TW
"

inherit chromium-2 desktop unpacker xdg

DESCRIPTION="Quantum Resistant Ledger desktop wallet"
HOMEPAGE="https://github.com/theQRL/qrl-wallet"
SRC_URI="https://github.com/theQRL/qrl-wallet/releases/download/v${PV}/QRLWallet-${PV}-linux-x64.deb"
S="${WORKDIR}"

LICENSE="MIT"
# From www-client/chromium for the bundled Electron runtime
LICENSE+=" Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD BSD-2 Base64 Boost-1.0 CC-BY-3.0 CC-BY-4.0 Clear-BSD FFT2D FTL"
LICENSE+=" IJG ISC LGPL-2 LGPL-2.1 MIT MPL-1.1 MPL-2.0 Ms-PL PSF-2 SGI-B-2.0 SSLeay SunSoft Unicode-3.0"
LICENSE+=" Unicode-DFS-2015 Unlicense UoI-NCSA ZLIB libtiff openssl"
SLOT="0"
KEYWORDS="-* ~amd64"

RDEPEND="
	>=app-accessibility/at-spi2-core-2.46.0:2
	dev-libs/expat
	dev-libs/glib:2
	dev-libs/libusb:1
	dev-libs/nspr
	dev-libs/nss
	media-libs/alsa-lib
	media-libs/mesa
	net-print/cups
	sys-apps/dbus
	virtual/libudev
	x11-libs/cairo
	x11-libs/gtk+:3
	x11-libs/libX11
	x11-libs/libxcb
	x11-libs/libXcomposite
	x11-libs/libXdamage
	x11-libs/libXext
	x11-libs/libXfixes
	x11-libs/libxkbcommon
	x11-libs/libXrandr
	x11-libs/pango
	x11-misc/xdg-utils
"

QA_PREBUILT="opt/QRLWallet/*"

pkg_pretend() {
	chromium_suid_sandbox_check_kernel_config
}

src_prepare() {
	default

	pushd opt/QRLWallet/locales >/dev/null || die
	chromium_remove_language_paks
	popd >/dev/null || die

	# drop native node modules for other OSes, arches and musl
	local nm=opt/QRLWallet/resources/app.asar.unpacked/app/programs/server/npm/node_modules
	local prebuilds
	for prebuilds in "${nm}"/{argon2,@ledgerhq/hw-transport-node-hid/node_modules/usb}/prebuilds; do
		find "${prebuilds}" -mindepth 1 -maxdepth 1 ! -name linux-x64 -exec rm -r {} + || die
		rm "${prebuilds}"/linux-x64/*.musl.node || die
	done
}

src_install() {
	domenu usr/share/applications/QRLWallet.desktop
	doicon -s 1024 usr/share/icons/hicolor/1024x1024/apps/QRLWallet.png

	# moved as a whole to keep the exec bits of the bundled node and Electron binaries
	dodir /opt
	mv opt/QRLWallet "${ED}"/opt/ || die

	dosym -r /opt/QRLWallet/QRLWallet /usr/bin/qrl-wallet
}
