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

VER := $(shell dpkg-query -W binutils-source | cut -f2 | cut -d'-' -f1)
DVER := $(shell dpkg-query -W binutils-source | cut -f2)
DVER_T = $(DVER)cross1
DEB_NAME_ACT     := $(shell dpkg-parsechangelog| sed -n 's/-*//; s/^Source: \(.*\)/\1/p')
DEB_SVER_ACT     := $(shell dpkg-parsechangelog| sed -n 's/-*//; s/^Version: \(.*\)/\1/p')

HOST_ARCH = amd64 ppc64el arm64 i386

ARCH = mips64 mipsn32 mipsn32el mipsr6 mipsr6el mipsn32r6 mipsn32r6el mips64r6 mips64r6el

TRIPLE = $(foreach a,$(ARCH),$(shell dpkg-architecture -f -a$(a) -qDEB_HOST_GNU_TYPE))

stamp-dir/control:
	cat debian/control.in > debian/control.tmp
	nl=0; IFS= ; while read -r line; do \
	   if [ "$$line" = "" ];then \
	     nl=0; \
	     continue; \
	   fi; \
	   for t in $(TRIPLE); do \
	     tmp=`echo $$line | grep "Package: binutils-$$t"`; \
	     if [ -n "$$tmp" ];then \
	       echo ""; \
	       echo $$line; \
	       nl=1; \
	       continue 2; \
	     fi; \
	   done; \
	   if [ $$nl -eq 1 ] && [ "$$line" != "" ];then \
	     echo $$line; \
	   fi; \
	done < /usr/src/binutils/debian/control >> debian/control.tmp
	sed -i 's/^Architecture: .*/Architecture: $(HOST_ARCH)/g' debian/control.tmp
	sed -i 's/binutils-common (= [^ ]*)/binutils-common/g' debian/control.tmp
	sed -i 's/binutils-doc (= [^ ]*)/binutils-doc/g' debian/control.tmp
	mv debian/control.tmp debian/control
	touch $@

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

stamp-dir/prepare: stamp-dir/control
	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
	touch $@

stamp-dir/build: stamp-dir/prepare
	cd binutils-$(VER); \
		sed -i 's/src_name := .*/src_name := binutils-mipsen/g' debian/rules; \
		DEB_BUILD_OPTIONS="nocheck nomult nohppa" CROSS_ARCHS="$(ARCH)" dpkg-buildpackage -B -d -uc -us
	touch $@

$(ARCH): build-arch
	gnu_type=`dpkg-architecture -f -a$@ -qDEB_HOST_GNU_TYPE`; \
	for type in $$gnu_type $${gnu_type}-dbg; do \
	  pkg="binutils-$${type}_$(DVER)_$(DEB_HOST_ARCH).deb"; \
	  pkg_d=binutils-$${type}_$(DVER_T)_$(DEB_HOST_ARCH); \
	  rm -rf $$pkg_d; \
	  echo dpkg-deb -R $$pkg $$pkg_d; \
	  dpkg-deb -R $$pkg $$pkg_d; \
	  sed -i 's/Source: binutils/Source: binutils-mipsen ($(DEB_SVER_ACT))/g' $$pkg_d/DEBIAN/control; \
	  sed -i 's/Version: .*/Version: $(DVER_T)/g' $$pkg_d/DEBIAN/control; \
	  sed -i 's/ (= $(DVER))/ (>= $(DVER))/g' $$pkg_d/DEBIAN/control; \
	  if [ -f "$$pkg_d/usr/share/doc/binutils-$${type}/changelog.Debian.gz" ]; then \
	    mv -f $$pkg_d/usr/share/doc/binutils-$${type}/changelog.Debian.gz $$pkg_d/usr/share/doc/binutils-$${type}/changelog.Debian.binutils.gz; \
	    gzip -c9n debian/changelog > $$pkg_d/usr/share/doc/binutils-$${type}/changelog.Debian.gz; \
	    chmod 0644 $$pkg_d/usr/share/doc/binutils-$${type}/changelog.Debian.gz; \
	    rm -f $$pkg_d/DEBIAN/md5sums; \
	    (cd $$pkg_d && find usr -type f | LC_ALL=C sort | xargs -r md5sum >>DEBIAN/md5sums); \
	  fi; \
	  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; \
	done

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

binary-arch: $(ARCH) 
binary-indep:

binary: binary-arch binary-indep

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