# Copyright 1999-2026 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

EAPI=8

inherit git-r3
inherit meson

DESCRIPTION="A bridge to use Windows VST and CLAP plugins on Linux via wine."
HOMEPAGE="https://github.com/robbert-vdh/yabridge"

EGIT_COMMIT="48ea9749b682c48875366134a42073d6b3d0a8c4"
EGIT_REPO_URI="https://github.com/robbert-vdh/yabridge.git"

# NOTE: Sub-repositories are originally cloned through Meson which then also adds additional configuration files.
#       Those files are unfortunately missing when cloned through this ebuild and need to be added through patches.
SUBREPOS=(
	"https://github.com/chriskohlhoff/asio.git                 ed6aa8a13d51dfc6c00ae453fc9fb7df5d6ea963 subprojects/asio"
	"https://github.com/fraillt/bitsery.git                    d1a47e06e2104b195a19c73b61f1d5c1dceaa228 subprojects/bitsery"
	"https://github.com/Naios/function2.git                    9e303865d14f1204f09379e37bbeb30c4375139a subprojects/function2"
	"https://github.com/gulrak/filesystem.git                  8a2edd6d92ed820521d42c94d179462bf06b5ed3 subprojects/ghc_filesystem"
	"https://github.com/marzer/tomlplusplus.git                30172438cee64926dc41fdd9c11fb3ba5b2ba9de subprojects/tomlplusplus"
	"https://github.com/free-audio/clap.git                    094bb76c85366a13cc6c49292226d8608d6ae50c subprojects/clap"
	"https://github.com/robbert-vdh/vst3sdk.git                v3.7.7_build_19-patched                  subprojects/vst3"
	"https://github.com/steinbergmedia/vst3_base.git           ea2bac9a109cce69ced21833fa6ff873dd6e368a subprojects/vst3/base"
	"https://github.com/steinbergmedia/vst3_pluginterfaces.git bc5ff0f87aaa3cd28c114810f4f03c384421ad2c subprojects/vst3/pluginterfaces"
	"https://github.com/steinbergmedia/vst3_public_sdk.git     bbb0538535b171e805c8a8b612c2cd8a5f95738b subprojects/vst3/public.sdk"
)

LICENSE="GPL-3"
SLOT="0"

KEYWORDS="~amd64"

DEPEND="
	!media-sound/yabridge-bin
"

RDEPEND="
	${DEPEND}

	media-sound/yabridgectl

	app-crypt/libmd
	dev-libs/libbsd
	sys-devel/gcc
	sys-libs/glibc
	x11-libs/libXau
	x11-libs/libxcb
	x11-libs/libXdmcp
	virtual/wine
"

RESTRICT="mirror"

EMESON_BUILDTYPE="release"

PATCHES=(
	"${FILESDIR}/${P}-meson_subrepos.patch"
)

pkg_setup() {
	if [[ "${PV}" =~ _p[0-9]+$ ]]; then
	        #      12345678901234567890123456789012345678901234567890123456789012345678901234567890
		ewarn "You are about to install an untagged development version of yabridge"
		ewarn "(this is NOT version ${PV/_p*/} but the development state as of ${PV/*_p/})."
	fi
}

src_unpack() {
	git-r3_src_unpack || die "main git-r3_src_unpack failed"

	for subrepo in "${SUBREPOS[@]}"; do
		read -r subrepo_uri subrepo_ref subrepo_dest <<< "$subrepo" || die "failed to split subrepository definition: ${subrepo}"
		subrepo_dest="${S}/${subrepo_dest}"
		#echo "uri: ${subrepo_uri} / ref: ${subrepo_ref} / dest: ${subrepo_dest}"
		mkdir -p "${subrepo_dest}" || die "failed to create directory for subrepository: ${subrepo_dest}"
		git-r3_fetch "${subrepo_uri}" "${subrepo_ref}" || die "failed to fetch subrepository ${subrepo_uri} at revision ${subrepo_ref}"
		EGIT_SUBMODULES=() git-r3_checkout "${subrepo_uri}" "${subrepo_dest}" || die "failed to checkout subrepository ${subrepo_uri} to ${subrepo_dest}"

		# fake Meson checkout
		#echo "${subrepo_ref}" >"${subrepo_dest}/.meson-subproject-wrap-hash.txt"
		git -C "${subrepo_dest}" show --pretty='%H' --no-patch >"${subrepo_dest}/.meson-subproject-wrap-hash.txt"
	done
}

src_configure() {
	# arguments taken from https://github.com/robbert-vdh/yabridge/blob/master/.github/workflows/build.yml
	local emesonargs=(
		--cross-file=cross-wine.conf
		--unity=on
		--unity-size=10000
		-Dbitbridge=true
	)

	meson_src_configure
}

src_install() {
	# NOTE: by default, meson install would be called, however the project does not define that step

	# to avoid issues with linking etc. we install to default locations; see Arch Linux repository for reference:
	# https://archlinux.org/packages/multilib/x86_64/yabridge/
	# https://archlinux.org/packages/multilib/x86_64/yabridgectl/

	cd "${BUILD_DIR}"

	exeinto /usr/bin
	#doexe yabridgectl
	doexe *.exe
	doexe *.exe.so

	dolib.so *-clap.so
	dolib.so *-vst2.so
	dolib.so *-vst3.so

	cd "${S}"
	dodoc *.md
}

pkg_postinst() {
	if [[ "${PV}" =~ _p[0-9]+$ ]]; then
	        #      12345678901234567890123456789012345678901234567890123456789012345678901234567890
		ewarn "You have just installed an untagged development version of yabridge which"
		ewarn "actually is NOT version ${PV/_p*/}."
		ewarn ""
		ewarn "When reporting any issues to the yabridge project, please mention the following"
		ewarn "information to identify the source code used by this build:"
		ewarn ""
		ewarn "  yabridge commit hash: ${EGIT_COMMIT}"
		ewarn "       as available on: ${PV/*_p/} (YYYYMMDD)"
		ewarn ""
		ewarn "  Dependencies:"
		for subrepo in "${SUBREPOS[@]}"; do
			ewarn "    ${subrepo}"
		done
		ewarn ""
		ewarn "Make sure to switch back to media-sound/yabridge-bin when a stable releases"
		ewarn "more recent than ${PV/_p*/} is available."
	fi
}