#!/usr/bin/make -f

export DPKG_GENSYMBOLS_CHECK_LEVEL=4

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

NULL  =
PKD   = $(word 2, abspath $(dir $(MAKEFILE_LIST)))
PKG   = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
UVER  = $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\-\d+.*)};')
DTYPE = +repack1
VER  ?= $(subst $(DTYPE),,$(UVER))
REV   = $(shell echo $(VER) | perl -ne 'print "$$1" if m/(?:git|svn|hg|bzr)(\d+)/;')

LDFLAGS += -Wl,-z,defs -Wl,--as-needed

%:
	dh $@ --with autoreconf

override_dh_autoreconf:
	NOCONFIGURE=1 dh_autoreconf ./autogen.sh

override_dh_auto_configure:
	dh_auto_configure -- --disable-scrollkeeper

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog.bzr

override_dh_auto_test:
	VERBOSE=1 dh_auto_test

override_dh_install:
	find debian/ -name \*.la -delete
	find debian/ -name \*.a -delete
	dh_install --fail-missing

## http://wiki.debian.org/onlyjob/get-orig-source
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz $(info I: $(PKG)_$(VER)$(DTYPE))
	@

## checkout from bzr (add "bzr (>= 2.6.0~bzr6520)" to Build-Depends, see #666496)
UURL = lp:dbus-test-runner
$(PKG)_$(VER)$(DTYPE).orig.tar.xz:
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	bzr checkout --hardlink --lightweight --revision=$(REV) $(UURL) $(PKG)-$(VER) \
	    || $(RM) -r $(PKG)-$(VER)
	@echo "# Creating ChangeLog..."
	cd $(PKG)-$(VER) && bzr log --log-format=line > ChangeLog.bzr
	@echo "# Modifying..."
	cd $(PKG)-$(VER) && rm -Rfv \
	    .bzr-builddeb \
	    .bzr \
	    $(NULL)
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
	    | XZ_OPT="-6v" tar -caf "../$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	    && $(RM) -r "$(PKG)-$(VER)"
