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

EAPI=8

inherit cmake db-use multilib xdg-utils

MY_PV="${PV/_p/-hotfix-}"
DESCRIPTION="Proof-of-Stake based cryptocurrency that rewards BOINC computation"
HOMEPAGE="https://gridcoin.us/"
SRC_URI="https://github.com/${PN}-community/${PN^}-Research/archive/refs/tags/${MY_PV}.tar.gz -> ${P}.tar.gz"
S="${WORKDIR}/${PN^}-Research-${MY_PV}"

LICENSE="BSD BSD-2 Boost-1.0 MIT SSLeay"
SLOT="0"
KEYWORDS="~amd64"
IUSE="+asm dbus gui qrcode test +upnp"
IUSE+=" cpu_flags_arm_neon cpu_flags_x86_avx2 cpu_flags_x86_sha cpu_flags_x86_sse4_1"
RESTRICT="!test? ( test )"

REQUIRED_USE="
	dbus? ( gui )
	qrcode? ( gui )
"

BDB_SLOT="5.3"
RDEPEND="
	>=dev-libs/boost-1.66.0:=[zlib(+)]
	>=dev-libs/libsecp256k1-0.2.0:=[recovery(+)]
	>=dev-libs/leveldb-1.21:=
	dev-libs/libzip:=
	dev-libs/openssl:=
	dev-libs/univalue
	net-misc/curl[ssl]
	sys-libs/db:${BDB_SLOT}[cxx]
	gui? (
		dev-qt/qt5compat:6[gui]
		dev-qt/qtbase:6[concurrent,dbus?,gui,network,widgets]
		dev-qt/qtsvg:6
		qrcode? ( media-gfx/qrencode:= )
	)
	upnp? ( net-libs/miniupnpc:= )
"
DEPEND="${RDEPEND}
	virtual/zlib
"
BDEPEND="
	virtual/pkgconfig
	gui? ( dev-qt/qttools:6[linguist] )
	test? ( dev-util/xxd )
"
IDEPEND="gui? ( dev-util/desktop-file-utils )"

src_configure() {
	local mycmakeargs=(
		-DENABLE_DAEMON=$(usex !gui)
		-DENABLE_GUI=$(usex gui)
		-DENABLE_TESTS=$(usex test)

		-DENABLE_SSE41=$(usex cpu_flags_x86_sse4_1)
		-DENABLE_AVX2=$(usex cpu_flags_x86_avx2)
		-DENABLE_X86_SHANI=$(usex cpu_flags_x86_sha)
		-DENABLE_ARM_SHANI=$(usex cpu_flags_arm_neon)
		-DUSE_ASM=$(usex asm)

		-DENABLE_QRENCODE=$(usex qrcode)
		-DENABLE_UPNP=$(usex upnp)
		-DDEFAULT_UPNP=$(usex upnp)
		-DUSE_DBUS=$(usex dbus)
		-DUSE_QT6=ON

		-DSYSTEM_BDB=ON
		-DBerkeleyDB_INCLUDE_DIR="$(db_includedir ${BDB_SLOT})"
		-DBerkeleyDB_CXX_LIBRARY="${ESYSROOT}/usr/$(get_libdir)/libdb_cxx-${BDB_SLOT}$(get_libname)"
		-DSYSTEM_LEVELDB=ON
		-DSYSTEM_SECP256K1=ON
		-DSYSTEM_UNIVALUE=ON
		-DSYSTEM_XXD=ON
	)
	cmake_src_configure
}

src_install() {
	cmake_src_install
	if use gui; then
		rm "${ED}"/usr/bin/gridcoin.icns || die
	fi
}

pkg_postinst() {
	# we don't use xdg.eclass because it adds unconditional IDEPENDs
	if use gui; then
		xdg_desktop_database_update
		xdg_icon_cache_update
	fi
}

pkg_postrm() {
	if use gui; then
		xdg_desktop_database_update
		xdg_icon_cache_update
	fi
}
