#!/usr/bin/make -f
control:debian/control

OUTDATED_CONTROL_IS_FATAL ?= 1 # set to "0" to build anyway

GCC_VER=$(shell dpkg-query -f '$${Version}' -W gcc | sed -e 's/^[0-9]://;s/-.*//')
THIS_VERSION=$(shell dpkg-parsechangelog -SVersion)

build:
build-arch:build
build-indep:build
binary-arch: PKGSEL=-a
binary-arch:binary
binary-indep: PKGSEL=-i
binary-indep:binary

binary:check-control
	dh_testdir $(PKGSEL)
	dh_testroot $(PKGSEL)
	dh_prep $(PKGSEL)
	dh_installdirs $(PKGSEL)
	dh_install $(PKGSEL)
	dh_installdocs $(PKGSEL) -A
	dh_installchangelogs $(PKGSEL)
	dh_compress $(PKGSEL)
	dh_fixperms $(PKGSEL)
	dh_installdeb $(PKGSEL)
	dh_gencontrol $(PKGSEL) -- "-v$(GCC_VER)-1~exp$(THIS_VERSION)" \
		"-Vhost:gnu-type=`dpkg-architecture -qDEB_HOST_GNU_TYPE | tr _ -`"
	dh_md5sums $(PKGSEL)
	dh_builddeb $(PKGSEL)

clean:
	dh_testdir
	dh_testroot
	dh_clean


debian/control:debian/control.in
	sh $< > $@

check-control:
	if ! sh debian/control.in | diff -u debian/control -; then \
		echo "debian/control is outdated and needs to be regenerated"; \
		exit $(OUTDATED_CONTROL_IS_FATAL); \
	fi

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