#!/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


STOCKMODDIR=$(CURDIR)/src/omap1
POWERMODDIR=$(CURDIR)/src/power40
KPSRC=/usr/src/kernel-power-2.6.28

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
EXTRA_CFLAGS += -DENABLE_MUSB_CONTROLLER

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

configure: configure-stamp
configure-stamp:
	dh_testdir
	cd /usr/src
	if ! grep "deb-src http://repository.maemo.org/extras-devel fremantle free" /etc/apt/sources.list >/dev/null 2>&1; then echo "deb-src http://repository.maemo.org/extras-devel fremantle free" >> /etc/apt/sources.list && apt-get update; fi
	if [ -d $(KPSRC) ]; then rm -rf $(KPSRC); fi 
	cd /usr/src && apt-get source kernel-power
	cd $(KPSRC)
	cd $(KPSRC) && patch -p1 <debian/patches/nokia-20094803.3+0m5.diff
	cd $(KPSRC) && patch -p1 <debian/patches/nokia-20100903+0m5.diff
	cd $(KPSRC) && patch -p1 <debian/patches/nokia-20101501+0m5.diff
	cd $(KPSRC) && patch -p1 <debian/patches/maemo-build.diff
	cd $(KPSRC) && patch -p1 <debian/patches/unionfs-2.5.3.diff
	cd $(KPSRC) && patch -p1 <debian/patches/dm-loop.diff
	cd $(KPSRC) && patch -p1 <debian/patches/usbip.diff
	cd $(KPSRC) && patch -p1 <debian/patches/iphb-matan.diff
	cd $(KPSRC) && patch -p1 <debian/patches/ppp_async_matan.diff
	cd $(KPSRC) && patch -p1 <debian/patches/2.6.28.10.diff
	cd $(KPSRC) && patch -p1 <debian/patches/block2mtd-yoush.diff
	cd $(KPSRC) && patch -p1 <debian/patches/gentoo-fsfixes.diff
	cd $(KPSRC) && patch -p1 <debian/patches/kexec.diff
	cd $(KPSRC) && patch -p1 <debian/patches/bq_matan.diff
	cd $(KPSRC) && patch -p1 <debian/patches/leds-lp5523.diff
	cd $(KPSRC) && patch -p1 <debian/patches/trig-keyb.diff
	cd $(KPSRC) && patch -p1 <debian/patches/twl-scrollock.diff
	cd $(KPSRC) && patch -p1 <debian/patches/squashfs.diff
	cd $(KPSRC) && patch -p1 <debian/patches/led_dbrightness.diff
	cd $(KPSRC) && patch -p1 <debian/patches/wl1251-monitor-mode.diff
	cd $(KPSRC) && patch -p1 <debian/patches/mmcnames-fanoush.diff
	cd $(KPSRC) && patch -p1 <debian/patches/gethercharge.diff
	cd $(KPSRC) && patch -p1 <debian/patches/ondemand-avoid.diff
	cd $(KPSRC) && patch -p1 <debian/patches/overclock.diff
	cd $(KPSRC) && patch -p1 <debian/patches/armthumb.diff
	cd $(KPSRC) && cp debian/rx51power_defconfig arch/arm/configs/ && make rx51power_defconfig && make -j $(NJOBS) EXTRAVERSION=.10power40 bzImage
	cd /usr/src && tar -xjvf kernel-source.tar.bz2 && cd kernel-source && make rx51_defconfig && time make -j $(NJOBS) EXTRAVERSION=-omap1 bzImage
	cd $(CURDIR)
	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp
	dh_testdir
	cd $(STOCKMODDIR) && $(MAKE) -C /usr/src/kernel-source M=`pwd` EXTRAVERSION=-omap1 EXTRA_CFLAGS=-DENABLE_MUSB_CONTROLLER modules
	cd $(POWERMODDIR) && $(MAKE) -C /usr/src/kernel-power-2.6.28 M=`pwd` EXTRAVERSION=.10power40 EXTRA_CFLAGS=-DENABLE_MUSB_CONTROLLER modules
	cd $(CURDIR)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	cd $(STOCKMODDIR) && $(MAKE) clean
	cd $(POWERMODDIR) && $(MAKE) clean
	rm -f build-stamp configure-stamp
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_install
	cp -r $(CURDIR)/data/resources $(CURDIR)/debian/psfreedom/opt/psfreedom/

# 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_installdocs
#	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_python
#	dh_installinit
#	dh_installcron
#	dh_installinfo
#	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_makeshlibs
	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
