#!/usr/bin/make -f
SHELL = /bin/sh -e

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -g -Wall
export DPKG_GENSYMBOLS_CHECK_LEVEL = 4

# Fast version of dpkg/architecture.mk defining all vars in one go
ifeq (${DEB_HOST_MULTIARCH},)
   $(foreach d, $(shell dpkg-architecture | sed 's/=/?=/'), $(eval export $d))
endif
include /usr/share/dpkg/buildflags.mk
V = 1
WAF = PYTHONHASHSEED=1 ./buildtools/bin/waf \
	$(if $(filter parallel=%,${DEB_BUILD_OPTIONS}),\
	  $(patsubst parallel=%,-j%,$(filter parallel=%,${DEB_BUILD_OPTIONS})))

DESTDIR = ${CURDIR}/debian/tmp

build-arch build-indep build \
install-indep install-arch install \
binary-arch binary-indep binary \
: %:
	dh $@

override_dh_auto_configure: bin/config-stamp
bin/config-stamp:
	CPPFLAGS="${CPPFLAGS}" CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" \
	    ${WAF} -j1 -C configure \
		--prefix=/usr \
		--libdir=/usr/lib/${DEB_HOST_MULTIARCH} \
		--disable-rpath --disable-rpath-install \
		--bundled-libraries=NONE \
	    && touch $@

override_dh_auto_build: bin/config-stamp
	${WAF} build $(if $V,-v)
	# Waf should be doing this..
	ar cr bin/libtdb.a bin/default/common/*.o

override_dh_auto_test:
ifeq (,$(filter nocheck,${DEB_BUILD_OPTIONS}))
	${WAF} test
endif

override_dh_auto_install:
	${WAF} install --destdir=${DESTDIR}
	cp bin/libtdb.a ${DESTDIR}/usr/lib/${DEB_HOST_MULTIARCH}
	mv ${DESTDIR}/usr/bin/tdbbackup \
		${DESTDIR}/usr/bin/tdbbackup.tdbtools
	mv ${DESTDIR}/usr/share/man/man8/tdbbackup.8 \
		${DESTDIR}/usr/share/man/man8/tdbbackup.tdbtools.8
	#HACK alert: fix up wrong markers in the common include file
	sed -i 's/^_PUBLIC_ //' ${DESTDIR}/usr/include/tdb.h
	# ..and remove the #define itself
	patch --batch ${DESTDIR}/usr/include/tdb.h debian/header-remove-_PUBLIC_-define.patch

clean:
	# see also debian/clean
	dh_clean bin/

get-packaged-orig-source:
	./debian/build-orig.sh
