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

EAPI=8

inherit desktop xdg

MY_PN="audacity"

# Upstream renamed every Linux asset at the 4.0.0 stable release:
#   beta   tag Audacity-4.0.0-beta-4  file Audacity-4.0.0-beta4-x86_64.AppImage
#   stable tag Audacity-4.0.0         file audacity-linux-4.0.0-x86_64.AppImage
# Only the stable scheme is expressed here. A future pre-release must have its
# own derivation checked against the actual release assets -- the two schemes
# agree on nothing, not even the capitalisation, so it cannot be extrapolated.
MY_TAG="Audacity-${PV}"

DESCRIPTION="Multi-track audio editor and recorder (official Audacity 4 AppImage)"
HOMEPAGE="https://www.audacityteam.org/"
SRC_URI="
	amd64? (
		https://github.com/audacity/audacity/releases/download/${MY_TAG}/${MY_PN}-linux-${PV}-x86_64.AppImage
			-> ${P}-x86_64.AppImage
	)
	arm64? (
		https://github.com/audacity/audacity/releases/download/${MY_TAG}/${MY_PN}-linux-${PV}-aarch64.AppImage
			-> ${P}-aarch64.AppImage
	)
"
S="${WORKDIR}"

LICENSE="GPL-3+"
SLOT="0"
# 4.0.0 is the first release with an official aarch64 AppImage. Verified to be
# structurally identical to the x86_64 one (same layout, same set of external
# NEEDED entries, only the ELF interpreter differs).
KEYWORDS="-* ~amd64 ~arm64"
RESTRICT="bindist mirror strip"

QA_PREBUILT="*"

# Measured on the 4.0.0 payload, by walking every bundled ELF and keeping the
# NEEDED entries that resolve outside lib/ and fallback/. Qt6, wxWidgets, the
# codec stack, krb5, harfbuzz and freetype are all bundled and must NOT be
# listed here -- an earlier revision of this ebuild depended on expat, freetype
# and zlib, none of which the payload actually resolves from the system.
#
#   libX11 / libX11-xcb ... main binary + plugins/platforms/libqxcb.so
#   libxcb ............... plugins/platforms/libqxcb.so
#   libwayland-{client,egl} plugins/platforms/libqwayland.so and friends
#   libglib-2.0 et al .... libQt6Core / libQt6Gui / libQt6XcbQpa
#   libfontconfig ........ libQt6Gui
#   libEGL/libGL/libGLX/libOpenGL  libQt6Gui + the GL integration plugins
#   libasound ............ main binary
#   libcom_err ........... bundled libkrb5, reached from libQt6Network via the
#                          bundled libgssapi_krb5 -- a hard load-time edge
#   libdbus-1 ............ libQt6DBus, see the removal in src_install below
#
# Deliberately absent: x11-libs/gtk+:3, needed only by the optional
# plugins/platformthemes/libqgtk3.so, which Qt loads under GNOME and otherwise
# never touches; and dev-libs/nss plus dev-db/sqlite, reachable only through
# the bundled fallback/libnss3.so, which AppRun loads only when the system has
# no libnss3 of its own.
RDEPEND="
	dev-libs/glib:2
	dev-libs/wayland
	media-libs/alsa-lib
	media-libs/fontconfig
	media-libs/libglvnd
	sys-apps/dbus
	sys-fs/e2fsprogs
	virtual/opengl
	x11-libs/libX11
	x11-libs/libxcb
"

# Real binary lives at bin/audacity4portable inside the AppImage ("portable"
# build, not "audacity"). usr/ is a symlink to "." in the payload, so
# usr/bin/<x> and bin/<x> are the same file.
MY_BIN="audacity4portable"

src_unpack() {
	local appimage
	if use amd64; then
		appimage="${P}-x86_64.AppImage"
	elif use arm64; then
		appimage="${P}-aarch64.AppImage"
	else
		die "no upstream AppImage for this arch"
	fi

	cp "${DISTDIR}/${appimage}" "${WORKDIR}/${appimage}" || die
	chmod +x "${WORKDIR}/${appimage}" || die
	cd "${WORKDIR}" || die
	"./${appimage}" --appimage-extract > /dev/null || die
	rm -f "${WORKDIR}/${appimage}" || die
}

src_prepare() {
	default

	# The bundled libdbus-1.so.3 is dbus 1.12.20 built on a systemd CI runner,
	# so it carries NEEDED libsystemd.so.0. Nothing in ::gentoo provides that
	# soname outside sys-apps/systemd -- sys-apps/systemd-utils does not -- so
	# on an OpenRC or musl host the loader fails on it and Audacity does not
	# start at all. It is not a soft failure: libQt6DBus.so.6 is a NEEDED of
	# the main binary, so the whole process dies.
	#
	# Dropping it makes libQt6DBus.so.6 fall through its RUNPATH ($ORIGIN) to
	# the system sys-apps/dbus, which follows the host's own init choice.
	# Verified compatible: libQt6DBus references 89 dbus_* symbols, all at
	# version node LIBDBUS_1_3, which ::gentoo's dbus-1.16.2 defines. The only
	# other node either library defines is LIBDBUS_PRIVATE_<ver>, used solely
	# by dbus's own tools and referenced by nothing in this payload.
	#
	# Guarded rather than best-effort: if a future payload stops bundling it,
	# this must be noticed, not silently skipped.
	[[ -f squashfs-root/lib/libdbus-1.so.3 ]] ||
		die "bundled libdbus-1.so.3 is gone -- recheck whether the libsystemd workaround is still needed"
	rm squashfs-root/lib/libdbus-1.so.3 || die

	# Upstream ships the CMake install manifest inside the AppImage. Every line
	# is an absolute path from the GitHub runner that produced it
	# (/home/runner/work/audacity/...), which is a build-path leak and of no
	# use on a user's machine.
	rm -f squashfs-root/install_manifest.txt || die
}

src_install() {
	local dest="/opt/${PN}"

	# Install the fully self-contained bundle. The AppImage ships a portable
	# layout (bin/, lib/, lib/audacity/, plugins/, qml/, fallback/, share/,
	# translations/) plus its own AppRun launcher that sets APPDIR /
	# LD_LIBRARY_PATH and loads jack/nss fallbacks when the system lacks them.
	# doins -r preserves symlinks in EAPI 8, which matters here: the payload
	# contains "usr -> .", and a follow-symlinks copy would recurse forever.
	insinto "${dest}"
	doins -r squashfs-root/.
	chmod -R a+rX "${ED}${dest}" || die
	fperms +x \
		"${dest}/AppRun" \
		"${dest}/bin/${MY_BIN}" \
		"${dest}/bin/crashpad_handler" \
		"${dest}/bin/findlib" \
		"${dest}/bin/ldd-recursive" \
		"${dest}/bin/portable-utils" \
		"${dest}/bin/rm-empty-dirs" \
		"${dest}/wrappers/xdg-open"

	# Launch through the bundled AppRun so APPDIR / LD_LIBRARY_PATH / fallback
	# resolution matches upstream. Expose it under a distinct name so it never
	# collides with a from-source media-sound/audacity in /usr/bin/audacity.
	dosym "../${PN}/AppRun" /opt/bin/audacity-bin

	# Desktop entry: rewrite Exec/Icon to our install and a stable menu name.
	sed \
		-e "s|^Exec=.*|Exec=audacity-bin %U|" \
		-e "s|^Icon=.*|Icon=audacity-bin|" \
		-e "s|^Name=.*|Name=Audacity 4|" \
		squashfs-root/share/applications/org.audacityteam.Audacity4portable.desktop \
		> "${T}/audacity-bin.desktop" || die
	domenu "${T}/audacity-bin.desktop"

	# Icons (rename hicolor app icons to match Icon=audacity-bin). These are
	# the eight sizes the payload actually ships under apps/; it also carries
	# mimetype icons, which are left alone because the .desktop's MimeType is
	# not registered here.
	local size
	for size in 16 24 32 48 64 96 128 512; do
		newicon -s "${size}" \
			"squashfs-root/share/icons/hicolor/${size}x${size}/apps/audacity4portable.png" \
			audacity-bin.png
	done
}