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

EAPI=8

inherit desktop xdg

MY_PN="${PN%-bin}"
GH_URI="https://github.com/turinglambdaai/${MY_PN}"

DESCRIPTION="Desktop interface for Resilio Sync (GTK4 and WebKitGTK)"
HOMEPAGE="https://github.com/turinglambdaai/syncpilot"
# The tag archive holds the complete corresponding source of the prebuilt
# release, which the AGPL requires us to make available.
SRC_URI="
	amd64? ( ${GH_URI}/releases/download/v${PV}/${MY_PN}-${PV}-linux-x64.tar.gz )
	arm64? ( ${GH_URI}/releases/download/v${PV}/${MY_PN}-${PV}-linux-arm64.tar.gz )
	${GH_URI}/archive/refs/tags/v${PV}.tar.gz -> ${P}-src.tar.gz
	${GH_URI}/raw/v${PV}/assets/icon-512.png -> ${P}-icon-512.png
"

S="${WORKDIR}"

# Rivet and the crypto library are MIT and Apache-2.0, Racket CS is
# Apache-2.0 or LGPL-3, SyncPilot itself is AGPL-3.
LICENSE="AGPL-3 MIT Apache-2.0 || ( Apache-2.0 LGPL-3 )"
SLOT="0"
KEYWORDS="-* ~amd64 ~arm64"
RESTRICT="mirror strip"

RDEPEND="
	app-arch/lz4
	dev-libs/glib:2
	dev-libs/json-glib
	gui-libs/gtk:4
	net-libs/webkit-gtk:6
	net-p2p/resilio-sync
	sys-libs/ncurses[tinfo]
	virtual/zlib:=
	>=sys-libs/glibc-2.38
"

QA_PREBUILT="opt/syncpilot/*"

src_unpack() {
	local arch
	use amd64 && arch=x64
	use arm64 && arch=arm64
	[[ -n ${arch} ]] || die "Unsupported architecture"

	unpack "${MY_PN}-${PV}-linux-${arch}.tar.gz" "${P}-src.tar.gz"
	mv "${MY_PN}-linux-${arch}" "${MY_PN}" || die
}

src_install() {
	dodir /opt/syncpilot
	cp -a "${MY_PN}"/. "${ED}"/opt/syncpilot/ || die

	# RivetHost finds its resources through /proc/self/exe, which follows
	# the symlink to the real location.
	dosym ../../opt/syncpilot/RivetHost /usr/bin/syncpilot

	newicon -s 512 "${DISTDIR}/${P}-icon-512.png" syncpilot.png
	make_desktop_entry syncpilot SyncPilot syncpilot \
		"Network;FileTransfer;P2P;GTK;" "StartupWMClass=site.jrtx.syncpilot"

	dodoc "${MY_PN}-${PV}"/{CHANGELOG.md,README.md,LICENSE}
}

pkg_postinst() {
	xdg_pkg_postinst

	elog "SyncPilot drives the rslsync binary from net-p2p/resilio-sync."
	elog "Upstream issue #6: versions 0.8.1 to 0.8.3 can open a blank window."
	elog "Follow https://github.com/turinglambdaai/syncpilot/issues/6 for a fix."
	elog "Upstream issue #5: the first launch installer reports a failure. It"
	elog "has no effect here, rslsync comes from net-p2p/resilio-sync."
}

pkg_postrm() {
	xdg_pkg_postrm
}