#!/bin/bash
cd /usr/src/linux

# ccache doesn't seem to help, since each kernel build is unique (version
# info)

CC="distcc $MACHTYPE-gcc"
MAKEOPTS="-j5"

# safe mode
#CC=gcc
#MAKEOPTS="-j1"


if [ "$1" == "mod" ]; then
        nice make CC="$CC" $MAKEOPTS modules && \
        nice make CC="$CC" $MAKEOPTS modules_install 
	exit
else
        nice make CC="$CC" $MAKEOPTS zImage modules && \
        nice make CC="$CC" $MAKEOPTS modules_install || exit
fi

#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe3811031,4' # Linkstation Pro/Live (2.6.26)
devio > foo 'wl 0xe3a01c0a,4' 'wl 0xe3811067,4' # LS-XHL

cat foo arch/arm/boot/zImage > zImage.new
mkimage -A arm -O linux -T kernel -C none -a 0x00008000 -e 0x00008000 -n 'linux' -d zImage.new uImage.new
rm foo zImage.new

# backup the old kernel at /boot/uImage.buffalo and copy uImage.new in its place

for i in VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION; do
	eval $i=`grep "^$i =" Makefile | cut -d" " -f3`
done

for i in CONFIG_LOCALVERSION; do
        eval `grep "^$i=" .config`
done
        
KV=$VERSION.$PATCHLEVEL.$SUBLEVEL$EXTRAVERSION$CONFIG_LOCALVERSION
        
BI=/boot/uImage.buffalo

# old versions stay as backups, but only one older piece of the same
# kernelversion

if [ -f $BI-$KV ]; then
	mv -f $BI-$KV $BI-$KV.old
fi

cp uImage.new $BI-$KV

# wonder if a symlink would work
cp -f $BI-$KV $BI
