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

EAPI=8

inherit toolchain-funcs

DESCRIPTION="Modern, actively maintained Quake III Arena engine (Vulkan + OpenGL)"
HOMEPAGE="https://github.com/ec-/Quake3e"

if [[ ${PV} == 9999 ]]; then
	inherit git-r3
	EGIT_REPO_URI="https://github.com/ec-/Quake3e.git"
	S="${WORKDIR}/${P}"
else
	MY_PV="${PV//./-}"
	SRC_URI="https://github.com/ec-/Quake3e/archive/refs/tags/${MY_PV}.tar.gz -> ${P}.tar.gz"
	S="${WORKDIR}/Quake3e-${MY_PV}"
	KEYWORDS="~amd64 ~x86"
fi

LICENSE="GPL-2"
SLOT="0"
IUSE="+client +vulkan +opengl curl +dedicated +vorbis"

REQUIRED_USE="
	|| ( client dedicated )
	client? ( || ( opengl vulkan ) )
"

# Engine ships bundled libjpeg/libogg/libvorbis/Vulkan headers; we prefer
# system libraries where the build system allows it (USE_SYSTEM_*).
RDEPEND="
	client? (
		media-libs/libsdl2
		media-libs/libjpeg-turbo:0=
		opengl? ( media-libs/libglvnd )
		vulkan? ( media-libs/vulkan-loader )
		curl? ( net-misc/curl )
		vorbis? (
			media-libs/libogg
			media-libs/libvorbis
		)
	)
"
DEPEND="${RDEPEND}"
BDEPEND="virtual/pkgconfig"

# The upstream Makefile uses its own BASE_CFLAGS and does not honour the
# user CFLAGS/LDFLAGS, so silence the respect-flags QA checks.
QA_FLAGS_IGNORED="usr/.*/quake3e/quake3e.*"

src_compile() {
	# Gentoo exports ARCH=amd64, which overrides quake3e's Makefile ARCH (it
	# expects x86_64) and drops the SSE asm sound mixer (snd_mix_x86_64.s),
	# breaking the link: undefined reference to S_WriteLinearBlastStereo16_SSE_x64.
	local q3arch
	case $(tc-arch) in
		amd64) q3arch=x86_64 ;;
		x86)   q3arch=x86 ;;
		arm64) q3arch=aarch64 ;;
		*)     q3arch=$(tc-arch) ;;
	esac

	emake \
		ARCH="${q3arch}" \
		V=1 \
		CC="$(tc-getCC)" \
		USE_SDL=1 \
		USE_LOCAL_HEADERS=0 \
		USE_SYSTEM_JPEG=1 \
		USE_RENDERER_DLOPEN=1 \
		USE_CURL=$(usex curl 1 0) \
		USE_CURL_DLOPEN=0 \
		USE_VULKAN=$(usex vulkan 1 0) \
		USE_VULKAN_API=$(usex vulkan 1 0) \
		USE_OPENGL=$(usex opengl 1 0) \
		USE_OPENGL_API=$(usex opengl 1 0) \
		USE_OGG_VORBIS=$(usex vorbis 1 0) \
		USE_SYSTEM_OGG=$(usex vorbis 1 0) \
		USE_SYSTEM_VORBIS=$(usex vorbis 1 0) \
		RENDERER_DEFAULT=$(usex vulkan vulkan opengl) \
		BUILD_CLIENT=$(usex client 1 0) \
		BUILD_SERVER=$(usex dedicated 1 0) \
		release
}

src_install() {
	local libdir="/usr/$(get_libdir)/quake3e"
	local bdir f
	bdir=$(echo "${S}"/build/release-*) || die "build dir not found"
	[[ -d ${bdir} ]] || die "release build directory missing"

	exeinto "${libdir}"

	if use client; then
		# client binary: quake3e.<arch> (not the .ded variant, not a .so)
		for f in "${bdir}"/quake3e.*; do
			[[ ${f} == *.so ]] && continue
			[[ ${f} == *.ded.* ]] && continue
			newexe "${f}" quake3e
		done
		# dlopen renderers (quake3e_<renderer>_<arch>.so) live next to the
		# binary: the engine resolves them via dirname(/proc/self/exe).
		for f in "${bdir}"/quake3e_*_*.so; do
			doexe "${f}"
		done
		dosym "../$(get_libdir)/quake3e/quake3e" /usr/bin/quake3e
		# convenience alias: quake3 -> quake3e (resolved via /proc/self/exe)
		dosym quake3e /usr/bin/quake3
	fi

	if use dedicated; then
		for f in "${bdir}"/quake3e.ded.*; do
			newexe "${f}" quake3e.ded
		done
		dosym "../$(get_libdir)/quake3e/quake3e.ded" /usr/bin/quake3e.ded
	fi

	dodoc README.md
	docinto changelog
	dodoc -r docs/. 2>/dev/null
}

pkg_postinst() {
	elog "Quake3e looks for game data in:"
	elog "  - the engine directory (/usr/$(get_libdir)/quake3e), and"
	elog "  - your home path: ~/.q3a (default, same as id Quake III)"
	elog
	elog "Copy your retail pak0.pk3..pak8.pk3 (and any mods) into:"
	elog "  ~/.q3a/baseq3/"
	elog
	if use vulkan; then
		elog "Default renderer: Vulkan. Switch at runtime with:"
		elog "  \\cl_renderer opengl ; \\vid_restart   (or 'vulkan' to switch back)"
	fi
	if use dedicated; then
		elog "Dedicated server binary installed as: quake3e.ded"
	fi
}