#!/bin/sh
#
# Kernel building helper script (C)2008 Openmoko, Inc
# Andy Green <andy@openmoko.org>
#
# Licensed under GPLv3 or later
#
#
# this radically speeds up swapping between build contexts.  Note the config
# for each build lives in the subdir.

export CROSS_COMPILE=/usr/local/openmoko/arm/bin/arm-angstrom-linux-gnueabi-

#
# figure out what we are building
#

PRODUCT=

if [ ! -z "`grep CONFIG_MACH_NEO1973_GTA01=y ./.config`" ] ; then
	START=30008000
	PRODUCT=GTA01
fi

if [ ! -z "`grep CONFIG_MACH_NEO1973_GTA02=y ./.config`" ] ; then
	START=30008000
	PRODUCT=GTA02
fi

if [ ! -z "`grep CONFIG_MACH_OPENMOKO_GTA03=y ./.config`" ] ; then
        START=50008000
        PRODUCT=GTA03
fi

if [ ! -z "`grep CONFIG_MACH_SMDK6410=y ./.config`" ] ; then
        START=50008000
        PRODUCT=SMDK6410
fi

if [ ! -z "`grep CONFIG_MACH_M800=y ./.config`" ] ; then
	START=30008000
	PRODUCT=M800
fi

if [ -z "$PRODUCT" ] ; then
	echo "Unable to figure out what we are building from the config"
	exit 1
fi

#
# get the branch and head hash for the version we are building to
# allow source tracability
#

VERSION=
if [ -d .git ] ; then
 HEAD=`git show --pretty=oneline | head -n1 | cut -d' ' -f1 | cut -b1-16`
 VERSION=-$PRODUCT
fi


echo $MKIMAGECMD

# you can get mkimage from U-Boot or Qi build
#

${CROSS_COMPILE}objcopy -O binary -R .note -R .comment -S ./arch/arm/boot/compressed/vmlinux ./linux.bin
mkimage -A arm -O linux -T kernel -C none -a $START -e $START -n "OM $PRODUCT $BRANCH""_$HEAD" -d ./linux.bin ./uImage-$PRODUCT.bin

# we can see if it is an "moredrivers" build by looking for USB Eth gadget
# if it is then keep a stamped copy of last build

if [ ! -z "`grep CONFIG_USB_USBNET=y ./.config`" ] ; then
	rm -f ./uImage-moredrivers-$PRODUCT*.bin ./modules-$PRODUCT*.tar.gz
	cp ./uImage-$PRODUCT.bin ./uImage-moredrivers$VERSION-$HEAD.bin
	rm -rf ./staging
	mkdir -p ./staging
	if [ ! -z "$2" ] ; then
		make O=. ARCH=arm modules_install INSTALL_MOD_PATH=staging
		cd ./staging
		tar czf ../modules$VERSION-$HEAD.tar.gz .
		cd ../..
	fi
fi
