#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

srcpkg = $(shell dpkg-parsechangelog | sed -ne 's/Source: *//p')
srcver = $(shell dpkg-parsechangelog | sed -ne 's/Version: *\([0-9]*:\)\?\(.*\)-[^-]*$$/\2/p')
libpkg = $(shell  sed -ne 's/Package: *\(.*\)-dbg/\1/p' debian/control)
dbgpkg = $(libpkg)-dbg

CFLAGS = -g -Wall -Wextra $(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),-O0,-O2)
LDFLAGS= -Wl,-z,defs

#{{{ generic rules

../$(srcpkg)_$(srcver).orig.tar.gz:
	@if git rev-parse --git-dir >/dev/null 2>&1; then                 \
	    echo -n 'Regenerating $(@F)';                                 \
	    git show pristine-tar:$(@F).delta | pristine-tar gentar - $@; \
	    echo .;                                                       \
	fi

check-tarball: ../$(srcpkg)_$(srcver).orig.tar.gz

.PHONY: check-tarball

#}}}

config.status: configure
	dh_testdir
	dh_autoreconf
	./configure --prefix=/usr \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--disable-tests \
		--with-zeroconf \
		--enable-ipv6 \
		CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"

build: build-stamp
build-stamp: config.status
	@echo ">>> Starting compile (`date`)..."
	dh_testdir
	$(MAKE)
	make check
	(cd po && make update-gmo)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	# Remove the auto-generated files so that they do not enter the diff.
	dh_autoreconf_clean
	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_install --sourcedir=debian/tmp
	dh_link
	dh_strip --dbg-package=$(dbgpkg)
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -L$(libpkg) -ldebian/$(libpkg)/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

