# packages/kernel/auxbuild
#

# Pull in our local sources and patch sets.
# We need this first in order to work out which tarballs to pull from kernel.org
# check out git repository (URL, ref, destination)
build_git_clone "ord:/srv/git/kernel" "stable_or_development" "kernel"

# Grab version information that goes with the patch set
. ./Distn/kernel/version.sourceme


# download and unpack tarballs
# Kernel.org now automatically redirects to a local mirror
SRCDIRURL="ftp://ftp.kernel.org/pub/linux/kernel/v2.6"

TARDIR="linux-${BASEVERSION}"
TARBALL="${TARDIR}.tar.bz2"
build_download "${SRCDIRURL}/${TARBALL}"
build_unpack "${TARBALL}"

# Apply offical patches if we're an intermediate version
if [ "$VERSION" != "$BASEVERSION" ]
then
    PATCHFILE="patch-${VERSION}.bz2"
    build_download "${SRCDIRURL}/testing/${PATCHFILE}"

    ( cd "Distn/$TARDIR" ;
	bzcat "../${PATCHFILE}" | patch -p1 ;
    )
    rm -rf "Distn/linux-${VERSION}"
    mv "Distn/$TARDIR" "Distn/linux-${VERSION}"
fi

PKGDIR="linux-${VERSION}"


# We now have a source and destination tree to play with
S=Distn/kernel
D=Distn/$PKGDIR


# Patch in the YAFFS2 sources
( cd $S/yaffs2 ; ./patch-ker.sh c ../../../$D/ )

# Create a link to the patches source
[ ! -d patches -a ! -L patches ] && ln -s $S/patches patches

# apply patches matching pattern (repository, patch prefix)
# Official kernel.org stable patches (kernel version 4th number updates)
#build_patch "${PKGDIR}"	"patch-*"
# Our patches from other sources
build_patch "${PKGDIR}"	"*.patch"

# Copy our additional sources into place for GPIO driver
cp $S/src/gpio/*.c			$D/drivers/gpio/

# NAND Flash drivers
cp $S/src/nand/*.c			$D/drivers/mtd/nand/

# Spyrus Lynks driver
cp $S/src/spyrus/*.c $S/src/spyrus/*.h	$D/drivers/char/pcmcia/

# Balloon2 support
cp $S/src/balloon2/balloon2.h		$D/arch/arm/mach-sa1100/include/mach/
cp $S/src/balloon2/balloon2_cpld.h	$D/arch/arm/mach-sa1100/include/mach/
cp $S/src/balloon2/balloon2_gpio.h	$D/arch/arm/mach-sa1100/include/mach/
cp $S/src/balloon2/balloon2_samosa.h	$D/arch/arm/mach-sa1100/include/mach/
cp $S/src/balloon2/balloon2_sdcmbi.h	$D/arch/arm/mach-sa1100/include/mach/
cp $S/src/balloon2/balloon2.c		$D/arch/arm/mach-sa1100/
cp $S/src/balloon2/balloon2_samosa.c	$D/arch/arm/mach-sa1100/
cp $S/src/balloon2/balloon2_sdcmbi.c	$D/arch/arm/mach-sa1100/
cp $S/src/watchdog/sdcmbi_wdt.c		$D/drivers/watchdog/



# Copy the default kernel config into place and build
cp $S/defconfig.$BUILD_MACHINE $D/.config

case $BUILD_MACHINE in
CMG-DCM-mk2x|CMG-DCM-mk4)
    export ARCH="arm"
    export CROSS_COMPILE="arm-softfloat-linux-gnu-"
    ;;
CMG-NAM)
    export ARCH="x86"
    export CROSS_COMPILE="i686-unknown-linux-gnu-"
    ;;
CMG-NAM64)
    export ARCH="x86"
    export CROSS_COMPILE="x86_64-unknown-linux-gnu-"
    ;;
*)
    echo "ERROR: unknown build machine $BUILD_MACHINE"
    exit 1
    ;;
esac

build_make "${PKGDIR}" "ARCH=$ARCH" "CROSS_COMPILE=$CROSS_COMPILE" "oldconfig"
build_make "${PKGDIR}" "ARCH=$ARCH" "CROSS_COMPILE=$CROSS_COMPILE"


# Now install the kernel image and modules
mkdir -p $BUILDROOT/boot

build_make "${PKGDIR}" "ARCH=$ARCH" "CROSS_COMPILE=$CROSS_COMPILE" \
			"INSTALL_MOD_PATH=$BUILDROOT/" "modules_install"


# Copy new pps header files to crosslib directory

echo "Copying new pps header files to crosslib directory"

INCDIR=$CROSSLIB/include/linux
mkdir -p $INCDIR

cp $D/include/linux/pps.h $INCDIR/


# Remove a couple of info symbolic links that make no sense on the target system
rm -f $BUILDROOT/lib/modules/*/build
rm -f $BUILDROOT/lib/modules/*/source

# Completion is machine type dependent
case $BUILD_MACHINE in
CMG-DCM-mk2x)
    # cp $D/arch/arm/boot/zImage $BUILDROOT/boot/

    # build Distn/boot directory
    rm -rf "Distn/boot"
    mkdir "Distn/boot"
    cp "$D/arch/arm/boot/zImage" "Distn/boot"
    echo "set linuxargs root=/dev/mtdblock1 console=ttySA0,115200" \
							> "Distn/boot/params"
    md5sum "Distn/boot/zImage" "Distn/boot/params" > "Distn/boot/md5sum"

    # build image file
    mkdir -p "${BUILDROOT}/usr/share/boot"
    mkyaffsimage.mk2x "Distn/boot" "${BUILDROOT}/usr/share/boot/boot.yaffs"
    cp "Distn/boot/md5sum" "${BUILDROOT}/usr/share/boot"
    ;;

CMG-DCM-mk4)
    cp $D/arch/arm/boot/zImage $BUILDROOT/boot/

    # Now add in the Marvell libertas firmware
    FWDIR=$BUILDROOT/lib/firmware
    FWSRC=$S/firmware
    mkdir -p $FWDIR/libertas
    cp -a $FWSRC/LICENSE $FWSRC/sd8686_v9.bin $FWSRC/sd8686_v9_helper.bin \
	$FWDIR/libertas/
    ( cd $FWDIR ;
	ln -s libertas/sd8686_v9_helper.bin sd8686_helper.bin ;
	ln -s libertas/sd8686_v9.bin sd8686.bin ;
    )
    ;;

CMG-NAM|CMG-NAM64)
    cp $D/arch/x86/boot/bzImage $BUILDROOT/boot/kernel
    ;;

*)
    echo "WARNING: unknown build machine $BUILD_MACHINE in finalisation"
    ;;
esac

true
# vim: ts=4:sw=4:expandtab:syntax=sh
