#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

ifneq (,$(findstring parallel,$(DEB_BUILD_OPTIONS)))
    PARALLEL_JOBS := $(shell echo $(DEB_BUILD_OPTIONS) | \
        sed -e 's/.*parallel=\([0-9]\+\).*/\1/')
    ifeq ($(DEB_BUILD_OPTIONS),$(PARALLEL_JOBS))
        PARALLEL_JOBS := $(shell if [ -f /proc/cpuinfo ]; \
            then echo `cat /proc/cpuinfo | grep 'processor' | wc -l`; \
            else echo 1; fi)
    endif
    NJOBS := -j$(PARALLEL_JOBS)
endif

CFLAGS = -Wall -g
CROSS_COMPILE=arm-none-linux-gnueabi-
#PATH=$PATH:/opt/arm2008q1/bin


ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	tar -xzf u-boot-bd2313078114c4b44c4a5ce149af43bcb7fc8854.tar.gz
	mv u-boot/* ./ && rm -rf u-boot/
	QUILT_PATCHES=debian/patches quilt push -a -q || test $$? = 2
	make nokia_rx51_config
	cd /usr/src && tar -xjf kernel-power-source.tar.bz2 && cd kernel-power-source && $(MAKE) rx51power_defconfig && time $(MAKE) $(NJOBS) EXTRAVERSION=.10power46 bzImage
	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp
	dh_testdir
	$(MAKE) $(NJOBS)
	mkdir -p uImage-builddir
	cp /usr/src/kernel-power-source/arch/arm/boot/zImage ./uImage-builddir
	cp `find -name u-boot.bin` uImage-builddir/
	cp builder.py uImage-builddir/
	cd uImage-builddir && mkimage -A arm -O linux -T kernel -C none -a 80008000 -e 80008000 -n "Maemo 5 - kernel-power-2.6.28 46" -d zImage uImage
	cd uImage-builddir && python2.5 builder.py
	cd uImage-builddir && fiasco-gen -k combined.bin -g -o kernel-power-46-uboot.fiasco
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	#$(MAKE) clean
	#QUILT_PATCHES=debian/patches quilt pop -a -q -R || test $$? = 2
	-rm -rf .pc
	if [ -d uImage-builddir ]; then rm -rf uImage-builddir; fi
	debian/cleansource.sh
	rm -f build-stamp configure-stamp
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs debian/changelog
	dh_install
#	dh_python
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
