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

EAPI=8

inherit cmake flag-o-matic xdg

MY_PN="UZDoom"
DESCRIPTION="Modder-friendly hardware-accelerated source port of the DOOM engine"
HOMEPAGE="https://zdoom.org"
SRC_URI="https://github.com/${MY_PN}/${MY_PN}/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz"
S="${WORKDIR}/${MY_PN}-${PV}"

LICENSE="Apache-2.0 BSD BZIP2 GPL-3 LGPL-2.1+ LGPL-3 MIT
	non-free? ( Activision ChexQuest3 DOOM-COLLECTORS-EDITION freedist WidePix )"
SLOT="0"
KEYWORDS="~amd64 ~arm64 ~ppc64"
IUSE="alsa debug gles2 mpg123 +non-free openmp +sndfile +swr telemetry vulkan"

DEPEND="
	app-arch/bzip2:=
	media-libs/libsdl2[gles2?,joystick,opengl,video,vulkan?]
	media-libs/libvpx:=
	media-libs/libwebp:=
	media-libs/openal

	alsa? ( media-libs/alsa-lib )
	mpg123? ( media-sound/mpg123 )
	sndfile? ( media-libs/libsndfile[-minimal] )
"
RDEPEND="
	${DEPEND}
	!games-fps/gzdoom
"
BDEPEND="
	virtual/pkgconfig
"

PATCHES=(
	"${FILESDIR}"/${PN}-5.0.1-deps.patch
)

src_prepare() {
	# The bundled cppdap is heavily modified.
	rm -r docs/licenses libraries/{bzip2,webp} || die
	cmake_src_prepare

	cat <<-EOF > "${S}/src/gitinfo.h" || die
	// 0
	// The above line prevents UpdateRevision.cmake from replacing this file.
	// This file was generated by the Gentoo ebuild.

	#define GIT_DESCRIPTION "${PV}"
	#define GIT_TAG "0"
	#define GIT_DISTANCE 0
	#define GIT_HASH "0"
	#define GIT_TIME ""
	EOF
}

src_configure() {
	# https://bugs.gentoo.org/858749
	filter-lto
	append-flags -fno-strict-aliasing

	local mycmakeargs=(
		-DBUILD_NONFREE=$(usex non-free)
		-DDYN_OPENAL=OFF
		-DFORCE_INTERNAL_WEBP=OFF
		-DFORCE_NO_LTO=ON
		-DHAVE_GLES2=$(usex gles2)
		-DHAVE_VULKAN=$(usex vulkan)
		-DINSTALL_DOCS_PATH="${EPREFIX}/usr/share/doc/${PF}"
		-DINSTALL_PK3_PATH="${EPREFIX}/usr/share/doom"
		-DINSTALL_SOUNDFONT_PATH="${EPREFIX}/usr/share/doom"
		-DNO_OPENAL=OFF
		-DNO_OPENMP=$(usex !openmp)
		-DSEND_ANON_STATS=$(usex telemetry)
		-DZDOOM_ENABLE_SWR=$(usex swr)

		-DCMAKE_DISABLE_FIND_PACKAGE_ALSA="$(usex !alsa)"
		-DCMAKE_DISABLE_FIND_PACKAGE_MPG123="$(usex !mpg123)"
		-DCMAKE_DISABLE_FIND_PACKAGE_SndFile="$(usex !sndfile)"
		-DDYN_MPG123=OFF
		-DDYN_SNDFILE=OFF
	)

	use debug || append-cppflags -DNDEBUG
	cmake_src_configure
}

pkg_postinst() {
	xdg_pkg_postinst

	if ! use non-free ; then
		ewarn
		ewarn "${MY_PN} has been installed without non-free components. Commercial"
		ewarn "games should still run, but the experience may be suboptimal."
		ewarn
	fi
}
