#!/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

%:
	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 \
	  && genpeimg -d +d -d +n -d +s $(shell pwd)/debian/tmp/usr/$$arch/bin/win_iconv.exe \
	  && genpeimg -d +d -d +n -d +s $(shell pwd)/debian/tmp/usr/$$arch/bin/iconv.dll \
	  || exit 1 ; \
	  cd .. ; \
	done

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifdef TESTARCH
	cd build-$(TESTARCH) && WINEPREFIX=$(shell pwd)/build-$(TESTARCH)/.wine $(MAKE) CC=$(TESTARCH)-gcc AR=$(TESTARCH)-ar RANLIB=$(TESTARCH)-ranlib DLLTOOL=$(TESTARCH)-dlltool test
endif
endif
