#!/usr/bin/make -f
# -*- makefile -*-

DEB_HOST_ARCH  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

%:
	dh $@ --parallel --with autoreconf

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	-dh_auto_test 2>&1 | tee test.log
	rv=0; \
	fails=$$(awk '/^# FAIL:/ { print $$3 }' test.log); \
	if [ "$$fails" -eq 0 ]; then \
	  echo "all tests did pass on $(DEB_HOST_ARCH)"; \
        else \
	  if [ -f debian/test-expected.$(DEB_HOST_ARCH) ]; then \
	    cp debian/test-expected.$(DEB_HOST_ARCH) test-expected.log; \
	  else \
	    rm -f test-expected.log; touch test-expected.log; \
	  fi; \
	  grep '^FAIL: ' test.log | sort > test-fail.log; \
	  comm -13 test-expected.log test-fail.log > test-regress.log; \
	  comm -23 test-expected.log test-fail.log > test-progress.log; \
	  if [ -z "$$(cat test-regress.log)" ]; then \
	    echo "expected test failures on $(DEB_HOST_ARCH)"; \
	    if [ -n "$$(cat test-progress.log)" ]; then \
	      echo "unexpected test progressions on $(DEB_HOST_ARCH)"; \
	      cat test-progress.log; \
	    fi; \
	  else \
	    echo "unexpected test failures on $(DEB_HOST_ARCH)"; \
	    cat test-regress.log; \
	    rv=1; \
	  fi; \
	  if [ -n "$$(cat test-fail.log)" ]; then \
	    for t in $$(awk '{print $$2}' test-fail.log); do \
	      echo "=============== $$t ==============="; \
	      cat tests/$$t.log; \
	    done; \
	    echo "=============== tests/test-suite.log ==============="; \
	    cat tests/test-suite.log; \
	  fi; \
	fi; \
	exit $$rv
endif

override_dh_auto_install:
	dh_auto_install --destdir=$(CURDIR)/debian/tmp

override_dh_auto_clean:
	rm -f test*.log
	dh_auto_clean

