#!/usr/bin/make -f

# Some Debian build tools clear out some variables
export SHELL=/bin/bash

distrel := $(shell lsb_release --codename --short)

# Reduce memory usage of the linker at the expense of processing time
# This should help on lower-end architectures like arm and mips, which
# spend an immense amount of time swapping.
LDFLAGS += -Wl,--reduce-memory-overheads
LDFLAGS += -Wl,--no-keep-memory

# Add execution time and memory usage stats in the logs
LDFLAGS += -Wl,--stats

ifneq (,$(filter $(DEB_HOST_ARCH), armhf))
	export DEB_CFLAGS_MAINT_APPEND = -mfpu=neon -funsafe-math-optimizations
	export DEB_CXXFLAGS_MAINT_APPEND = -mfpu=neon -funsafe-math-optimizations
endif

ifneq (,$(filter $(distrel), buster  bullseye bookworm bionic focal jammy))
	MOZCONFIGFILE = debian/mozconfig
endif

# enabling jxl causes FTBFS on arm64 and gcc-10, and armhf for gcc-12
# ifneq (,$(filter $(distrel), bullseye))
# ifneq (,$(filter $(DEB_HOST_ARCH), arm64))
# 	MOZCONFIGFILE = debian/mozconfig.nojxl
# else
# 	MOZCONFIGFILE = debian/mozconfig
# endif
# endif

# ifneq (,$(filter $(distrel), kinetic bookworm sid))
# ifneq (,$(filter $(DEB_HOST_ARCH), armhf))
# 	MOZCONFIGFILE = debian/mozconfig.nojxl
# else
# 	MOZCONFIGFILE = debian/mozconfig
# endif
# endif

%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean
	find . -name '*.pyc' -delete
	rm -f mozconfig

override_dh_autoreconf:

override_dh_auto_configure:
	cp $(MOZCONFIGFILE) .

override_dh_auto_build:
	# building the stuff
	make -f client.mk build

override_dh_auto_install:
	make -f client.mk DESTDIR=$$(pwd)/debian/palemoon prefix=/usr \
        installdir=/usr/lib/palemoon \
        sdkdir=/usr/lib/palemoon-devel install
	rm -rf $$(pwd)/debian/palemoon/usr/share/idl
	rm -rf $$(pwd)/debian/palemoon/usr/lib/palemoon-devel
	rm -rf $$(pwd)/debian/palemoon/usr/include
# remove vestigial duplicate file
	rm -rf $$(pwd)/debian/palemoon/usr/lib/palemoon/palemoon-bin

override_dh_shlibdeps:
	dh_shlibdeps -l /usr/lib/palemoon

override_dh_strip_nondeterminism:
	dh_strip_nondeterminism -Xdebian/palemoon/usr/lib/palemoon/browser/extensions/{972ce4c6-7e08-4474-a285-3208198ce6fd}.xpi
