#!/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

LOCAL_CONFIGURE_FLAGS :=

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/vinyl-cache/example.vcl
	rm -v debian/tmp/usr/bin/vinylstat_help_gen
	@ # compatibility files to be able to build Varnish modules
	perl -pE 's/\bVINYLAPI\b/VARNISHAPI/g; s/\bVINYLAPI_/VARNISHAPI_/g; s/\bVINYL_/VARNISH_/g; s/\b_VINYL_/_VARNISH_/g; ' \
		< debian/tmp/usr/share/aclocal/vinyl.m4 > debian/tmp/usr/share/aclocal/varnish.m4
	perl -pE 's/\bVINYLAPI\b/VARNISHAPI/g; s/\bVINYLAPI_/VARNISHAPI_/g; s/\bVINYL_/VARNISH_/g; s/\b_VINYL_/_VARNISH_/g; ' \
		< debian/tmp/usr/share/aclocal/vinyl-legacy.m4 > debian/tmp/usr/share/aclocal/varnish-legacy.m4

execute_after_dh_install-arch:
	install -T -m 0644 etc/example.vcl debian/vinyl-cache/etc/vinyl-cache/default.vcl
	install -m 755 \
		debian/vinylreload \
		debian/vinyl-cache/usr/share/vinyl-cache/vinylreload

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog doc/changes.html

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

override_dh_installdocs:
	dh_installdocs -pvinyl-cache -plibvinylapi-dev --link-doc=libvinylapi3
	dh_installdocs -plibvinylapi3
	dh_installdocs -pvinyl-cache-doc --doc-main-package=vinyl-cache-doc

execute_after_dh_installdocs-indep:
#	Use the packaged javascript libraries
	cd debian/vinyl-cache-doc/usr/share/doc/vinyl-cache-doc/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=vinyl-cache --name=vinylncsa --no-enable

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

override_dh_makeshlibs:
	dh_makeshlibs -VUpstream-Version -- -c4

override_dh_compress:
	dh_compress -X/usr/share/doc/vinyl-cache-doc/html

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

