#!/usr/bin/make -f

CFLAGS = -g -Wall
SHLD_FLAGS = -shared -Wl,--as-needed

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif

LDFLAGS += -Wl,--as-needed

DESTDIR=$(CURDIR)/debian/tmp

configure: configure-stamp
configure-stamp:
	dh_testdir
	PYTHON=`which python` PYTHON_CONFIG=`which python-config` CFLAGS="$(CFLAGS)" ./configure --prefix=/usr --disable-rpath
	touch configure-stamp

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

build: configure build-stamp
build-stamp:
	dh_testdir
	$(MAKE) all SHLD_FLAGS="$(SHLD_FLAGS)"
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	LD_LIBRARY_PATH=bin/shared $(MAKE) check
endif
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf build
	rm -f buildtools/wafsamba/*.pyc
	rm -rf buildtools/bin/.waf*
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install DESTDIR=$(DESTDIR)
	ar cr libtdb.a bin/default/common/*.o
	cp libtdb.a $(CURDIR)/debian/tmp/usr/lib
	mv $(CURDIR)/debian/tmp/usr/bin/tdbbackup \
		$(CURDIR)/debian/tmp/usr/bin/tdbbackup.tdbtools
	mv $(CURDIR)/debian/tmp/usr/share/man/man8/tdbbackup.8 \
		$(CURDIR)/debian/tmp/usr/share/man/man8/tdbbackup.tdbtools.8
	dh_install --fail-missing --list-missing --sourcedir=$(DESTDIR)

binary-indep: 
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs 
	dh_installman
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -- -c4
	dh_pysupport
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install configure 
