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

gcc_confopts =	--with-gnu-as --with-gnu-ld --verbose \
		--without-newlib \
		--disable-multilib \
		--with-system-zlib \
		--disable-nls --without-included-gettext \
		--disable-win32-registry \
		--enable-version-specific-runtime-libs \
		--enable-languages="c" \
		--enable-fully-dynamic-strings \
                --enable-hash-synchronization \
		--prefix=/usr \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_HOST_GNU_TYPE) \
                --enable-decimal-float=yes
 

configure: configure-stamp
configure-stamp:
	dh_testdir
	
	mkdir -p build/64
	cd build/64 && $(CURDIR)/configure \
		--target=$(MINGW64_TARGET) \
		--with-sysroot=$(MINGW64_SYSROOT) \
		$(gcc_confopts)
	
	mkdir -p build/32
	cd build/32 && $(CURDIR)/configure \
		--target=$(MINGW32_TARGET) \
		--with-sysroot=$(MINGW32_SYSROOT) \
		$(gcc_confopts)
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	
	cd build/64 && $(MAKE) $(MAKEOPTS) all-gcc
	cd build/32 && $(MAKE) $(MAKEOPTS) all-gcc
	
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf build-stamp configure-stamp build debian/tmp
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	
	rm -rf $(BUILD_ROOT)
	
	mkdir -p $(BUILD_ROOT)
	cd build/64 && $(MAKE) $(MAKEOPTS) install-gcc DESTDIR=$(CURDIR)/$(BUILD_ROOT)/64
	cd build/32 && $(MAKE) $(MAKEOPTS) install-gcc DESTDIR=$(CURDIR)/$(BUILD_ROOT)/32
	
	# These files are in conflict with system files
	rm -rf $(BUILD_ROOT)/32/usr/share/info
	rm -f  $(BUILD_ROOT)/32/usr/lib/libiberty*
	rm -f  $(BUILD_ROOT)/32/usr/share/man/man7/*
	rm -f  $(BUILD_ROOT)/32/usr/bin/vxaddr2line
	rm -rf $(BUILD_ROOT)/64/usr/share/info
	rm -f  $(BUILD_ROOT)/64/usr/lib/libiberty*
	rm -f  $(BUILD_ROOT)/64/usr/share/man/man7/*
	rm -f  $(BUILD_ROOT)/64/usr/bin/vxaddr2line
	
	mkdir -p $(BUILD_ROOT)/32/lib
	ln -sf ../usr/bin/$(MINGW32_TARGET)-cpp $(BUILD_ROOT)/32/lib/$(MINGW32_TARGET)-cpp
	mkdir -p $(BUILD_ROOT)/64/lib
	ln -sf ../usr/bin/$(MINGW64_TARGET)-cpp $(BUILD_ROOT)/64/lib/$(MINGW64_TARGET)-cpp

binary-indep: build install
	#dh_testdir -i
	#dh_testroot -i
	#dh_movefiles -i
	#dh_installdocs -i
	#dh_installexamples -i
	#dh_install -i
	#dh_link -i
	#dh_fixperms -i
	#dh_makeshlibs -i
	#dh_installdeb -i
	#dh_shlibdeps -i
	#dh_installchangelogs ChangeLog
	#dh_gencontrol -i
	#dh_md5sums -i
	#dh_builddeb -i

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_movefiles -a
	dh_installdocs -a
	dh_installexamples -a
	dh_install -a
	dh_link -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_installchangelogs ChangeLog
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install configure
