#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
# export DH_VERBOSE = 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

mandir   := debian/varnish-modules/usr/share/man/man3
manpages = $(patsubst docs/%.man.rst,$(mandir)/%.3,$(rstfiles))
rstfiles := $(filter docs/%.man.rst,$(wildcard docs/*.rst))

# Workaround for a breaking change in GNU make 4.3.
H := \#

# Extract varnishd VRT version
VARNISHD_VRT = $(shell \
	printf '$(H)include "vdef.h"\n$(H)include "vrt.h"\n%s.%s\n' \
		VRT_MAJOR_VERSION VRT_MINOR_VERSION | \
	cpp - -I/usr/include/varnish | \
	tail -1 | \
	tr -c -d '[0-9.]')

%:
	dh $@

override_dh_auto_install:
	dh_auto_install
	# Empty dependency_libs field in .la files (c.f. Policy 10.2).
	sed -i "/dependency_libs/ s/'.*'/''/" `find $(installdir) -name '*.la'`

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	LD_PRELOAD=libnss_wrapper.so NSS_WRAPPER_HOSTS=../debian/varnishtest_nss_wrapper_hosts make check
endif

override_dh_gencontrol:
	echo "Varnish:ABI=varnishabi-$(VARNISHD_VRT)" >> debian/varnish-modules.substvars
	dh_gencontrol

override_dh_installman: $(manpages)
	dh_installman

$(mandir)/%.3: docs/%.rst
	rst2man --report=error $< $@
