#!/usr/bin/make -f

DEB_TARGET_ARCH=mips
# prefix for builds - without leading slash
PF ?= usr

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            := stamps/

DISTRO ?= $(shell lsb_release -is)

ifeq ($(DISTRO),Debian)
HOST_LIST ?= amd64
BASE_VER_GCC ?= 4.9
else # Ubuntu
HOST_LIST ?= amd64
BASE_VER_GCC ?= 4.9
endif

VER_GCC     := $(shell dpkg-parsechangelog -l/usr/src/gcc-$(BASE_VER_GCC)/debian/changelog | egrep '^Version:' | cut -f 2 -d ' '|cut -f 1 -d '-'|cut -d '.' -f1-3)
PKG_VER_GCC := $(shell dpkg-parsechangelog -l/usr/src/gcc-$(BASE_VER_GCC)/debian/changelog | egrep '^Version:' | cut -f 2 -d ' '|cut -f 1 -d '-')
DEB_VER_GCC := $(shell dpkg-parsechangelog -l/usr/src/gcc-$(BASE_VER_GCC)/debian/changelog | egrep '^Version:' | cut -f 2 -d ' ')

GCC_DIR  := /usr/src/gcc-$(BASE_VER_GCC)

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


info:
	@echo "Gcc version:         ${VER_GCC}	${DEB_VER_GCC}	${PKG_VER_GCC}"
	@echo
	@echo "Target architecture:      ${DEB_TARGET_ARCH}"     
	@echo "Host architecture:        ${DEB_HOST_ARCH}"

$(stamp): 
	mkdir $(stamp) -p

debian/tmp:
	mkdir debian/tmp -p

$(stamp)init-dirs: $(stamp) debian/tmp
	touch $@

init: $(stamp)init-dirs $(stamp)init

$(stamp)init:
	@echo START $@
	tar -x -f ${GCC_DIR}/gcc-${VER_GCC}*.tar.[gx]z
	set -e; \
	cd gcc-${PKG_VER_GCC} ;\
	cp -a ${GCC_DIR}/debian/ . ;\
	if [ -n "$$(grep -v '^\#' ${CURDIR}/debian/patches/series)" ]; then \
	  QUILT_PATCHES=${CURDIR}/debian/patches quilt --quiltrc /dev/null push -a; \
	fi; \

	touch $@

# dpkg-buildpackage target-arch setting changed from env var to option from 1.17.17
DPKG_HAS_TARGETARCH = $(shell (cd /; dpkg-buildpackage --target-arch; if [ $$? -eq 2 ] ; then echo 'no'; fi))
ifeq ($(DPKG_HAS_TARGETARCH),no)
dpkg_target_cmd = DEB_TARGET_ARCH=$* with_deps_on_target_arch_pkgs=yes DEB_CROSS_NO_BIARCH=yes dpkg-buildpackage
else # must use --target-arch
dpkg_target_cmd = with_deps_on_target_arch_pkgs=yes DEB_CROSS_NO_BIARCH=yes dpkg-buildpackage --target-arch $*
endif

$(stamp)build-%: $(stamp)init
	@echo START $@
	set -e; \
	cd gcc-${PKG_VER_GCC}; \
	$(dpkg_target_cmd) -d -T control; \
	$(dpkg_target_cmd) -B -d -uc -us; \
	cd ..; \
	touch $@


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


BUILD_TARGET := $(patsubst %,$(stamp)build-%,${DEB_TARGET_ARCH})

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

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

# Get the list of binaries we care about
PACKAGES := $(patsubst  %,%*.deb, $(shell awk  '/^Package: / { print $$2 }' debian/control)) 

#
# Repack resulting packages to make changes:
# - Set Source and Version to this package 
# - Change Maintainer for cross packages
# - Get rid of unecessary gcc-<ver>-base dep - all docs are in cpp-<triplet> 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}\nXB-Original-Source-&/' tmp/DEBIAN/control; \
	sed -i -e's/gcc-${BASE_VER_GCC}-base[^,]*,//' 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);\
	dpkg-deb -b tmp/ ../$$deb; \
	echo "        done";
endef


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


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

define generate_debian_files
	@echo Debs: $1
	@for deb in $1; \
	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
endef

mangle-debian-files: $(stamp)debian-files-base repack-debs
	@echo START $@
	$(call generate_debian_files,  $(wildcard ${PACKAGES}))


# Generate the control file from head plus stanza for each binary
# Set the TARGET arch when doing it:
# DEB_TARGET_ARCH=armhf debian/rules control
# HOST=TARGET doesn't make sense.

control: debian/control.head.in debian/control.binary.in
	DEB_TARGET_ARCH=${DEB_TARGET_ARCH} ; \
	DEB_TARGET_GNU_TYPE=`dpkg-architecture -a$${DEB_TARGET_ARCH} -qDEB_HOST_GNU_TYPE -f 2>/dev/null`; \
	ARCH_LIST_FOR_BINARY=`echo ${HOST_LIST}`; \
	sed -e "s/DEB_TARGET_ARCH/$${DEB_TARGET_ARCH}/g" debian/control.head.in | \
	sed -e "s/BASE_VER_GCC/${BASE_VER_GCC}/g" | \
	sed -e "s/DEB_TARGET_GNU_TYPE/$${DEB_TARGET_GNU_TYPE}/g" > debian/control; \
	sed -e "s/DEB_TARGET_ARCH/$${DEB_TARGET_ARCH}/g" debian/control.binary.in | \
	sed -e "s/BASE_VER_GCC/${BASE_VER_GCC}/g" | \
	sed -e "s/ARCH_LIST_FOR_BINARY/$${ARCH_LIST_FOR_BINARY}/g" | \
	sed -e "s/DEB_TARGET_GNU_TYPE/$${DEB_TARGET_GNU_TYPE}/g" >> debian/control;
