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

EAPI=8

PYTHON_COMPAT=( python3_14 )
inherit flag-o-matic python-any-r1 toolchain-funcs

DESCRIPTION="Oberon Barev serverless messaging plugin for Pidgin over Yggdrasil"
HOMEPAGE="https://github.com/norayr/barev-purple-oberon"
SRC_URI="https://github.com/norayr/${PN}/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="GPL-3+"
SLOT="0"
KEYWORDS="~amd64"
IUSE="+gtk test"
# The privately linked VOC archive includes XMPP code with unconfirmed licensing.
RESTRICT="mirror bindist !test? ( test )"

DEPEND="
	>=dev-libs/glib-2.54:2
	net-im/pidgin:0
	gtk? ( x11-libs/gtk+:2 )
"
RDEPEND="
	${DEPEND}
	net-p2p/yggdrasil-go
"
BDEPEND="
	${PYTHON_DEPS}
	>=dev-build/make-4.3
	>=dev-lang/voc-5.3.0[network]
	virtual/pkgconfig
	test? (
		sys-apps/dbus
		$(python_gen_any_dep 'dev-python/cryptography[${PYTHON_USEDEP}]')
	)
"

python_check_deps() {
	if use test; then
		python_has_version "dev-python/cryptography[${PYTHON_USEDEP}]"
	fi
}

src_configure() {
	tc-export CC PKG_CONFIG
	filter-lto
	unset OBERON MODULES VOCROOT VOCLIBDIR VOC_MODULE_PATH VOC_SYM_PATH VOC_RUNPATH
}

barev_make() {
	emake CC="${CC}" PKG_CONFIG="${PKG_CONFIG}" PYTHON="${PYTHON}" \
		PIDGIN_UI="$(usex gtk 1 0)" \
		CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" \
		PLUGINDIR="${EPREFIX}/usr/$(get_libdir)/purple-2" \
		DATADIR="${EPREFIX}/usr/share" "$@"
}

src_compile() {
	barev_make plugin
}

src_test() {
	# Keep libpurple's optional media probing away from host devices.
	export GST_PLUGIN_SYSTEM_PATH_1_0="" GST_PLUGIN_PATH_1_0=""
	export GST_REGISTRY_1_0="${T}/gstreamer-registry.bin"
	barev_make test protocol-test protocol-address-test tls-test auth-test \
		auth-protocol-test dbus-plugin-test dbus-publishing-plugin-test
}

src_install() {
	barev_make DESTDIR="${D}" install
	dodoc addresses.md identity.md images.md changes.md testing.md dbus.md \
		posts.md post-snapshots.md publishing.md publishing-accounts.md \
		post-io.md post-async.md
}

pkg_postinst() {
	elog "Restart Pidgin and choose the 'VOC Barev' protocol when adding an account."
	elog "Add contacts as nick@Yggdrasil-IPv6 or nick@<encoded>.barev.v6.alt."
	elog "Get Info shows both address forms; Accounts -> your account -> My Barev address"
	elog "shows your own full encoded address for sharing. Conversion needs no DNS server."
	elog "Back up your profile. Exchange and verify Ed25519 keys,"
	elog "pin each other's key through Peer identity, then reconnect both accounts."
	elog "Pinned connections require mutual TLS 1.3 and SASL EXTERNAL; unpinned contacts use legacy transport."
	use gtk && elog "The conversation information pane shows a shield for authenticated peers; hover for details."
	elog "The default messaging port is TCP 1337; file transfers use TCP 50000-50049."
	elog "Use Insert -> Image in a conversation to send PNG/JPEG pictures."
	elog "Large pictures offer Fit for chat, Send original, or Cancel."
	elog "For Insert Image, disable Colorize on the sender and disable Conversation Colors'"
	elog "Ignore incoming format option on the receiver. See images.md for details."
	elog "The local D-Bus publishing interface is described in dbus.md."
	elog "If C Barev is enabled at the same time, give the accounts different local ports."
	elog "Remove or move any manually installed ~/.purple/plugins/libvoc-barev.so"
	elog "before starting Pidgin, so it loads the packaged plugin."
}