#!/usr/bin/make -f

DH_VERBOSE=1

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

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

# Explicitly initialize a variable to select architecture, unless it has been
# defined before.  This is compared against the DISABLE_*_LIST variables later
# in this makefile
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

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

# Extract VMOD_ABI version, this is used for building vmods using the official
# VMOD API
VMOD_ABI = $(shell printf '\#include "vrt.h"\nvarnishabi- VRT_MAJOR_VERSION . VRT_MINOR_VERSION\n' \
	   | cpp - -Iinclude \
	   | sed -e '/^varnishabi-/!d' -e 's/U//g' -e 's/ //g')

# Extract VMOD_strictABI, which refers to the exact build of varnish. This is
# used for old style vmods bypassing the VMOD API, accessing the internal data
# structures directly.
VMOD_strictABI = $(shell printf '\#include "vcs_version.h"\nVCS_Version\n' \
		 | cpp - -Iinclude \
		 | sed -e '/^"/!d' -e 's/\"//g' -e 's/^/varnishabi-strict-/')


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 $@

ifeq (,$(filter test,$(LOCAL_BUILD_OPTIONS)))
# Disable automated build tests
override_dh_auto_test:
endif

# Override to add local configure flags
override_dh_auto_configure:
	dh_auto_configure -- $(LOCAL_CONFIGURE_FLAGS)

override_dh_auto_install:
	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

override_dh_auto_install-arch:
	dh_auto_install -a
	rm -v debian/tmp/usr/share/doc/varnish/builtin.vcl
	rm -v debian/tmp/usr/share/doc/varnish/example.vcl

override_dh_install-arch:
	install -d debian/tmp/etc/varnish
	install -T -m 0644 etc/example.vcl debian/tmp/etc/varnish/default.vcl
	dh_install -a
	@ # Override to add the "reload-vcl" script
	install -o root -g root -m 755 \
		$(CURDIR)/debian/reload-vcl \
		$(CURDIR)/debian/varnish/usr/share/varnish/reload-vcl

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog doc/changes.html

override_dh_installdocs:
	dh_installdocs
	@ # Use the packaged javascript libraries
	if [ -d $(CURDIR)/debian/varnish-doc ]; then \
		rm -f $(CURDIR)/debian/varnish-doc/usr/share/doc/varnish-doc/html/_static/jquery.js; \
		ln -s /usr/share/javascript/jquery/jquery.js \
			$(CURDIR)/debian/varnish-doc/usr/share/doc/varnish-doc/html/_static/jquery.js; \
		rm -f $(CURDIR)/debian/varnish-doc/usr/share/doc/varnish-doc/html/_static/underscore.js; \
		ln -s /usr/share/javascript/underscore/underscore.js \
			$(CURDIR)/debian/varnish-doc/usr/share/doc/varnish-doc/html/_static/underscore.js; \
	fi

# Override to add several init scripts
override_dh_installinit:
	dh_installinit -a
	dh_installinit --name=varnishlog
	dh_installinit --name=varnishncsa

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

override_dh_gencontrol:
	echo "Varnish:ABI=$(VMOD_ABI)"             >> debian/varnish.substvars
	echo "Varnish:strictABI=$(VMOD_strictABI)" >> debian/varnish.substvars
	dh_gencontrol

override_dh_strip:
	dh_strip  --dbgsym-migration='varnish-dbg (<< 4.1.2-4~)'
