#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export SDL_AUDIODRIVER = dummy
export SDL_VIDEODRIVER = dummy

include /usr/share/dpkg/default.mk

built_binaries := $(shell dh_listpackages)
configure_options := \
	-DSDL2COMPAT_STATIC=ON \
	-DSDL2COMPAT_VENDOR_INFO="$(DEB_VENDOR) $(DEB_VERSION)" \
	-DSDL2COMPAT_WERROR=OFF \
	$(NULL)

ifneq ($(filter nocheck,$(DEB_BUILD_OPTIONS))$(filter %-tests,$(built_binaries)),nocheck)
configure_options += -DSDL2COMPAT_TESTS=ON
else
configure_options += -DSDL2COMPAT_TESTS=OFF
endif

ifneq ($(filter %-tests,$(built_binaries)),)
configure_options += -DSDL2COMPAT_INSTALL_TESTS=ON
else
configure_options += -DSDL2COMPAT_INSTALL_TESTS=OFF
endif

%:
	dh $@ -Scmake+ninja -Bcmake-build

override_dh_auto_configure:
ifeq ($(DEB_VERSION_UPSTREAM),2.32.56+git20251029~ee7cf25+ds)
	echo release-2.32.56-63-gee7cf25 > REVISION.txt
endif
	dh_auto_configure -- $(configure_options)

# Only run testtimer as an autopkgtest on machines that we're reasonably
# sure are both fast and set up to be usable for interactive use
# (currently only x86 and 64-bit ARM).
# There is an assertion that requesting a 1ms delay does not result in
# a delay of more than 100ms, and that's not going to be reliably true
# on slow machines like riscv64 or batch-processing-oriented machines
# like s390x.
ifeq (,$(filter amd64 arm64 i386,$(DEB_HOST_ARCH)))
execute_after_dh_auto_install:
	rm -fv debian/tmp/usr/share/installed-tests/SDL2_compat/testtimer.test
endif

override_dh_strip:
	dh_strip -plibsdl2-compat-shim --no-automatic-dbgsym
	dh_strip --remaining-packages

override_dh_makeshlibs:
	dh_makeshlibs -plibsdl2-compat -n
	dh_makeshlibs --remaining-packages
	library=$$(cd debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) && ls -1 libSDL2-2.0.so.0.*) && \
	ln -fns sdl2-compat/$$library debian/libsdl2-compat-shim/usr/lib/$(DEB_HOST_MULTIARCH)/
	ln -fns sdl2-compat/libSDL2-2.0.so.0 debian/libsdl2-compat-shim/usr/lib/$(DEB_HOST_MULTIARCH)/

override_dh_shlibdeps:
	dh_shlibdeps -lusr/lib/$(DEB_HOST_MULTIARCH)/sdl2-compat

# debhelper >= 13.4 makes all of /usr/libexec executable, which is not
# quite right for installed-tests
override_dh_fixperms:
	dh_fixperms -Xusr/libexec/installed-tests
	chmod --recursive a+rX,u+w,og-w debian/*-tests/usr/libexec/installed-tests
