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

EAPI=8

inherit cmake xdg

WHISPER_PV=1.9.4
ORT_PV=1.30.0

DESCRIPTION="Open-source, cross-platform video editor"
HOMEPAGE="https://github.com/thiagovespa/clivcut"

if [[ ${PV} == 9999 ]]; then
	inherit git-r3
	EGIT_REPO_URI="https://github.com/thiagovespa/clivcut.git"
else
	SRC_URI="https://github.com/thiagovespa/clivcut/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"
	KEYWORDS="~amd64"
fi
# The build would download these itself; Portage fetches them instead.
SRC_URI+="
	whisper? (
		https://github.com/ggml-org/whisper.cpp/archive/refs/tags/v${WHISPER_PV}.tar.gz
			-> whisper-cpp-${WHISPER_PV}.tar.gz
	)
	onnx? (
		https://github.com/microsoft/onnxruntime/releases/download/v${ORT_PV}/onnxruntime-linux-x64-${ORT_PV}.tgz
	)
"

LICENSE="GPL-3+ onnx? ( MIT ) whisper? ( MIT )"
SLOT="0"
IUSE="+onnx +tts vulkan +whisper"
REQUIRED_USE="
	tts? ( onnx )
	vulkan? ( whisper )
"
# The test suite needs a GPU (or Xvfb with Mesa) and generates media with ffmpeg.
RESTRICT="test"

RDEPEND="
	>=dev-qt/qtbase-6.7:6[concurrent,gui,network]
	>=dev-qt/qtdeclarative-6.7:6
	>=dev-qt/qtsvg-6.7:6
	media-libs/libsdl2
	media-video/ffmpeg:=
	tts? ( app-accessibility/espeak-ng )
	vulkan? ( media-libs/vulkan-loader )
"
DEPEND="
	${RDEPEND}
	>=dev-qt/qtshadertools-6.7:6
	vulkan? ( dev-util/vulkan-headers )
"
BDEPEND="
	>=dev-qt/qtshadertools-6.7:6
	dev-qt/qttools:6[linguist]
	virtual/pkgconfig
	onnx? ( dev-util/patchelf )
	vulkan? ( media-libs/shaderc )
"

# Official ONNX Runtime binaries (the build only links against them).
QA_PREBUILT="usr/lib*/${PN}/libonnxruntime.so*"

src_unpack() {
	if [[ ${PV} == 9999 ]]; then
		git-r3_src_unpack
	fi
	default
}

src_configure() {
	local mycmakeargs=(
		-DCLIVCUT_BUILD_TESTS=OFF
		-DCLIVCUT_WITH_WHISPER=$(usex whisper)
		-DCLIVCUT_WITH_ONNX=$(usex onnx)
		-DFETCHCONTENT_FULLY_DISCONNECTED=ON
	)
	if use whisper; then
		mycmakeargs+=(
			-DFETCHCONTENT_SOURCE_DIR_WHISPER="${WORKDIR}/whisper.cpp-${WHISPER_PV}"
			-DCLIVCUT_WHISPER_VULKAN=$(usex vulkan)
			# CFLAGS already carry -march; do not let ggml add its own.
			-DGGML_NATIVE=OFF
		)
	fi
	if use onnx; then
		mycmakeargs+=(
			-DCLIVCUT_ONNXRUNTIME_ROOT="${WORKDIR}/onnxruntime-linux-x64-${ORT_PV}"
			-DCLIVCUT_WITH_TTS=$(usex tts)
			-DCLIVCUT_SYSTEM_ESPEAK=ON
		)
	fi
	cmake_src_configure
}

src_install() {
	cmake_src_install

	if use onnx; then
		# Keep the bundled ONNX Runtime private, so it never clashes with a system copy.
		local libdir=/usr/$(get_libdir)/${PN}
		rm "${ED}"/usr/$(get_libdir)/libonnxruntime.so* || die
		insinto ${libdir}
		doins "${WORKDIR}"/onnxruntime-linux-x64-${ORT_PV}/lib/libonnxruntime.so.${ORT_PV}
		dosym libonnxruntime.so.${ORT_PV} ${libdir}/libonnxruntime.so.1
		local exe
		for exe in clivcut clivcut-render; do
			patchelf --set-rpath "${EPREFIX}${libdir}" "${ED}"/usr/bin/${exe} || die
		done
	fi
}

pkg_postinst() {
	xdg_pkg_postinst
	if use onnx || use whisper; then
		elog "The AI models are downloaded from inside the app the first time each"
		elog "feature is used, into ~/.local/share/clivcut/models."
	fi
}