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

EAPI=8

inherit cmake

DESCRIPTION="SFZ sample player: LV2 plugin, JACK client, MIDI renderer and library"
HOMEPAGE="https://sfz.tools/sfizz/ https://github.com/sfztools/sfizz-ui"
# The sfizz-ui release archive includes the library and every submodule.
# The library-only sfizz release uses the very same file name thus the rename is needed.
SRC_URI="https://github.com/sfztools/sfizz-ui/releases/download/${PV}/${P}.tar.gz -> ${P}-ui.tar.gz"

# sfizz itself is BSD-2, the rest licenses list covers vendored parts (e.g. abseil, vstgui, dr_libs, ...)
LICENSE="BSD-2 Apache-2.0 BSD Boost-1.0 ISC MIT WTFPL-2 ZLIB public-domain"
SLOT="0"
KEYWORDS="~amd64"
IUSE="+gui +jack test"
RESTRICT="!test? ( test )"

RDEPEND="
	dev-libs/pugixml:=
	gui? (
		dev-libs/glib:2
		gnome-extra/zenity
		media-libs/fontconfig
		media-libs/freetype
		x11-libs/cairo[X]
		x11-libs/libxcb:=
		x11-libs/libxkbcommon[X]
		x11-libs/pango
		x11-libs/xcb-util
		x11-libs/xcb-util-cursor
	)
	jack? ( virtual/jack )
"
DEPEND="
	${RDEPEND}
	media-libs/lv2
	gui? (
		x11-libs/libX11
		x11-libs/xcb-util-keysyms
	)
"
BDEPEND="virtual/pkgconfig"

PATCHES=(
	"${FILESDIR}"/${P}-gcc15.patch
	"${FILESDIR}"/${P}-curve-test-tolerance.patch
)

src_prepare() {
	# Unused (VST3 is off and its SDK isn't fully shipped anyway). Its
	# cmake_minimum_required(3.4.3) files trip the CMake 4 compatibility check.
	rm -r plugins/vst || die

	cmake_src_prepare
}

src_configure() {
	local mycmakeargs=(
		-DENABLE_LTO=OFF
		-DSFIZZ_GIT_SUBMODULE_CHECK=OFF

		-DPLUGIN_LV2=ON
		-DPLUGIN_LV2_UI=$(usex gui)
		-DLV2_PLUGIN_INSTALL_DIR="${EPREFIX}/usr/$(get_libdir)/lv2"
		# the VST3 SDK is not part of the release archive
		-DPLUGIN_VST3=OFF
		-DPLUGIN_VST2=OFF
		-DPLUGIN_AU=OFF
		-DPLUGIN_PUREDATA=OFF

		-DSFIZZ_SHARED=ON
		-DSFIZZ_JACK=$(usex jack)
		-DSFIZZ_RENDER=ON
		-DSFIZZ_TESTS=$(usex test)
		-DSFIZZ_BENCHMARKS=OFF
		-DSFIZZ_DEMOS=OFF
		-DSFIZZ_DEVTOOLS=OFF

		# 'cause' bundled dr_libs/stb_vorbis already includes WAV, FLAC and Ogg
		-DSFIZZ_USE_SNDFILE=OFF
		-DSFIZZ_USE_SYSTEM_LV2=ON
		-DSFIZZ_USE_SYSTEM_PUGIXML=ON
		# vendored Abseil is pinned to LTS 20230125 and linked statically
		# newer versions are tricky and throw errors so skipping for now, mb later...
		-DSFIZZ_USE_SYSTEM_ABSEIL=OFF

		# doesn't hurt so included
		-DCMAKE_DISABLE_FIND_PACKAGE_Qt5=ON
		-DCMAKE_DISABLE_FIND_PACKAGE_OpenMP=ON
	)
	cmake_src_configure
}

src_test() {
	# The runner moves up from $PWD looking for tests/TestFiles so ctest
	# from the out-of-source build dir finds nothing, thus running it from the source
	cd "${S}"/library || die
	"${BUILD_DIR}"/library/bin/sfizz_tests || die "tests failed"
}