#!/usr/bin/make -f

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

configure_flags += \
	--prefix=/usr \
	--mandir=\$${prefix}/share/man \
	--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--enable-unversioned \
	--disable-maintainer-mode \
	--disable-silent-rules

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
configure_flags += --host=$(DEB_HOST_GNU_TYPE)
endif

CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CFLAGS += -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif

LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)
LDFLAGS += -Wl,-z,defs -Wl,-O1 -Wl,--as-needed

DEB_PARALLEL_JOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
ifneq (,$(DEB_PARALLEL_JOBS))
DEB_MAKE_PARALLEL = -j$(DEB_PARALLEL_JOBS)
else
DEB_MAKE_PARALLEL =
endif

builddir-stamp:
	dh_testdir
	# handle timestamp skew
	touch vala/vala.vala.stamp
	# update build system
	dh_autoreconf --as-needed
	touch $@

configure-bootstrap-stamp: builddir-stamp
	dh_testdir
	mkdir -p bootstrap/build
	cd bootstrap/build && \
		../../configure CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" $(configure_flags)
	touch $@

bootstrap-stamp: configure-bootstrap-stamp
	dh_testdir
	make $(DEB_MAKE_PARALLEL) -C bootstrap/build
	make $(DEB_MAKE_PARALLEL) -C bootstrap/build DESTDIR=$(CURDIR)/bootstrap/install install
	touch $@

configure-stamp: builddir-stamp bootstrap-stamp
	find -name '*.vala.stamp' -delete
	LD_LIBRARY_PATH="$(CURDIR)/bootstrap/install/usr/lib/$(DEB_HOST_MULTIARCH):$$LD_LIBRARY_PATH" \
		./configure CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		VALAC="$(CURDIR)/bootstrap/install/usr/bin/valac" \
		$(configure_flags)
	touch $@

build-stamp: configure-stamp
	LD_LIBRARY_PATH="$(CURDIR)/bootstrap/install/usr/lib/$(DEB_HOST_MULTIARCH):$(CURDIR)/bootstrap/install/usr/lib/$(DEB_HOST_MULTIARCH)/vala-0.40:$$LD_LIBRARY_PATH" \
		make $(DEB_MAKE_PARALLEL)
	touch $@

# Make testsuite failures non-fatal for these architectures. The DBus
# tests are failing there and an yet unknown reason.
NO_TESTSUITE_ARCHS := kfreebsd-i386 kfreebsd-amd64 hurd-i386

check-stamp: build-stamp
	# do not run the tests under fakeroot, that breaks D-Bus
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
ifeq ($(filter $(DEB_BUILD_ARCH),$(NO_TESTSUITE_ARCHS)),$(DEB_BUILD_ARCH))
	-LD_PRELOAD= make $(DEB_MAKE_PARALLEL) check
else
	LD_PRELOAD= make $(DEB_MAKE_PARALLEL) check
endif
endif
	touch $@

install-stamp: build-stamp check-stamp
	LD_LIBRARY_PATH="$(CURDIR)/bootstrap/install/usr/lib/$(DEB_HOST_MULTIARCH):$$LD_LIBRARY_PATH" \
		make $(DEB_MAKE_PARALLEL) DESTDIR=$(CURDIR)/debian/tmp install
	touch $@

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
install: build install-stamp

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i
	dh_installdirs -i
	dh_installdocs -i -A NEWS README AUTHORS THANKS
	dh_installchangelogs -i ChangeLog
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install -a
	dh_installdirs -a
	dh_installdocs -a -A README NEWS AUTHORS THANKS
	dh_installchangelogs -a ChangeLog
	dh_installman -a
	dh_installexamples -a
	dh_lintian -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V -- -c4
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

clean:
	dh_testdir
	dh_testroot

	rm -rf bootstrap
	rm -rf debian/tmp
	make distclean || true

	rm -rf tests/_test
	dh_autoreconf_clean

	rm -f builddir-stamp configure-bootstrap-stamp bootstrap-stamp
	rm -f configure-stamp build-stamp check-stamp install-stamp
	dh_gnome_clean
	dh_clean

