#!/usr/bin/make -f

INSTALL_FILE    := install -m 644
INSTALL_PROGRAM := install -m 755
INSTALL_DIR     := install -m 755 -d

version := $(shell dpkg-parsechangelog | sed -n 's/^Version: //p')
tools_version := 20081215-3+$(version)
kernel_version := $(shell dpkg-query -W -f \$${Version} kernel-bootimg)

package := u-boot-bootimg
flash_package := u-boot-flasher
tools_package := u-boot-tools

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
export CROSS_COMPILE ?= $(DEB_HOST_GNU_TYPE)-
endif

# the upstream build passes LDFLAGS directly to ld instead of calling gcc for
# linking; so instead of passing -Wl,foo in LDFLAGS as in automake builds, one
# should set LDFLAGS to foo directly
comma := ,
LDFLAGS := $(patsubst -Wl$(comma)%,%,$(LDFLAGS))

# with -Bsymbolic-functions flag u-boot is not working
LDFLAGS := $(subst -Bsymbolic-functions,,$(LDFLAGS))

# macro to test whether a host header is present
check_include = $(shell echo | $(CROSS_COMPILE)gcc -E -include $(1) -o /dev/null - 2>/dev/null && echo 1 || echo 0)

build-indep:

build: build-arch build-indep
build-arch:
	$(checkdir)

	set -e; sed -n 's/^$(DEB_HOST_ARCH)[[:space:]]\+//p' debian/targets \
	    | while read platform target; do \
	        builddir=`pwd`/debian/build/$$platform; \
	        installdir=debian/$(package)/boot; \
	        precompiled=precompiled-$$platform; \
	        $(INSTALL_DIR) $$installdir; \
	        if test -f $$precompiled; then \
	            $(INSTALL_FILE) -D $$precompiled $$installdir/u-boot.bin; \
	            continue; \
	        fi; \
	        mkdir -p $$builddir; \
	        $(MAKE) O=$$builddir $${platform}_config; \
	        $(MAKE) O=$$builddir $$builddir/$$target; \
	        $(INSTALL_FILE) $$builddir/$$target $$installdir; \
	done

	# board-independent tools
	$(INSTALL_DIR) debian/$(tools_package)/usr/bin/
	$(MAKE) HOSTCC=$(CROSS_COMPILE)gcc HOSTSTRIP=$(CROSS_COMPILE)strip \
	    tools
	$(INSTALL_PROGRAM) tools/mkimage debian/$(tools_package)/usr/bin/

	touch $@

clean:
	$(checkdir)
	rm -f debian/files debian/*.substvars
	rm -rf debian/$(package) debian/$(flash_package) debian/$(tools_package)
	rm -f build-arch
	$(MAKE) distclean
	rm -rf arch/arm/include/asm/arch arch/arm/include/asm/proc
	rm -rf debian/build
	rm -f `find . -name "*~"`

binary-indep:	checkroot
	$(checkdir)

binary-arch:	checkroot build
	$(checkdir)

	$(INSTALL_DIR) debian/$(package)/DEBIAN

	$(INSTALL_DIR) debian/$(package)/usr/share/doc/$(package)/
	$(INSTALL_FILE) debian/README.Debian \
	    debian/$(package)/usr/share/doc/$(package)/
	$(INSTALL_FILE) debian/copyright \
	    debian/$(package)/usr/share/doc/$(package)/
	$(INSTALL_FILE) debian/changelog \
	    debian/$(package)/usr/share/doc/$(package)/changelog.Debian

	$(INSTALL_DIR) debian/$(package)/opt/
	mv debian/$(package)/boot/ debian/$(package)/opt/boot/
	mv debian/$(package)/opt/boot/u-boot.bin debian/$(package)/opt/boot/u-boot-$(version).bin

	$(INSTALL_DIR) debian/$(package)/etc/bootmenu.d
	rm -f debian/$(package)/etc/bootmenu.d/30-U-Boot-$(version).item
	echo 'ITEM_NAME="U-Boot $(version)"' >> debian/$(package)/etc/bootmenu.d/30-U-Boot-$(version).item
	echo 'ITEM_KERNEL="u-boot-$(version).bin"' >> debian/$(package)/etc/bootmenu.d/30-U-Boot-$(version).item
	echo 'ITEM_OMAPATAG="1"' >> debian/$(package)/etc/bootmenu.d/30-U-Boot-$(version).item

	cp debian/$(package).postinst debian/$(package)/DEBIAN/postinst
	cp debian/$(package).postrm debian/$(package)/DEBIAN/postrm
	chmod 755 debian/$(package)/DEBIAN/postinst debian/$(package)/DEBIAN/postrm

	gzip -9f `find debian/$(package)/usr/share/doc -type f ! -name "copyright"`
	dpkg-gencontrol -ldebian/changelog -isp -p$(package) -Tdebian/$(package).substvars -Pdebian/$(package)
	cd debian/$(package) && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums

	chown -R root:root debian/$(package)
	chmod -R go=rX debian/$(package)

	dpkg --build debian/$(package) ..


	$(INSTALL_DIR) debian/$(tools_package)/DEBIAN

	$(INSTALL_DIR) debian/$(tools_package)/usr/share/doc/$(tools_package)/
	$(INSTALL_FILE) debian/copyright     debian/$(tools_package)/usr/share/doc/$(tools_package)/
	$(INSTALL_FILE) debian/changelog     debian/$(tools_package)/usr/share/doc/$(tools_package)/changelog.Debian

	$(INSTALL_DIR) debian/$(tools_package)/usr/bin/
	$(INSTALL_PROGRAM) debian/u-boot-update-bootmenu    debian/$(tools_package)/usr/bin/
	$(INSTALL_PROGRAM) debian/u-boot-gen-combined       debian/$(tools_package)/usr/bin/

	$(INSTALL_DIR) debian/$(tools_package)/usr/share/man/man1
	$(INSTALL_FILE) doc/mkimage.1                 debian/$(tools_package)/usr/share/man/man1/

	gzip -9f `find debian/$(tools_package)/usr/share/doc -type f ! -name "copyright"`
	gzip -9f `find debian/$(tools_package)/usr/share/man -type f`

	dpkg-shlibdeps -Tdebian/$(tools_package).substvars \
	    debian/$(tools_package)/usr/bin/*
	dpkg-gencontrol -ldebian/changelog -isp -p$(tools_package) -Tdebian/$(tools_package).substvars -Pdebian/$(tools_package)
	cd debian/$(tools_package) && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums

	chown -R root:root debian/$(tools_package)
	chmod -R go=rX debian/$(tools_package)

	dpkg --build debian/$(tools_package) ..


	$(INSTALL_DIR) debian/$(flash_package)/DEBIAN

	$(INSTALL_DIR) debian/$(flash_package)/usr/share/doc/$(flash_package)/
	$(INSTALL_FILE) debian/copyright     debian/$(flash_package)/usr/share/doc/$(flash_package)/
	$(INSTALL_FILE) debian/changelog     debian/$(flash_package)/usr/share/doc/$(flash_package)/changelog.Debian

	$(INSTALL_DIR) debian/$(flash_package)/boot
	debian/$(tools_package)/usr/bin/mkimage -A arm -O linux -T kernel -C none -a 80008000 -e 80008000 -n zImage-2.6.28-omap1 -d /boot/zImage-2.6.28-omap1 uImage
	debian/$(tools_package)/usr/bin/u-boot-gen-combined debian/$(package)/opt/boot/u-boot-$(version).bin uImage combined.bin
	fiasco-gen -o debian/$(flash_package)/boot/u-boot-$(version)+zImage-2.6.28-omap1.fiasco -g -k combined.bin -v u-boot-$(version)
	rm -f uImage combined.bin

	sed "s/@image@/u-boot-$(version)+zImage-2.6.28-omap1/" debian/$(flash_package).postinst.in > debian/$(flash_package)/DEBIAN/postinst
	chmod 755 debian/$(flash_package)/DEBIAN/postinst

	gzip -9f `find debian/$(flash_package)/usr/share/doc -type f ! -name "copyright"`
	dpkg-gencontrol -ldebian/changelog -isp -p$(flash_package) -Tdebian/$(flash_package).substvars -Pdebian/$(flash_package)
	sed "s/kernel-modules/kernel-modules (= $(kernel_version))/g" -i debian/$(flash_package)/DEBIAN/control
	cd debian/$(flash_package) && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums

	chown -R root:root debian/$(flash_package)
	chmod -R go=rX debian/$(flash_package)

	dpkg --build debian/$(flash_package) ..

define checkdir
	test -f debian/rules
endef

binary:		binary-arch binary-indep

prebuild:
	@true

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: build build-indep binary binary-arch binary-indep clean checkroot prebuild
