#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1 -Wl,-z,defs -Wl,--as-needed
export DEB_CFLAGS_MAINT_APPEND = -Wall
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

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

VALAVER = $(shell echo '$(DEB_VERSION_UPSTREAM)' | sed -e 's/\([0-9]\+\.[0-9]\+\).*/\1/')


%:
	dh $@ --with gnome

override_dh_clean:
	rm -rf bootstrap
	dh_clean

override_dh_autoreconf:
	# handle timestamp skew
	touch vala/vala.vala.stamp
	dh_autoreconf --as-needed

configure-bootstrap:
	dh_auto_configure --builddirectory=bootstrap/build

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

override_dh_auto_configure: bootstrap
	find -name '*.vala.stamp' -delete
	LD_LIBRARY_PATH="$(CURDIR)/bootstrap/install/usr/lib/$(DEB_HOST_MULTIARCH):$(CURDIR)/bootstrap/install/usr/lib/$(DEB_HOST_MULTIARCH)/vala-$(VALAVER):$$LD_LIBRARY_PATH" \
		dh_auto_configure -- \
			VALAC="$(CURDIR)/bootstrap/install/usr/bin/valac" \
			--enable-unversioned

override_dh_auto_build:
	LD_LIBRARY_PATH="$(CURDIR)/bootstrap/install/usr/lib/$(DEB_HOST_MULTIARCH):$(CURDIR)/bootstrap/install/usr/lib/$(DEB_HOST_MULTIARCH)/vala-$(VALAVER):$$LD_LIBRARY_PATH" \
		dh_auto_build

# 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

override_dh_auto_test:
ifeq ($(filter $(DEB_BUILD_ARCH),$(NO_TESTSUITE_ARCHS)),$(DEB_BUILD_ARCH))
	# do not run the tests under fakeroot, that breaks D-Bus
	-LD_PRELOAD= dh_auto_test
else
	LD_PRELOAD= dh_auto_test
endif

override_dh_auto_install:
	dh_auto_install
	find debian -name '*.la' -print -delete

override_dh_install-arch:
	dh_install
	printf '#!/bin/sh\nexec valac-$(VALAVER) "--cc=$${CC:-${DEB_HOST_GNU_TYPE}-gcc}" "--pkg-config=$${PKG_CONFIG:-${DEB_HOST_GNU_TYPE}-pkg-config}" "$$@"\n' > debian/valac/usr/bin/${DEB_HOST_GNU_TYPE}-valac-$(VALAVER)
	chmod 755 debian/valac/usr/bin/${DEB_HOST_GNU_TYPE}-valac-$(VALAVER)
	ln -s valac-$(VALAVER).1.gz debian/valac/usr/share/man/man1/${DEB_HOST_GNU_TYPE}-valac-$(VALAVER).1.gz
	ln -s ${DEB_HOST_GNU_TYPE}-valac-$(VALAVER) debian/valac/usr/bin/${DEB_HOST_GNU_TYPE}-valac
	ln -s ${DEB_HOST_GNU_TYPE}-valac-$(VALAVER).1.gz debian/valac/usr/share/man/man1/${DEB_HOST_GNU_TYPE}-valac.1.gz

override_dh_missing:
	dh_missing --fail-missing

override_dh_installdocs:
	dh_installdocs -A NEWS README THANKS

override_dh_makeshlibs:
	dh_makeshlibs -- -c4
