#!/usr/bin/make -f
#export DH_VERBOSE = 1

vafilt = $(subst $(2)=,,$(filter $(2)=%,$(1)))
DPKG_VARS := $(shell dpkg-architecture)
DEB_BUILD_GNU_TYPE      ?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH           ?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH)
DEB_HOST_GNU_CPU        ?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_CPU)
DEB_HOST_GNU_TYPE       ?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_TYPE)
DEB_HOST_MULTIARCH      ?= $(call vafilt,$(DPKG_VARS),DEB_HOST_MULTIARCH)

VER := $(shell dpkg-query -W binutils-source | cut -f2 | cut -d'-' -f1 | cut -d'.' -f1-3)
DVER := $(shell dpkg-query -W binutils-source | cut -f2)
DEB_NAME_ACT     := $(shell dpkg-parsechangelog| sed -n 's/-*//; s/^Source: \(.*\)/\1/p')
DEB_SVER_ACT     := $(shell dpkg-parsechangelog| sed -n 's/-*//; s/+b.*//g; s/^Version: \(.*\)/\1/p')
DEB_CVER_ACT     := $(shell dpkg-parsechangelog |egrep ^Version:| sed 's/.*c[ros]*\(.*\)/\1/')
CROSS_EXT        := cross$(DEB_CVER_ACT)
DVER_T = $(DVER)$(CROSS_EXT)

HOST64_ARCHS = i386 powerpc mips mipsel mips mipsn32 mipsn32el mipsr6 mipsr6el mipsn32r6 mipsn32r6el x32
ifneq (,$(filter $(DEB_HOST_ARCH), mips mipsn32))
  confargs_host64_gnu_type=mips64-linux-gnuabi64
  CFLAGS_64 = -mabi=64
  CXXFLAGS_64 += -mabi=64
else ifneq (,$(filter $(DEB_HOST_ARCH), mipsel mipsn32el))
  confargs_host64_gnu_type=mips64el-linux-gnuabi64
  CFLAGS_64 = -mabi=64
  CXXFLAGS_64 = -mabi=64
else ifneq (,$(filter $(DEB_HOST_ARCH), mipsr6 mipsn32r6))
  confargs_host64_gnu_type=mipsisa64-linux-gnuabi64
  CFLAGS_64 = -mabi=64
  CXXFLAGS_64 += -mabi=64
else ifneq (,$(filter $(DEB_HOST_ARCH), mipsr6el mipsn32r6el))
  confargs_host64_gnu_type=mipsisa64el-linux-gnuabi64
  CFLAGS_64 = -mabi=64
  CXXFLAGS_64 = -mabi=64
else ifneq (,$(filter $(DEB_HOST_ARCH), i386 x32))
  confargs_host64_gnu_type=x86_64-linux-gnu
  CFLAGS_64 = -m64
  CXXFLAGS_64 = -m64
else ifneq (,$(filter $(DEB_HOST_ARCH), powerpc))
  confargs_host64_gnu_type=powerpc64-linux-gnu
  CFLAGS_64 = -m64
  CXXFLAGS_64 = -m64
endif

clean:
	rm -rf binutils* libbinutils* libctf* stamp-dir/* debian/control.tmp debian/files

stamp-dir/prepare:
	rm -rf binutils* libbinutils*
	rm -f debian/files
	tar xf /usr/src/binutils/binutils-$(VER).tar.xz
	cp -r /usr/src/binutils/debian binutils-$(VER)/
	cp -r /usr/src/binutils/patches binutils-$(VER)/debian
	sed -i '/.*ps aux.*/d' binutils-$(VER)/debian/rules
	cd binutils-$(VER) ;\
	  for i in `ls $(CURDIR)/debian/patches/binutils/*.patch 2>/dev/null`; do \
	    patch -p1 < $$i; \
	  done;

	sed -i 's/^src_name := .*/src_name := $(DEB_NAME_ACT)/g' binutils-$(VER)/debian/rules
	sed -i '/^src_name :=/a\with_native := yes' binutils-$(VER)/debian/rules
	sed -i 's/^with_host64 := .*/with_host64 := yes/g' binutils-$(VER)/debian/rules

	cd binutils-$(VER) ;\
		env -i make -f debian/rules stamps/control
	touch $@

stamp-dir/build: stamp-dir/prepare
	cd binutils-$(VER); \
		DEB_BUILD_OPTIONS="nomult nohppa nocross nocheck" dpkg-buildpackage -B -d -uc -us
	touch $@

stamp-dir/repack: stamp-dir/build
	echo "Repack ..."
	rm -f debian/files

	pkg=binutils64_$(DVER)_$(DEB_HOST_ARCH).deb; \
	    pkg_d=binutils64_$(DVER_T)_$(DEB_HOST_ARCH); \
	    rm -rf $$pkg_d; \
	    dpkg-deb -R $$pkg $$pkg_d; \
	        sed -i 's/^Version: .*/Version: $(DVER_T)/g' $$pkg_d/DEBIAN/control; \
	        sed -i 's/^Source: .*/Source: $(DEB_NAME_ACT) ($(DEB_SVER_ACT))/g' $$pkg_d/DEBIAN/control; \
	        sed -i '/^Built-Using/! s/(= $(DVER))/(>= $(DVER))/g' $$pkg_d/DEBIAN/control; \
	        mkdir -p $${pkg_d}/usr/share/doc/binutils64; \
		    gzip -c9n binutils-$(VER)/debian/changelog > $$pkg_d/usr/share/doc/binutils64/changelog.Debian.binutils.gz; \
		    gzip -c9n debian/changelog > $$pkg_d/usr/share/doc/binutils64/changelog.Debian.gz; \
		    cp binutils-$(VER)/debian/copyright $$pkg_d/usr/share/doc/binutils64/copyright.binutils; \
		    cp debian/copyright $$pkg_d/usr/share/doc/binutils64/copyright; \
	        mkdir -p $${pkg_d}/usr/share/lintian/overrides; \
		    cp debian/binutils64.lintian-overrides $${pkg_d}/usr/share/lintian/overrides/binutils64; \
	        rm -f $$pkg_d/DEBIAN/triggers; \
	        rm -f $$pkg_d/DEBIAN/md5sums; \
	        (cd $$pkg_d && find usr -type f | LC_ALL=C sort | xargs -r md5sum >>DEBIAN/md5sums); \
	    dpkg-deb -b $$pkg_d ../$${pkg_d}.deb; \
	    echo -n "$${pkg_d}.deb " >> debian/files; \
            dpkg-deb -I ../$${pkg_d}.deb | grep Section  | cut -d ' ' -f 3 | tr "\n" ' ' >>debian/files; \
            dpkg-deb -I ../$${pkg_d}.deb | grep Priority | cut -d ' ' -f 3 | tr -d "\n" >>debian/files; \
            echo "" >> debian/files

	pkg=binutils64-dbg_$(DVER)_$(DEB_HOST_ARCH).deb; \
	    pkg_d=binutils64-dbg_$(DVER_T)_$(DEB_HOST_ARCH); \
	    rm -rf $$pkg_d; \
	    dpkg-deb -R $$pkg $$pkg_d; \
	        sed -i 's/^Source: .*/Source: $(DEB_NAME_ACT) ($(DEB_SVER_ACT))/g' $$pkg_d/DEBIAN/control; \
	        sed -i '/^Built-Using/! s/$(DVER)/$(DVER_T)/g' $$pkg_d/DEBIAN/control; \
	        mkdir -p $${pkg_d}/usr/share/lintian/overrides; \
	            cp debian/binutils64-dbg.lintian-overrides $${pkg_d}/usr/share/lintian/overrides/binutils64-dbg; \
	        rm -f $$pkg_d/DEBIAN/md5sums; \
	        (cd $$pkg_d && find usr -type f | LC_ALL=C sort | xargs -r md5sum >>DEBIAN/md5sums); \
	    dpkg-deb -b $$pkg_d ../$${pkg_d}.deb; \
	    echo -n "$${pkg_d}.deb " >> debian/files; \
            dpkg-deb -I ../$${pkg_d}.deb | grep Section  | cut -d ' ' -f 3 | tr "\n" ' ' >>debian/files; \
            dpkg-deb -I ../$${pkg_d}.deb | grep Priority | cut -d ' ' -f 3 | tr -d "\n" >>debian/files; \
            echo "" >> debian/files
	
	touch $@

build-arch: stamp-dir/build
build-indep:
build: build-arch build-indep

binary-arch: stamp-dir/repack
binary-indep:

binary: binary-arch binary-indep

.PHONY: binary binary-arch binary-indep clean checkroot $(ARCHS) build-arch build-indep
