#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# To enable all, uncomment following line
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DPKG_VENDOR          ?= $(shell dpkg-vendor --query Vendor | tr [A-Z] [a-z])
DEB_HOST_ARCH        ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),hurd-i386))
  RECVMMSG:=--enable-recvmmsg=no
else
  RECVMMSG:=--enable-recvmmsg=yes
endif

DEB_BUILD_DISTRIBUTION = $(shell dpkg-parsechangelog | sed -ne 's/^Distribution: //p')

ENABLE_SYSTEMD = yes

ifeq (ubuntu,$(DPKG_VENDOR))
  ifeq ($(DEB_BUILD_DISTRIBUTION),$(filter $(DEB_BUILD_DISTRIBUTION),precise))
    $(warning Disabling systemd on $(DPKG_VENDOR) $(DEB_BUILD_DISTRIBUTION))
    ENABLE_SYSTEMD = no
  endif
endif

ifneq (linux,$(DEB_HOST_ARCH_OS))
  $(warning Disabling systemd on $(DEB_HOST_ARCH_OS))
  ENABLE_SYSTEMD = no
endif

ifeq (yes,$(ENABLE_SYSTEMD))
  DH_SYSTEMD = --with systemd
endif

%:
	dh $@ --with autotools-dev,autoreconf $(DH_SYSTEMD) --parallel

override_dh_auto_configure:
	dh_auto_configure -- \
	  --sysconfdir=/etc \
	  --localstatedir=/var/lib \
	  --libexecdir=/usr/lib/knot \
	  --with-rundir=/run/knot \
	  --with-storage=/var/lib/knot \
	  --enable-systemd=$(ENABLE_SYSTEMD) \
	  $(RECVMMSG) \
	  --disable-silent-rules

override_dh_auto_build-arch:
	dh_auto_build -- all

override_dh_auto_build-indep:
	mv debian/logo.pdf doc/logo.pdf
	dh_auto_build -- info pdf html
	rm doc/logo.pdf

override_dh_auto_install-arch:
	dh_auto_install

override_dh_auto_install-indep:
	dh_auto_install -- install-info install-pdf install-html

override_dh_auto_test:
	-dh_auto_test

override_dh_strip:
	dh_strip --dbg-package=knot-dbg

override_dh_install-arch:
	# Install sample conf as /etc/knot/knot.conf
	mv $(CURDIR)/debian/tmp/etc/knot/knot.sample.conf $(CURDIR)/debian/tmp/etc/knot/knot.conf
	# Remove useless example zone from /etc/knot
	rm $(CURDIR)/debian/tmp/etc/knot/example.com.zone
	# Install custom script which creates RUNDIR and chowns it according to conffile settings
	install -m 755 -d $(CURDIR)/debian/tmp/usr/lib/knot/
	install -m 755 $(CURDIR)/debian/prepare-environment $(CURDIR)/debian/tmp/usr/lib/knot/

	# Remove .la and .so
	rm $(CURDIR)/debian/tmp/usr/lib/*/*.la $(CURDIR)/debian/tmp/usr/lib/*/*.so

	dh_install -Nknot-doc --list-missing

	install -m 755 -d $(CURDIR)/debian/knot/etc/ufw/applications.d/
	install -m 644 $(CURDIR)/debian/knot.ufw $(CURDIR)/debian/knot/etc/ufw/applications.d/knot

override_dh_install-indep:
	dh_install -pknot-doc --list-missing

override_dh_installchangelogs:
	dh_installchangelogs NEWS
