#!/usr/bin/make -f

# we do not want stackprotector because there is no implementation on mingw32:
export DEB_BUILD_MAINT_OPTIONS = hardening=-stackprotector

ifeq ($(DEB_BUILD_ARCH),i386)
  TESTARCH=i686-w64-mingw32
else ifeq ($(DEB_BUILD_ARCH), amd64)
  TESTARCH=x86_64-w64-mingw32
endif

ifdef TESTARCH
	TESTPLAN=cd build-$(TESTARCH) && WINEPREFIX=$(shell pwd)/build-$(TESTARCH)/.wine $(MAKE) CC=$(TESTARCH)-gcc AR=$(TESTARCH)-ar RANLIB=$(TESTARCH)-ranlib DLLTOOL=$(TESTARCH)-dlltool test
else
	TESTPLAN=true
endif

%:
	dh $@

override_dh_auto_build-indep:
	for arch in x86_64-w64-mingw32 i686-w64-mingw32; do \
	 mkdir -p build-$$arch && \
	 cd build-$$arch && \
	  ln -s ../*.h ../*.c ../*.def ../Makefile ./ && \
	  $(MAKE) CC=$$arch-gcc AR=$$arch-ar RANLIB=$$arch-ranlib DLLTOOL=$$arch-dlltool prefix=/usr/$$arch \
	  || exit 1 ; \
	  cd .. ; \
	done

override_dh_auto_install-indep:
	for arch in x86_64-w64-mingw32 i686-w64-mingw32; do \
	  cd build-$$arch && \
	  $(MAKE) CC=$$arch-gcc AR=$$arch-ar RANLIB=$$arch-ranlib DLLTOOL=$$arch-dlltool prefix=/usr/$$arch install DESTDIR=$(shell pwd)/debian/tmp \
	  || exit 1 ; \
	  cd .. ; \
	done

override_dh_auto_test:
	$(TESTPLAN)
