#!/usr/bin/make -f

export CFLAGS := -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
export CXXFLAGS := $(CFLAGS)
export LDFLAGS := -Wl,--as-needed

ifneq (,$(findstring parallel,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += $(subst parallel=,-j,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
# cmake coloured output can get messy quickly...
export COLOR := off
endif

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

build-stamp: build/Makefile
	$(MAKE) -C build

build/Makefile :
	mkdir -p build
	cd build && cmake -D prefix=/usr ..

install-stamp : build
	dh_testroot
	$(MAKE) -C build DESTDIR=$$(pwd)/debian/tmp install
	touch install-stamp

binary : binary-arch binary-indep

binary-arch : install-stamp
	dh_install -a --sourcedir=debian/tmp
	dh_strip
	dh_shlibdeps
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a -A doc/README doc/examples
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-indep :

clean :
	rm -rf build
	dh_clean install-stamp

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