#!/usr/bin/make -f
# debian/rules for the dpkg suite.
# Copyright © 2004 Scott James Remnant <scott@netsplit.com>
# Copyright © 2006-2012 Guillem Jover <guillem@debian.org>

WFLAGS := \
	-Wall -Wextra \
	-Wno-missing-field-initializers \
	-Wno-nonnull-compare \
	-Wno-unused-parameter \
	$(nil)

# Use the in-tree dpkg-buildflags
dpkg_buildflags = \
	DEB_BUILD_MAINT_OPTIONS="hardening=+all" \
	DEB_CFLAGS_MAINT_APPEND="$(WFLAGS)" \
	DEB_CXXFLAGS_MAINT_APPEND="$(WFLAGS)" \
	$(CURDIR)/run-script scripts/dpkg-buildflags.pl

DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Support cross-compiling.
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build=$(DEB_HOST_GNU_TYPE)
else
	confflags += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

# Do not enable everything on all platforms.
ifeq ($(DEB_HOST_ARCH_OS),linux)
	confflags += --with-libselinux
endif
ifeq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
	confflags += --disable-silent-rules
	testflags += TESTSUITEFLAGS=--verbose
endif

# Enable parallel test suite
NUMJOBS = 1
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  MAKEFLAGS += -j$(NUMJOBS)
endif

D := $(CURDIR)/debian/tmp

# Create configure script if necessary, automake handles rebuilding it.
configure:
	dh_testdir

	./autogen

# Configure the build tree
build-tree/config.status: configure
	dh_testdir
	dh_autoreconf

	install -d build-tree
	cd build-tree && ../configure $(confflags) \
		$(shell $(dpkg_buildflags) --export=configure) \
		--prefix=/usr \
		--mandir=\$${datadir}/man \
		--infodir=\$${datadir}/info \
		--sysconfdir=/etc \
		--sbindir=/sbin \
		--localstatedir=/var \
		--libexecdir=\$${exec_prefix}/lib \
		--with-devlibdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--without-libmd \
		--with-libz \
		--with-liblzma \
		--with-libbz2

# Build the package in build-tree
build-indep build-arch build: build-tree/config.status
	dh_testdir

	cd build-tree && $(MAKE)

# Run the test suites
check: build
	dh_testdir

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd build-tree && $(MAKE) $(testflags) TEST_PARALLEL=$(NUMJOBS) check
endif

# Install the package underneath debian/tmp
install: check
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	cd build-tree && $(MAKE) DESTDIR=$(D) install

	# Special-case the lintian profile, as dh cannot rename on install.
	mkdir -p $(D)/usr/share/lintian/profiles/dpkg
	cp debian/dpkg.lintian-profile \
	   $(D)/usr/share/lintian/profiles/dpkg/main.profile

define dpkg-installmanl10n
for f in `sed -e 's:\*:*/*:' debian/$(1).manpages`; do \
	test -e $$f && \
	install -D $$f `echo $$f | sed -e 's:^debian/tmp:debian/$(1):'`; \
done
endef

# Put together the dpkg and dselect packages
binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_install -a
	dh_installcron -a
	dh_installlogrotate -a
	dh_installlogrotate -a --name=alternatives
	dh_installchangelogs -a ChangeLog*
	dh_installdocs -a
	$(call dpkg-installmanl10n,dpkg)
	$(call dpkg-installmanl10n,dselect)
	dh_installman -a
	dh_link -a
	dh_bugfiles -a -A
	dh_lintian -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# Put together the dpkg-dev package
binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_install -i
	dh_installcron -i
	dh_installchangelogs -i ChangeLog*
	dh_installdocs -i
	$(call dpkg-installmanl10n,dpkg-dev)
	dh_installman -i
	dh_link -i
	dh_bugfiles -i -A
	dh_lintian -i
	# The perl modules should not depend on a specific interpreter.
	dh_perl -i -Nlibdpkg-perl
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary: binary-arch binary-indep


# Clean up the mess we made
clean:
	dh_testdir

	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf build-tree
	dh_clean


.PHONY: build check install binary-arch binary-indep binary clean
