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

ifndef PERL
PERL := /usr/bin/perl
endif

-include /usr/share/dpkg/buildflags.mk

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

D := $(CURDIR)/debian/libberkeleydb-perl


clean:
	dh_testdir

	[ ! -f Makefile ] || $(MAKE) distclean

	dh_clean

Makefile: Makefile.PL
	$(PERL) Makefile.PL INSTALLDIRS=vendor

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: Makefile
	dh_testdir

	make OPTIMIZE="$(CFLAGS) $(CPPFLAGS)" LD="cc $(CFLAGS) $(LDFLAGS)"
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),amd64 i386))
	make test
else
	# sorry, I wasted too much time because of buildds with broken toolchains
	$(warning Tests disabled for this architecture.)
endif

	touch $@

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep

	$(MAKE) install DESTDIR=$D
	[ ! -d $D/usr/share/perl5/ ] \
	  || rmdir --ignore-fail-on-non-empty --parents $D/usr/share/perl5/
	rm $D/usr/lib/perl5/mkconsts.pl $D/usr/lib/perl5/scan.pl

	touch $@

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs Changes
	dh_installdocs
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_builddeb

binary-indep:

binary: binary-indep binary-arch

get-orig-source:
	cd $(dir $(firstword $(MAKEFILE_LIST)))../ && \
	uscan --rename --force-download --watchfile debian/watch --destdir $(CURDIR)

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