#!/usr/bin/make -f
export DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS \
        2>/dev/null || true)

export DEB_HOST_GNU_SYSTEM := $(shell dpkg-architecture \
        -qDEB_HOST_GNU_SYSTEM 2>/dev/null || true)

ifeq ($(DEB_HOST_ARCH_OS),)
  DEB_HOST_ARCH_OS := $(subst -gnu,,$(DEB_HOST_GNU_SYSTEM))
  ifeq ($(DEB_HOST_ARCH_OS),gnu)
    DEB_HOST_ARCH_OS := hurd
  endif
endif

DEB_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  CROSS :=
else
  CROSS := CC=$(DEB_HOST_GNU_TYPE)-gcc
endif


%:
	dh $@

override_dh_auto_build:
	./autogen.sh
ifeq ($(DEB_HOST_ARCH_OS),linux)
	./configure --with-acl=yes --with-selinux=yes
else
	./configure
endif
	$(MAKE) $(CROSS)

override_dh_auto_install:

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) distclean
	cd test ; rm -f $$(ls | egrep '^test-config.[0-9]+$$')
	cd test ; rm -f test*.log* anothertest*.log* state test-config.
	cd test ; rm -f scriptout mail-out compress-args different*.log*

# ACL tests will fail if we're not building with ACL
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
  ifeq ($(DEB_HOST_ARCH_OS),linux)
	cd test ; ./test
  endif
endif
