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

EAPI=8

DISTUTILS_SINGLE_IMPL=1
DISTUTILS_USE_PEP517=setuptools
PYTHON_COMPAT=( python3_{12..15} )
PYTHON_REQ_USE="sqlite"
WX_GTK_VER="3.2-gtk3"

inherit cmake distutils-r1 git-r3 wxwidgets

AUDACITY_HEADERS_PV="3.7.7"

DESCRIPTION="Export cuts and splits from Audacity via FFmpeg -c copy without re-encoding"
HOMEPAGE="https://github.com/vitaly-zdanevich/mod-export-no-reencode"
EGIT_REPO_URI="https://github.com/vitaly-zdanevich/${PN}.git"
SRC_URI="
	audacity-module? (
		https://github.com/audacity/audacity/releases/download/Audacity-${AUDACITY_HEADERS_PV}/audacity-sources-${AUDACITY_HEADERS_PV}.tar.gz
	)
"

LICENSE="MIT audacity-module? ( GPL-2+ )"
SLOT="0"
IUSE="+audacity-module"

DEPEND="
	audacity-module? (
		>=media-sound/audacity-3.7.0
		<media-sound/audacity-3.8
		x11-libs/wxGTK:${WX_GTK_VER}=[X]
	)
"
RDEPEND="
	${DEPEND}
	media-video/ffmpeg
"
BDEPEND="
	test? (
		media-video/ffmpeg[opus]
	)
"

DOCS=( README.md )

EPYTEST_PLUGINS=()
distutils_enable_tests pytest

src_unpack() {
	git-r3_src_unpack
	if use audacity-module; then
		tar -xf "${DISTDIR}/audacity-sources-${AUDACITY_HEADERS_PV}.tar.gz" \
			-C "${WORKDIR}" \
			"audacity-sources-${AUDACITY_HEADERS_PV}/libraries" || die
	fi
}

src_prepare() {
	distutils-r1_src_prepare
	if use audacity-module || use test; then
		CMAKE_USE_DIR="${S}/audacity3-module"
		BUILD_DIR="${WORKDIR}/module_build" cmake_src_prepare
	fi
}

src_configure() {
	distutils-r1_src_configure
	if use audacity-module || use test; then
		local CMAKE_USE_DIR="${S}/audacity3-module"
		local BUILD_DIR="${WORKDIR}/module_build"
		local mycmakeargs=()

		if use audacity-module; then
			setup-wxwidgets
			local audacity_pv
			audacity_pv="$(best_version -d media-sound/audacity)"
			audacity_pv="${audacity_pv#media-sound/audacity-}"
			audacity_pv="${audacity_pv%-r*}"
			[[ -n ${audacity_pv} ]] || die "Could not detect installed media-sound/audacity version"

			mycmakeargs+=(
				-DAUDACITY_HEADERS_DIR="${WORKDIR}/audacity-sources-${AUDACITY_HEADERS_PV}"
				-DAUDACITY_LIB_DIR="${ESYSROOT}/usr/$(get_libdir)/audacity"
				-DAUDACITY_VERSION_STRING="${audacity_pv}"
			)
		fi

		cmake_src_configure
	fi
}

src_compile() {
	distutils-r1_src_compile
	if use audacity-module || use test; then
		local CMAKE_USE_DIR="${S}/audacity3-module"
		local BUILD_DIR="${WORKDIR}/module_build"
		cmake_src_compile
	fi
}

python_test() {
	epytest tests
}

src_test() {
	distutils-r1_src_test
	local CMAKE_USE_DIR="${S}/audacity3-module"
	local BUILD_DIR="${WORKDIR}/module_build"
	cmake_src_test
}

src_install() {
	distutils-r1_src_install
	if use audacity-module; then
		exeinto "/usr/$(get_libdir)/audacity/modules"
		doexe "${WORKDIR}/module_build/mod-export-no-reencode.so"
	fi
}

pkg_postinst() {
	if use audacity-module; then
		elog "To enable the File > Export Without Re-encoding menu in Audacity 3:"
		elog "  Edit > Preferences > Modules > mod-export-no-reencode -> Enabled"
		elog "Then restart Audacity."
		elog "Rebuild ${CATEGORY}/${PN} after upgrading media-sound/audacity."
	fi
}