#!/usr/bin/make -f


THIS_PKG         := $(shell dpkg-parsechangelog| sed -n 's/-*//; s/^Source: \(.*\)/\1/p')
THIS_PKG_VER     := $(shell dpkg-parsechangelog| sed -n 's/^Version: \(.*\)/\1/p')
THIS_PKG_MAINT   := $(shell dpkg-parsechangelog| sed -n 's/^Maintainer: \(.*\)/\1/p')

DEB_HOST_ARCH    := $(shell dpkg-architecture -qDEB_HOST_ARCH)
stamp            := stamp/



DEB_VER_BINUTILS := $(shell dpkg-parsechangelog -l/usr/src/binutils/debian/changelog | egrep '^Version:' | cut -f 2 -d ' ')
PKG_VER_BINUTILS := $(shell dpkg-parsechangelog -l/usr/src/binutils/debian/changelog | egrep '^Version:' | cut -f 2 -d ' '|cut -f 1 -d '-')
VER_BINUTILS     := $(shell dpkg-parsechangelog -l/usr/src/binutils/debian/changelog | egrep '^Version:' | cut -f 2 -d ' '|cut -f 1 -d '-'|cut -d '.' -f1-3)

BINUTILS_DIR  := /usr/src/binutils

DISTRO ?= $(shell dpkg-vendor --query Vendor)

# We know the HOST and TARGET arches for Debian and Ubuntu, but not others. 
ifeq ($(strip $(DISTRO)),)
  $(error cannot detect distro. Giving up. Set $DISTRO to 'Debian' or 'Ubuntu')
else ifeq ($(DISTRO),Debian)
  TARGET_LIST ?= armhf armel arm64 mips mips64 mipsel mips64el powerpc ppc64el i386 amd64
  HOST_LIST ?= amd64 i386 armhf arm64 ppc64el
else ifeq ($(DISTRO),Ubuntu)
  TARGET_LIST ?= armhf armel arm64 
  HOST_LIST ?= amd64 i386 arm64 ppc64el
else
  $(error unknown distro "$(DISTRO)". Nothing will be built.) 
endif

# Don't try to build the HOST=TARGET (ie. non-cross) combination for this arch
TARGETS := $(filter-out ${DEB_HOST_ARCH}, ${TARGET_LIST})


init: $(stamp)init

$(stamp)init:
	@echo START $@
	mkdir -p debian/tmp $(stamp)
	tar -x -f  ${BINUTILS_DIR}/binutils-${VER_BINUTILS}.tar.xz
	set -e; \
	cd binutils-${PKG_VER_BINUTILS} ;\
	cp -a ${BINUTILS_DIR}/debian/ . ;\
	if [ -n "$$(grep -v '^\#' debian/patches/binutils/series)" ]; then \
	  QUILT_PATCHES=debian/patches/binutils quilt push -a; \
	fi; \

	touch $@

$(stamp)build-%: $(stamp)init
	@echo START $@
	set -e; \
	cd binutils-${PKG_VER_BINUTILS}; \
	DEB_TARGET_ARCH=$* TARGET=$* dpkg-buildpackage -T control-stamp; \
	WITH_SYSROOT=/ DEB_TARGET_ARCH=$* TARGET=$* dpkg-buildpackage -B -uc -us; \
	cd ..; \
	touch $@


clean:
	rm -rf repackfiles tmp debian/tmp debian/files 
	rm -rf binutils-* *.deb *.changes
	find debian -name '*~' -print0 | xargs -0 -r rm -f
	rm -rf $(stamp)


#
# Repack resulting packages to make changes:
# - Set Source and Version to this package 
define repack_deb
	echo "repacking $$deb ..."; \
	rm -rf tmp/*; \
	dpkg-deb -x $$deb tmp; \
	dpkg-deb -e $$deb tmp/DEBIAN; \
	pkgname=`echo $$deb | cut -d'_' -f1`; \
        sed -i -e's/^Source:.*/Source: ${THIS_PKG} (${THIS_PKG_VER})/' tmp/DEBIAN/control; \
        sed -i -e's/^Maintainer:.*/Maintainer: ${THIS_PKG_MAINT}\nOriginal-Source-&/' tmp/DEBIAN/control; \
	if [ -e debian/overrides/$$pkgname ]; then \
	  mkdir -p tmp/usr/share/lintian/overrides; \
	  cp debian/overrides/$$pkgname tmp/usr/share/lintian/overrides/; \
	fi; \
	(cd tmp && md5sum `find usr -type f` >>DEBIAN/md5sums ;\
	 chmod 644 DEBIAN/md5sums ; \
        );\
	find tmp/usr -type d -execdir chmod 755 '{}' \; ;\
	dpkg-deb -b tmp/ ../$$deb; \
	echo "        done";
endef

repack-debs:
	@echo START $@
	install -d repackfiles; \
	for deb in $(wildcard *.deb); \
	do \
	  $(call repack_deb, $${deb}) \
	done; \

BUILD_TARGETS := $(patsubst %,$(stamp)build-%,${TARGETS})

build-arch: build
build-indep: 
build: $(BUILD_TARGETS)
	rm -f *.changes

binary: binary-arch binary-indep 
binary-arch:  build-arch mangle-debian-files
binary-indep:


$(stamp)debian-files:
	@echo START $@
	rm -f debian/files
	touch $@

mangle-debian-files: $(stamp)debian-files repack-debs
	@echo START $@
	@for deb in $(wildcard *.deb); \
	do \
		echo -n "`basename $$deb` " >>debian/files; \
		dpkg-deb -I $$deb | grep Section  | cut -d ' ' -f 3 | tr "\n" ' ' >>debian/files; \
		dpkg-deb -I $$deb | grep Priority | cut -d ' ' -f 3 | tr -d "\n" >>debian/files; \
		echo "" >>debian/files; \
	done


# Generate the control file from head plus stanza for each binary
# Don't build binaries when HOST=TARGET
# Ensure no underscores appear in package names (x86_64-linux-gnu we're looking at you)

control:
	cat debian/control.head.in > debian/control;
	for DEB_TARGET_ARCH in $(TARGET_LIST); do \
	    DEB_TARGET_GNU_TYPE=`dpkg-architecture -a$${DEB_TARGET_ARCH} -qDEB_HOST_GNU_TYPE -f 2>/dev/null`; \
	    TARGET_PACKAGENAME=`echo $${DEB_TARGET_GNU_TYPE} | tr _ -` ; \
	    ARCH_LIST_FOR_BINARY=`echo ${HOST_LIST} | sed "s/ *$${DEB_TARGET_ARCH} */ /"`; \
	    sed -e "s/DEB_TARGET_ARCH/$${DEB_TARGET_ARCH}/g" debian/control.binary.in | \
	    sed -e "s/ARCH_LIST_FOR_BINARY/$${ARCH_LIST_FOR_BINARY}/g" | \
	    sed -e "s/DEB_TARGET_GNU_TYPE/$${TARGET_PACKAGENAME}/g" >> debian/control; \
	done


# Manual convenience rule to update changelog to match current binutils version
# works outside build-environment  
update-changelog-external:
	pkgver=$$( rmadison -a source -s sid binutils |  tail -n 1 | awk '{ print $$1 "_" $$3 }' );\
	dch -i --no-auto-nmu -D unstable "Rebuild using $$pkgver"

# works inside build environment
update-changelog:
	dch -i --no-auto-nmu -D unstable "Rebuild using binutils ${DEB_VER_BINUTILS}"
