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

EAPI=8

inherit flag-o-matic toolchain-funcs

# Upstream has never tagged a release; the Makefile declares 0.2.2.
MY_COMMIT="ffe2632eb09f46a696fbf905788521c49f98b342"
# Same rtaudio snapshot that media-libs/wineasio pins; only asio.h is used.
RTAUDIO_SHA="e03448bd15c1c34e842459939d755f5f89e880ed"

DESCRIPTION="ASIO to PipeWire driver for Wine."
HOMEPAGE="https://github.com/dekrain/pipewireasio"
SRC_URI="https://github.com/dekrain/${PN}/archive/${MY_COMMIT}.tar.gz
	-> ${P}.tar.gz
	https://github.com/falkTX/rtaudio/archive/${RTAUDIO_SHA}.tar.gz
	-> ${PN}-rtaudio-${RTAUDIO_SHA}.tar.gz"
S="${WORKDIR}/${PN}-${MY_COMMIT}"

# The driver is LGPL-2.1; the configuration GUI is GPL-2 with a GPL-3 option.
LICENSE="GPL-2 GPL-3 LGPL-2.1"
SLOT="0"
KEYWORDS="~amd64"
IUSE="abi_x86_32"

RDEPEND="
	dev-qt/qtbase:6[gui,widgets]
	media-video/pipewire[abi_x86_32(-)?]
	virtual/wine
"
DEPEND="${RDEPEND}"

PATCHES=( "${FILESDIR}/${PN}-0.2.2-qt6.patch" )

src_prepare() {
	rm -rf rtaudio || die
	mv "${WORKDIR}/rtaudio-${RTAUDIO_SHA}" rtaudio || die

	sed -re "s/^CC( .*)?= gcc$/CC = $(tc-getCC)/" -i Makefile.mk || die

	# The checked-in moc and uic output was generated by Qt5; regenerate it.
	rm -f new_gui/*.moc new_gui/ui_*.hpp || die

	default
}

src_compile() {
	filter-lto

	local qt6_libexec
	qt6_libexec=$($(tc-getPKG_CONFIG) --variable=libexecdir Qt6Core) || die
	[[ -x ${qt6_libexec}/moc ]] || die "moc not found in ${qt6_libexec}"

	local emakeargs=(
		"VERSION=${PV%_p*}"
		"PREFIX=${EPREFIX}/usr"
		"MOC=${qt6_libexec}/moc"
		"UIC=${qt6_libexec}/uic"
		"QT_CFLAGS=$($(tc-getPKG_CONFIG) --cflags Qt6Core Qt6Gui Qt6Widgets)"
		"QT_LIBS=$($(tc-getPKG_CONFIG) --libs Qt6Core Qt6Gui Qt6Widgets)"
	)

	emake "${emakeargs[@]}" 64
	use abi_x86_32 && emake "${emakeargs[@]}" 32
}

src_install() {
	insinto /usr/lib/wine/x86_64-windows
	doins "build64/${PN}64.dll"
	exeinto /usr/lib/wine/x86_64-unix
	doexe "build64/${PN}64.dll.so"
	doexe build64/libpwasio_gui.so

	if use abi_x86_32; then
		insinto /usr/lib/wine/i386-windows
		doins "build32/${PN}32.dll"
		exeinto /usr/lib/wine/i386-unix
		doexe "build32/${PN}32.dll.so"
		doexe build32/libpwasio_gui.so
	fi

	einstalldocs
}

pkg_postinst() {
	elog "Register the driver in a Wine prefix with:"
	elog
	elog "    wine regsvr32 ${PN}64.dll"
}