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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/buildopts.mk
# 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

CFLAGS += -g -Wall
V = 1
WAF = PYTHONHASHSEED=1 ./buildtools/bin/waf \
	$(if ${DEB_BUILD_OPTION_PARALLEL},-j${DEB_BUILD_OPTION_PARALLEL})

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:
	LDFLAGS="${LDFLAGS}" CFLAGS="${CFLAGS}" CPPFLAGS="${CPPFLAGS}" \
	    ${WAF} -j1 configure -C \
		--prefix=/usr \
		--libdir=/usr/lib/${DEB_HOST_MULTIARCH} \
		--disable-rpath-install --disable-rpath \
		--bundled-libraries=NONE \
	    && touch $@

override_dh_auto_build: bin/config-stamp
	${WAF} $(if $V, -v) build

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}
	# Waf really should be doing this:
	ar -rc libtalloc.a bin/default/talloc*.o
	mv 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:
	# also debian/clean
	dh_clean bin/

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