#!/bin/bash
set -x
set -e
HEAD=$(git show --pretty=oneline | head -n1 | cut -d' ' -f1 | cut -b1-16)
# TODO: check for uncommitted changes by default?
BRANCH=$(git branch | grep ^\* | cut -d' ' -f2)
VERSION=$BRANCH\_$HEAD
: ${BUILDROOT:=build-$VERSION}
: ${CONFIG:=arch/arm/configs/gta02_defconfig}
: ${PARALLEL:=4}
: ${CROSS_COMPILE:=/usr/local/openmoko/arm/bin/arm-angstrom-linux-gnueabi-}
export CROSS_COMPILE

rm -fr $BUILDROOT
mkdir $BUILDROOT
cp $CONFIG $BUILDROOT/.config
make O=$BUILDROOT ARCH=arm silentoldconfig
make -j$PARALLEL O=$BUILDROOT ARCH=arm CONFIG_DEBUG_SECTION_MISMATCH=y EXTRAVERSION=$VERSION
${CROSS_COMPILE}objcopy -O binary -R .note -R .comment -S $BUILDROOT/arch/arm/boot/compressed/vmlinux $BUILDROOT/linux.bin
mkimage -A arm -O linux -T kernel -C none -a 30008000 -e 30008000 -n $VERSION -d $BUILDROOT/linux.bin $BUILDROOT/uImage-$VERSION.bin
mkdir -p $BUILDROOT/staging
make O=$BUILDROOT ARCH=arm modules_install INSTALL_MOD_PATH=staging
cd $BUILDROOT/staging
tar czf ../modules-$VERSION.tar.gz .

echo Congratulations, your kernel has now been built.
echo Please unpack $BUILDROOT/modules-$VERSION.tar.gz to the root of your Openmoko
echo and then boot $BUILDROOT/uImage-$VERSION.bin using either U-boot or Qi.
echo Alternatively you can boot $BUILDROOT/arch/arm/boot/zImage with kexec.

