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

DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

BUILD_ROOT	= debian/tmp
PREFIX		= /usr

CFLAGS		= -Wall -g
INSTALL		= install
INSTALL_FILE	= $(INSTALL) -p -o root -g root -m 644
INSTALL_PROGRAM	= $(INSTALL) -p -o root -g root -m 755
INSTALL_SCIPRT	= $(INSTALL) -p -o root -g root -m 755
INSTALL_DIR	= $(INSTALL) -p -d -o root -g root -m 755

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	
	mkdir -p build/64
	cd build/64 && $(CURDIR)/configure \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_HOST_GNU_TYPE) \
		--target=x86_64-w64-mingw32 \
		--verbose \
		--disable-nls \
		--without-included-gettext \
		--disable-win32-registry \
		--disable-werror \
		--with-sysroot=/usr/x86_64-w64-mingw32/sys-root/mingw \
		--prefix=/usr \
		--bindir=/usr/bin \
		--includedir=/usr/include \
		--libdir=/usr/lib \
		--enable-plugins \
		--enable-gold
	
	mkdir -p build/32 
	cd build/32 && $(CURDIR)/configure \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--host=$(DEB_HOST_GNU_TYPE) \
		--target=i686-w64-mingw32 \
		--verbose \
		--disable-nls \
		--without-included-gettext \
		--disable-win32-registry \
		--disable-werror \
		--with-sysroot=/usr/i686-w64-mingw32/sys-root/mingw \
		--prefix=/usr \
		--bindir=/usr/bin \
		--includedir=/usr/include \
		--libdir=/usr/lib \
		--enable-plugins \
		--enable-gold

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	
	# Nothing to do on this package
	
	cd build/32 && $(MAKE) $(MAKEOPTS) all
	cd build/64 && $(MAKE) $(MAKEOPTS) all
	
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf build-stamp configure-stamp build $(BUILD_ROOT)
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	
	mkdir -p $(BUILD_ROOT)/32
	cd build/32 && $(MAKE) DESTDIR=$(CURDIR)/$(BUILD_ROOT)/32 install
	cd build/64 && $(MAKE) DESTDIR=$(CURDIR)/$(BUILD_ROOT)/64 install
	
	rm -rf $(BUILD_ROOT)/32/usr/share/info
	rm -f $(BUILD_ROOT)/32/usr/lib/libiberty*
	rm -rf $(BUILD_ROOT)/64/usr/share/info
	rm -f $(BUILD_ROOT)/64/usr/lib/libiberty*
	rm -vrf $(BUILD_ROOT)/*/usr/lib/bfd-plugins
	
	for i in ar as dlltool ld ld.bfd nm objcopy objdump ranlib strip; do \
		rm -f $(BUILD_ROOT)/64/usr/bin/x86_64-w64-mingw32-$$i; \
		ln -s /usr/x86_64-w64-mingw32/bin/$$i $(BUILD_ROOT)/64/usr/bin/x86_64-w64-mingw32-$$i; \
		rm -f $(BUILD_ROOT)/32/usr/bin/i686-w64-mingw32-$$i; \
		ln -s /usr/i686-w64-mingw32/bin/$$i $(BUILD_ROOT)/32/usr/bin/i686-w64-mingw32-$$i; \
	done

	# Make some links to fool us
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 --sourcedir=$(CURDIR)/debian/tmp/32 -pmingw32-cross-binutils
	dh_install -a --sourcedir=$(CURDIR)/debian/tmp/64 -pmingw64-cross-binutils
	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
