#!/usr/bin/make -f

# Ensure documentation builds do not vary based on locale
export LC_ALL = C.UTF-8

# LTO needs to be disabled until it will be compatible with jemalloc
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk

# List of architectures where jemalloc is not available
DISABLE_JEMALLOC_ARCH_LIST := hppa m68k

# List of architectures where tests are not expected to fail
RELIABLE_TESTS_ARCH_LIST := amd64 arm64 loong64 ppc64el s390x

# Set local state dir for FHS
LOCAL_CONFIGURE_FLAGS = --localstatedir=/var/lib

ifneq ($(filter $(DEB_HOST_ARCH),$(DISABLE_JEMALLOC_ARCH_LIST)),)
LOCAL_CONFIGURE_FLAGS += --disable-jemalloc
endif

# Fix FTBFS on i386:
# https://github.com/varnishcache/varnish-cache/issues/1875
CFLAGS += -fexcess-precision=standard

# Main build rule, leave everything to debhelper
%:
	dh $@

override_dh_auto_test:
	# ignore the blhc false positives (see $VCC_CC)
	@echo 'blhc: ignore-line-regexp: .+"exec .+-gcc .+ -o %o %s" .+'
ifneq (, $(filter $(DEB_HOST_ARCH), $(RELIABLE_TESTS_ARCH_LIST)))
	dh_auto_test
else
	dh_auto_test || true
endif

# Override to add local configure flags
override_dh_auto_configure:
	VCC_GCC="$(DEB_HOST_GNU_TYPE)-gcc" \
	VCC_CFLAGS="$(shell DEB_BUILD_MAINT_OPTIONS='$(DEB_BUILD_MAINT_OPTIONS) reproducible=-all' dpkg-buildflags --get CFLAGS)" \
	dh_auto_configure -- $(LOCAL_CONFIGURE_FLAGS)

execute_after_dh_auto_build:
	$(MAKE) html
	cd doc && $(MAKE) changes.html

execute_after_dh_auto_install:
	@ # Remove .la files
	@ # (See http://wiki.debian.org/ReleaseGoals/LAFileRemoval)
	find $(CURDIR)/debian/ -name \*.la -delete
	@ # Nuke the static libs too
	find $(CURDIR)/debian -name \*.a -delete
	rm -v debian/tmp/usr/share/doc/varnish/example.vcl
	rm -v debian/tmp/usr/bin/varnishstat_help_gen

execute_after_dh_install-arch:
	install -T -m 0644 etc/example.vcl debian/varnish/etc/varnish/default.vcl
	install -m 755 \
		debian/varnishreload \
		debian/varnish/usr/share/varnish/varnishreload

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog doc/changes.html

execute_after_dh_installman:
	mkdir -p debian/libvarnishapi-dev/usr/share/man/man1/
	pod2man --section=1p --utf8 debian/extra/dh_varnishabi \
		debian/libvarnishapi-dev/usr/share/man/man1/dh_varnishabi.1p

execute_after_dh_installdocs-indep:
#	Use the packaged javascript libraries
	cd debian/varnish-doc/usr/share/doc/varnish/html/_static/ && \
	ln -vsf /usr/share/javascript/jquery/jquery.js \
		/usr/share/javascript/sphinxdoc/1.0/doctools.js \
		/usr/share/javascript/sphinxdoc/1.0/language_data.js \
		/usr/share/javascript/sphinxdoc/1.0/searchtools.js \
		/usr/share/javascript/underscore/underscore.js \
		.

execute_after_dh_installsystemd:
	dh_installsystemd --package=varnish --name=varnishncsa --no-enable

# this is a workaround for https://github.com/systemd/systemd/issues/33547
execute_after_dh_installsysusers:
	dh_installsysusers --name=varnish2

override_dh_makeshlibs:
	dh_makeshlibs -VUpstream-Version -- -c4

override_dh_compress:
	dh_compress -X/usr/share/doc/varnish-doc/html

override_dh_gencontrol:
	perl debian/extra/dh_varnishabi
	dh_gencontrol -- \
		-V"Varnish:gcc=$(shell echo gcc-$(DEB_HOST_GNU_TYPE) | sed -e 's/_/-/g')"

