#!/usr/bin/make -f

# Temporarily disable LTO https://gitlab.gnome.org/GNOME/gimp/-/issues/9633
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1

ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
	BUGTRACKER = https://bugs.launchpad.net/ubuntu/+source/gimp/+filebug?no-redirect
else
	BUGTRACKER = https://www.debian.org/Bugs/Reporting
endif

built_binaries := $(shell dh_listpackages)

ifneq ($(filter %-doc,$(built_binaries)),)
	BUILD_DOCS := -Dgi-docgen=enabled -Dg-ir-doc=true
else
	BUILD_DOCS := -Dgi-docgen=disabled -Dg-ir-doc=false
endif

# Check Filters > Development > Plug-in Examples
# If this is enabled, a JavaScript version is installed but there is no menu button for it
# Therefore, we disable it
ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armhf i386 mips64el ppc64el riscv64 s390x loong64 ppc64 sparc64))
	JS = disabled
else
	JS = disabled
endif

# Upstream has marked the Lua plugins as experimental and disabled by default
# One of the plugins is an example like JavaScript but there is no menu button for it either
ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips64el riscv64 s390x loong64 powerpc))
	LUA = false
else
	LUA = false
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
		-Dlibexecdir=/usr/lib/$(DEB_HOST_MULTIARCH)/gimp \
		-Dauto_features=enabled \
		$(BUILD_DOCS) \
		-Dcheck-update=no \
		-Dappdata-test=disabled \
		-Dheadless-tests=disabled \
		-Djavascript=$(JS) \
		-Dlua=$(LUA) \
		-Dmng=disabled \
		-Dilbm=disabled \
		-Dbug-report-url=$(BUGTRACKER)

override_dh_makeshlibs:
	dh_makeshlibs -X/usr/lib/$(DEB_HOST_MULTIARCH)/gimp/3.0/modules -- -c4

# Give tests more time on slow architectures like riscv64
override_dh_auto_test:
	xvfb-run -s -noreset -a dh_auto_test --  --verbose --timeout-multiplier 5

# No dbgsym packages:
override_dh_strip:
	@echo "NO--THANKS ANYWAY--but not building dbgsym packages for MX Linux"
	dh_strip --no-automatic-dbgsym
