#!/usr/bin/make -f

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

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

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

#{{{ generic rules

../$(srcpkg)_$(srcver).orig.tar.gz:
	@if git rev-parse --git-dir >/dev/null 2>&1; then                 \
	    pristine-tar checkout $@;                                     \
	fi

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

.PHONY: check-tarball

#}}}
#{{{ maintainer stuff

refresh-patches: check-tarball
	@dh_testdir
	@echo 'refreshing debian/patches:'
	@rm -rf '$(CURDIR)'/debian/patches
	@mkdir -p '$(CURDIR)'/debian/patches
	@cd '$(CURDIR)'/debian/patches && git format-patch upstream..upstream+patches
	@cd debian/patches && ls *.patch > debian/patches/series
	@echo .

#}}}

%:
	dh $@ --parallel

override_dh_auto_configure: check-tarball
	dh_auto_configure -- \
	    $(if $(findstring $(DEB_BUILD_ARCH),s390),--disable-pthread) \
	    --enable-devel --enable-off64 --enable-swab \
	    \
	    CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"

override_dh_auto_test:
	$(if $(findstring $(DEB_BUILD_ARCH),armel)$(findstring nocheck,$(DEB_BUILD_OPTIONS)),,$(MAKE) check)

override_dh_shlibdeps:
	dh_shlibdeps -L$(libpkg) -ldebian/$(libpkg)/usr/lib

clean:
	dh $@
	! test -e Makefile || $(MAKE) distclean
	rm -f build-*-stamp

override_dh_strip:
	dh_strip --dbg-package=libtokyocabinet-dbg

