#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export DH_OPTIONS

include /usr/share/cdbs/1/class/mingw64.mk
include /usr/share/cdbs/1/rules/mingw64-autotools.mk
				
MINGW64_CONFIGURE	= $(MINGW64_ENVARS) \
	$(CURDIR)/source/configure \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--host=$(MINGW64_TARGET) \
	--prefix=$(MINGW64_SYSROOT)mingw \
	--bindir=$(MINGW64_SYSROOT)mingw/bin \
	--libdir=$(MINGW64_SYSROOT)mingw/lib \
	--cache-file=mingw64-config.cache

MINGW32_CONFIGURE	=  $(MINGW32_ENVARS) \
	$(CURDIR)/source/configure \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--host=$(MINGW32_TARGET) \
	--prefix=$(MINGW32_SYSROOT)mingw \
	--bindir=$(MINGW32_SYSROOT)mingw/bin \
	--libdir=$(MINGW32_SYSROOT)mingw/lib \
	--cache-file=mingw32-config.cache

CONFIGOPTS	= --enable-shared --enable-static \
		--with-cross-build=$(CURDIR)/source/nativebuild \
		--with-data-packaging=library \
		--disable-rpath \
		--without-samples

configure: configure-stamp
configure-stamp:
	dh_testdir
	
	# First we have to build a native copy for whatever reason
	mkdir -p source/nativebuild
	cd source/nativebuild && \
		CFLAGS='-O0' CXXFLAGS='-O0' ../configure --enable-static --disable-shared && \
		($(MAKE) $(MAKEOPTS) || ($MAKE))
	
	mkdir -p source/build-64
	cd source/build-64 && $(MINGW64_CONFIGURE) $(CONFIGOPTS)
	
	mkdir -p source/build-32
	cd source/build-32 && $(MINGW32_CONFIGURE) $(CONFIGOPTS)
		
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	
	# Build 32
	#sed -i -e 's,^GENCCODE_ASSEMBLY.*$$,GENCCODE_ASSEMBLY=-a gcc-mingw64,' source/config/mh-mingw64
	cd source/build-32 && $(MAKE) $(MAKEOPTS)
	
	# Build 64
	#sed -i -e 's,^GENCCODE_ASSEMBLY.*$$,GENCCODE_ASSEMBLY=-a gcc-mingw64,' source/config/mh-mingw64
	cd source/build-64 && $(MAKE) $(MAKEOPTS)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf build-stamp configure-stamp source/build-32 source/build-64 source/nativebuild $(BUILD_ROOT)
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	
	mkdir -p $(BUILD_ROOT)/32
	cd source/build-32 && $(MAKE) DESTDIR=$(CURDIR)/$(BUILD_ROOT)/32 install
	mkdir -p $(BUILD_ROOT)/64
	cd source/build-64 && $(MAKE) DESTDIR=$(CURDIR)/$(BUILD_ROOT)/64 install
	
	find $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib -name '*.dll' -type l -delete
	find $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib -name '*.dll' -type l -delete
	
	#for file in $$(find $(BUILD_ROOT) -name '*.a'); do \
	#	mv $$file $$(echo $$file | sed -e 's/\./48./'); \
	#done
	
	#rm -f $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib/icudata48.dll
	#rm -f $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib/icudata48.dll
	
	#mv $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib/libicudata48.1.1.dll $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib/icudata48.dll
	#mv $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib/libicudata48.1.1.dll $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib/icudata48.dll
	
	for i in $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/lib/*.dll; \
		do mv $$i $(BUILD_ROOT)/64$(MINGW64_SYSROOT)mingw/bin; done
	
	for i in $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/lib/*.dll; \
		do mv $$i $(BUILD_ROOT)/32$(MINGW32_SYSROOT)mingw/bin; done
	
	#sed -i -e 's,default_ENABLE_RPATH=.*,default_ENABLE_RPATH="N",' -e 's,^SO=.*$$,SO="dll",' $$(find . -name icu-config)
