#!/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 rc bin/libtalloc.a bin/default/talloc*.o

override_dh_auto_test:
ifeq (,$(filter nocheck,${DEB_BUILD_OPTIONS}))
	LD_LIBRARY_PATH=bin/shared ./bin/talloc_testsuite
endif

override_dh_auto_install:
	${WAF} install --destdir=${DESTDIR}
	cp bin/libtalloc.a ${DESTDIR}/usr/lib/${DEB_HOST_MULTIARCH}/
	#HACK alert: fix up wrong markers in the common include file
	sed -i 's/^_PUBLIC_ //' \
		${DESTDIR}/usr/include/talloc.h \
		${DESTDIR}/usr/include/pytalloc.h
	# ..and remove the #define itself
	patch --batch ${DESTDIR}/usr/include/talloc.h debian/header-remove-_PUBLIC_-define.patch

override_dh_makeshlibs:
	dh_makeshlibs -plibtalloc2 -- -c4
	{ \
	    debver=$$(dpkg-parsechangelog -SVersion | sed 's/-.*//'); \
	    suff=$$(python3-config --extension-suffix | tr _ -); \
	    SUFF=$$(echo "$${suff%.so}" | tr a-z- A-Z_)_$${debver}; \
	    echo "libpytalloc-util$${suff}.2 python3-talloc #MINVER#"; \
	    echo " PYTALLOC_UTIL$${SUFF}@PYTALLOC_UTIL$${SUFF} $${debver}"; \
	    cat debian/python3-talloc.symbols.in; \
	} > debian/python3-talloc.symbols
	dh_makeshlibs -ppython3-talloc -X/talloc.cpython -- -c3
	rm debian/python3-talloc.symbols

clean:
	# see also debian/clean
	dh_clean bin/

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