#!/usr/bin/make -f
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export LC_ALL=C.UTF-8
export TZ=UTC

DEB_CPPFLAGS_MAINT_APPEND := \
	-DWITH_OPENARENA \
	$(NULL)

DEB_CFLAGS_MAINT_APPEND := \
	-fsigned-char \
	$(NULL)

include /usr/share/dpkg/architecture.mk

ifeq ($(DEB_HOST_ARCH_CPU),i386)
# The build fails without this, and Debian >= 13 requires SSE2
# as per the release notes
DEB_CFLAGS_MAINT_APPEND += -msse2
endif

include /usr/share/dpkg/default.mk

%:
	dh $@ --buildsystem=cmake+ninja

OPTIONS = \
	-DBUILD_CLIENT=ON \
	-DBUILD_GAME_LIBRARIES=ON \
	-DBUILD_GAME_QVMS=OFF \
	-DBUILD_RENDERER_GL1=ON \
	-DBUILD_RENDERER_GL2=ON \
	-DBUILD_SERVER=ON \
	-DCMAKE_INSTALL_PREFIX=/usr/lib/ioquake3 \
	-DPRODUCT_VERSION="$(DEB_VERSION)/$(DEB_VENDOR)" \
	-DUSE_CODEC_VORBIS=ON \
	-DUSE_FREETYPE=OFF \
	-DUSE_HTTP=ON \
	-DUSE_INTERNAL_LIBS=OFF \
	-DUSE_MUMBLE=ON \
	-DUSE_OPENAL=ON \
	-DUSE_OPENAL_DLOPEN=OFF \
	-DUSE_RENDERER_DLOPEN=ON \
	-DUSE_VOIP=ON \
	$(NULL)

# This might seem unexpected with the existence of the openarena package,
# but it's actually what we want. If we built a STANDALONE engine, it would
# be suitable for derivatives like OpenArena, but not for Quake III Arena.
# A non-STANDALONE engine can still be configured with com_standalone
# at runtime.
OPTIONS += -DBUILD_STANDALONE=OFF

override_dh_auto_configure:
	dh_auto_configure -- $(OPTIONS)

override_dh_auto_test:

debian/depends.so: debian/shlibdeps-helper.c debian/rules
	$(CC) -shared $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wl,--no-as-needed \
		-o$@ $< \
		$$($(PKG_CONFIG) --cflags --libs gl)

debian/recommends.so: debian/shlibdeps-helper.c debian/rules
	$(CC) -shared $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wl,--no-as-needed \
		-o$@ $< \
		$$($(PKG_CONFIG) --cflags --libs libcurl)

execute_after_dh_shlibdeps-arch: debian/depends.so debian/recommends.so
	dh_shlibdeps -pioquake3 \
		-- \
		--warnings=1 -pdlopen \
		-dDepends -edebian/depends.so \
		-dRecommends -edebian/recommends.so \
		$(NULL)

ifeq ($(filter pkg.flatpak.app,$(DEB_BUILD_PROFILES)),)
ifeq ($(DEB_HOST_ARCH_OS),linux)
execute_after_dh_install-arch:
	dh_apparmor -pioquake3 --profile-name=usr.lib.ioquake3.ioquake3
	dh_apparmor -pioquake3-server --profile-name=usr.lib.ioquake3.ioq3ded
endif
endif

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog
