#!/usr/bin/make -f

include /usr/share/quilt/quilt.make
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
CFLAGS += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	JOBS = -j1
endif

configure: configure-stamp
configure-stamp: patch
	dh_testdir
	[ -d build-static ] || mkdir build-static
	(cd build-static && LDFLAGS='-z relro' CXXFLAGS='-g -O2' \
		cmake -DCMAKE_INSTALL_PREFIX='/usr' -DBUILD_SHARED_LIBS=off -DINCLUDE_INSTALL_DIR='/usr/lib/ldc/${DEB_HOST_MULTIARCH}/include/d' ..)
	[ -d build-shared ] || mkdir build-shared
	(cd build-shared && LDFLAGS='-z relro' CXXFLAGS='-g -O2' \
		cmake -DCMAKE_INSTALL_PREFIX='/usr' -DBUILD_SHARED_LIBS=on -DINCLUDE_INSTALL_DIR='/usr/lib/ldc/${DEB_HOST_MULTIARCH}/include/d' ..)

	touch $@

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	(cd build-static && $(MAKE) VERBOSE=1 $(JOBS))
	(cd build-shared && $(MAKE) VERBOSE=1 $(JOBS))
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -rf build-static build-shared
	rm -f configure-stamp build-stamp
	dh_clean

install: build
	dh_testdir
	dh_prep
	(cd build-static && $(MAKE) install DESTDIR="$(CURDIR)/debian/tmp")
	(cd build-shared && $(MAKE) install DESTDIR="$(CURDIR)/debian/tmp")

binary-arch: build

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install
	dh_installdocs
	dh_installman debian/ldc2.1 debian/ldmd2.1
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -Llibphobos2-ldc71
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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