#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -g -Wall
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

DESTDIR=$(CURDIR)/debian/tmp

include /usr/share/dpkg/architecture.mk

%:
	dh $@ --with python3

override_dh_auto_configure:
	CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		./configure --prefix=/usr --disable-rpath-install \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		-v

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

override_dh_auto_build:
	$(MAKE) all V=2

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) check
endif

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf build
	# Waf should be doing this; see
	# https://bugzilla.samba.org/show_bug.cgi?id=8854
	rm -f .lock-wscript
	# Waf should be doing this; see
	# https://bugzilla.samba.org/show_bug.cgi?id=8855
	rm -rf buildtools/wafsamba/__pycache__ \
	       third_party/waf/waflib/__pycache__/ \
	       third_party/waf/waflib/extras/__pycache__/ \
	       third_party/waf/waflib/Tools/__pycache__/

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(DESTDIR)
	# Waf should be doing this..
	ar cr libtdb.a bin/default/common/*.o
	mv 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

override_dh_missing:
	dh_missing --fail-missing

override_dh_makeshlibs:
	dh_makeshlibs -Npython3-tdb -- -c4
