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

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic -fno-omit-frame-pointer
# package maintainers to append LDFLAGS
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

export ARCH=$(DEB_HOST_GNU_CPU)

MAKEVARS := ARCH=$(DEB_HOST_GNU_CPU) PREFIX=/usr MODULEDIR=/usr/lib/knot-resolver ETCDIR=/etc/knot-resolver V=1

RUN_TESTS := yes
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),hurd-i386 mips mipsel ppc64 ppc64el armel armhf arm64 kfreebsd-amd64 kfreebsd-i386))
  $(warning Disabling checks on $(DEB_HOST_ARCH))
  RUN_TESTS := no
endif

# main packaging script based on dh7 syntax
%:
	dh $@ --with systemd --fail-missing

override_dh_auto_clean:
	dh_auto_clean -- clean

override_dh_auto_clean-indep:
	dh_auto_clean -- doc-clean http-clean

override_dh_auto_build-arch:
	CFLAGS="$${CFLAGS} -DNDEBUG" dh_auto_build -- $(MAKEVARS) BUILDMODE=static lib
	CFLAGS="$${CFLAGS} -DNDEBUG" dh_auto_build -- $(MAKEVARS)

override_dh_auto_build-indep:
	dh_auto_build -- $(MAKEVARS) http
	dh_auto_build -- $(MAKEVARS) doc-doxygen
	cd doc && sphinx-build -b html -D html_theme=nature . html

override_dh_auto_install-arch:
	dh_auto_install --destdir=debian/tmp -- $(MAKEVARS)

# make the modules non-executable
	chmod -x debian/tmp/usr/lib/knot-resolver/*.lua
	chmod -x debian/tmp/usr/lib/knot-resolver/*.so

# install custom config file
	install -d -m 755 debian/tmp/etc/knot-resolver/
	install -m 644 debian/kresd.conf debian/tmp/etc/knot-resolver/
	install -d -m 755 debian/tmp/usr/lib/knot-resolver/
	install -m 755 debian/init-d-script debian/tmp/usr/lib/knot-resolver/

override_dh_auto_install-indep:
# install just the http/2 module
	make http-install DESTDIR=debian/tmp $(MAKEVARS)

# make the modules non-executable
	chmod -x debian/tmp/usr/lib/knot-resolver/http/*

# tidy up http/2 module
	rm debian/tmp/usr/lib/knot-resolver/http/LICENSE
	rm debian/tmp/usr/lib/knot-resolver/http/jquery.js
	ln -s /usr/share/javascript/jquery/jquery.min.js debian/tmp/usr/lib/knot-resolver/http/jquery.js
	rm debian/tmp/usr/lib/knot-resolver/http/d3.js
	ln -s /usr/share/javascript/d3/d3.min.js debian/tmp/usr/lib/knot-resolver/http/d3.js
	rm debian/tmp/usr/lib/knot-resolver/http/bootstrap.min.js
	ln -s /usr/share/javascript/bootstrap/js/bootstrap.min.js debian/tmp/usr/lib/knot-resolver/http/bootstrap.min.js
	rm debian/tmp/usr/lib/knot-resolver/http/bootstrap.min.css
	ln -s /usr/share/javascript/bootstrap/css/bootstrap.min.css debian/tmp/usr/lib/knot-resolver/http/bootstrap.min.css
	rm debian/tmp/usr/lib/knot-resolver/http/bootstrap-theme.min.css
	ln -s /usr/share/javascript/bootstrap/css/bootstrap-theme.min.css debian/tmp/usr/lib/knot-resolver/http/bootstrap-theme.min.css

override_dh_installinit:
	install -d -m 755 debian/knot-resolver/lib/systemd/system/
	install -m 644 systemd/kresd.service debian/knot-resolver/lib/systemd/system/
	install -m 644 systemd/kresd.socket debian/knot-resolver/lib/systemd/system/
	install -m 644 systemd/kresd-control.socket debian/knot-resolver/lib/systemd/system/
	install -m 644 systemd/kresd-tls.socket debian/knot-resolver/lib/systemd/system/
	install -d -m 755 debian/knot-resolver/usr/lib/tmpfiles.d/
	install -m 644 systemd/tmpfiles/kresd.conf debian/knot-resolver/usr/lib/tmpfiles.d/
	dh_installinit -pknot-resolver --name=kresd --no-start

# Due to socket activation we are doing the dh_systemd_* stuff by hand
override_dh_systemd_enable:
override_dh_systemd_start:

override_dh_auto_test-indep:
override_dh_auto_test-arch:
ifeq ($(RUN_TESTS),yes)
	LD_LIBRARY_PATH=$(CURDIR)/lib dh_auto_test -- $(MAKEVARS)
endif

override_dh_strip:
	dh_strip -O--dbgsym-migration='knot-resolver-dbg (<< 1.0.0~beta3-60-ge61c48e-1~)'
