#!/usr/bin/make -f

DEB_NAME_ACT     := $(shell dpkg-parsechangelog| sed -n 's/-*//; s/^Source: \(.*\)/\1/p')
DEB_VER_ACT      := $(shell dpkg-parsechangelog| sed -n 's/-[^-]*//; s/^Version: \(.*\)/\1/p')
VER_GCC        := $(shell dpkg-parsechangelog -l/usr/src/gcc-4.4/debian/changelog | egrep '^Version:' | cut -f 2 -d ' '|cut -f 1 -d '-')
DEB_VER_GCC    := $(shell dpkg-parsechangelog -l/usr/src/gcc-4.4/debian/changelog | egrep '^Version:' | cut -f 2 -d ' ')

CROSS_ARCH       := $(shell dpkg-parsechangelog| sed -n 's/-[^-]*//; s/^Source: .*-\(.*\)-.*/\1/p')
CROSS_GNU_TYPE   := $(shell dpkg-architecture -a${CROSS_ARCH} -qDEB_HOST_GNU_TYPE -f)
HOST_ARCH        := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
HOST_GNU_TYPE    := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
stamp            := stamp-dir/

# taken from gcc packaging
define unpack_tarball
	tar -x -f $1
endef

info:
	@echo "GCC 4.4 version:          ${VER_GCC}     	${DEB_VER_GCC}"
	@echo
	@echo "Target architecture:      ${CROSS_ARCH}"
	@echo "Target GNU type:          ${CROSS_GNU_TYPE}"
	@echo "Host architecture:        ${HOST_ARCH}"
	@echo "Host GNU type:            ${HOST_GNU_TYPE}"

	@echo ${DEB_NAME_ACT}

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

$(stamp)init-gcc:
	mkdir gcc -p ; \
	cd gcc ; \
	ln -sf /usr/src/gcc-4.4/gcc-${VER_GCC}.tar.xz gcc-${VER_GCC}.tar.xz ;\
	cp -a  /usr/src/gcc-4.4/debian/ . ; \
	cp -a  /usr/src/gcc-4.4/patches debian/ ; \
	echo ${CROSS_ARCH} >debian/target
	touch $@

$(stamp)build-gcc: $(stamp)init-gcc
	# cross build do not do docs so we do not have all build dependencies
	cd gcc && NO_PKG_MANGLE=1 dpkg-buildpackage -b -uc -us -d
	touch $@


clean:
	rm -rf gcc4*
	rm -rf debian/tmp
	rm -rf $(stamp)
	mkdir $(stamp)

#TODO: replace DEB_LIST_ALL and DEB_LIST_ARCH by foreach() or wildcards
#      will need to remember to not package libgcc1 and gcc-4.4-*-base
#      as they are part of a-c-t-base

DEB_LIST_ARCH = \
           cpp-4.4-${CROSS_GNU_TYPE}_${DEB_VER_GCC}_${HOST_ARCH}.deb \
           g++-4.4-${CROSS_GNU_TYPE}_${DEB_VER_GCC}_${HOST_ARCH}.deb \
           gcc-4.4-${CROSS_GNU_TYPE}_${DEB_VER_GCC}_${HOST_ARCH}.deb \
           gcc-4.4-${CROSS_GNU_TYPE}-base_${DEB_VER_GCC}_${HOST_ARCH}.deb \
           gfortran-4.4-${CROSS_GNU_TYPE}_${DEB_VER_GCC}_${HOST_ARCH}.deb \
           gobjc-4.4-${CROSS_GNU_TYPE}_${DEB_VER_GCC}_${HOST_ARCH}.deb \
           gobjc++-4.4-${CROSS_GNU_TYPE}_${DEB_VER_GCC}_${HOST_ARCH}.deb

DEB_LIST_ALL = \
	   libmudflap0-4.4-dev-${CROSS_ARCH}-cross_${DEB_VER_GCC}_all.deb \
           libstdc++6-4.4-dbg-${CROSS_ARCH}-cross_${DEB_VER_GCC}_all.deb \
           libstdc++6-4.4-dev-${CROSS_ARCH}-cross_${DEB_VER_GCC}_all.deb \
           libstdc++6-4.4-pic-${CROSS_ARCH}-cross_${DEB_VER_GCC}_all.deb

define repack_debs
	mkdir tmp/DEBIAN -p
	echo "Source: ${DEB_NAME_ACT} (${DEB_VER_ACT})" > control.in
	for deb in $1; \
	do \
	    dpkg-deb -e $$deb tmp/DEBIAN; \
	    dpkg-deb -x $$deb tmp; \
	    egrep -v "^Source" tmp/DEBIAN/control >temp; \
	    cat control.in temp >tmp/DEBIAN/control; \
	    dpkg-deb -b tmp/ ../$$deb; \
	    rm -rf tmp/*; \
	done
endef

repack-debs-arch: build
	$(call repack_debs, ${DEB_LIST_ARCH})

repack-debs-indep: build
	$(call repack_debs, ${DEB_LIST_ALL})

build:	$(stamp)build-gcc

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

define generate_debian_control
	for deb in $1; \
	do \
	    dpkg-deb -e $$deb; \
	    egrep -v '^(Source|Version|Maintainer|Installed-Size|Original-Maintainer)' DEBIAN/control >>debian/control; \
	    echo "" >>debian/control; \
	done
endef

define generate_debian_files
	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-arch: $(stamp)debian-files-base build repack-debs-arch
	$(call generate_debian_files,   ../*_${HOST_ARCH}.deb)

mangle-debian-files-indep: $(stamp)debian-files-base repack-debs-indep
	$(call generate_debian_files,   ../*${CROSS_ARCH}-cross*_all.deb)

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

# not finished version as it should also add what mangle-debian-files-{arch,indep} do
regenerate-debian-control:
	@cp debian/control.in debian/control
	@echo "Build-Depends: binutils-multiarch (>= 2.20.1-13~)," >>debian/control
	@echo "  gcc-4.4-source (>= 4.4.1-5ubuntu1)," >> debian/control
	@echo "  binutils-arm-linux-gnueabi (>= 2.20.51.20100813-1ubuntu1)," >>debian/control
	@echo "  libc6-dev-armel-cross (>= 2.12.1-0ubuntu1)," >> debian/control
	@echo -n "  " >>debian/control
	@perl debian/control-grab-build-depends.pl \
	    /usr/src/gcc-4.4/debian/control \
	    >> debian/control
	@echo >>debian/control
	@echo -n "Build-Conflicts: " >> debian/control
	@perl debian/control-grab-build-conflicts.pl \
	    /usr/src/gcc-4.4/debian/control \
	    >> debian/control
	@echo >>debian/control
#         $(call generate_debian_control, ../*_${HOST_ARCH}.deb)
#         $(call generate_debian_control, ../*${CROSS_ARCH}-cross*_all.deb)
