#!/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

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 \
		--disable-python \
	    && touch $@

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

override_dh_auto_test:
ifeq (,$(filter nocheck,${DEB_BUILD_OPTIONS}))
# for some weird reason, salsa-ci run build with fd#0 *closed*(!) and the test fail
	${WAF} test </dev/null
endif

override_dh_auto_install:
	${WAF} install --destdir=${DESTDIR}
	cp bin/libtevent.a ${DESTDIR}/usr/lib/${DEB_HOST_MULTIARCH}

clean:
	# see also debian/clean
	dh_clean bin/ compile_commands.json

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