#!/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 := $(if $(filter terse, ${DEB_BUILD_OPTIONS}),,1)
WAF := PYTHONHASHSEED=1 ./buildtools/bin/waf \
	$(patsubst parallel=%,-j%,$(filter parallel=%,${DEB_BUILD_OPTIONS}))

DESTDIR := ${CURDIR}/debian/tmp

# All python versions we build for (--supported or --default)
export PYVERSIONS := $(or ${PYVERSIONS}, $(shell py3versions -v \
     $$(sed -e '/^$$/q' -e 's/#.*//' debian/control | grep -q python3-all-dev \
        && echo --supported || echo --default)))

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

# Build in bld/pyNN/ subdirs (named after python version),
# Since wafsamba can't build things in a named subdir,
# we have to copy (link) whole source dir into each build subdir.

.NOTPARALLEL: # don't run commands at this level in parallel

py-targets = $(patsubst %, bld/py%/$(strip $1), ${PYVERSIONS})

override_dh_auto_configure: $(call py-targets, stamp-config)
$(call py-targets, stamp-config): %/stamp-config:
	@echo "=== running configure in $* ==="
	mkdir -p $*
	cp -al $$(ls -1 | egrep -v '^(bld|debian)$$') $*
	cd $*; \
	    CPPFLAGS="${CPPFLAGS}" CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" \
	    PYTHON=python$(subst bld/py,,$*) \
	    ${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: $(call py-targets, stamp-build)
$(call py-targets, stamp-build): %/stamp-build: %/stamp-config
	@echo "=== running build in $* ==="
	cd $*; ${WAF} $(if $V,-v) build
	# Waf should be doing this..
	ar cr $*/bin/libtdb.a $*/bin/default/common/*.o

# the testsuite does not test python bindings so there's no reason to test all pyversions
override_dh_auto_test: \
  $(if $(filter nocheck,${DEB_BUILD_OPTIONS}), , $(firstword $(call py-targets, test)))
$(call py-targets, test): %/test: %/stamp-build
	@echo "=== running test in $* ==="
	cd $*; ${WAF} test

$(call py-targets, install): %/install: %/stamp-build
	@echo "=== running install in $* ==="
	cd $*; ${WAF} install --destdir=${DESTDIR}
	cp $*/bin/libtdb.a ${DESTDIR}/usr/lib/${DEB_HOST_MULTIARCH}/
override_dh_auto_install: $(call py-targets, install)
	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:
	dh_clean bld/

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