# packages/kernel-mk2x/auxbuild
#

# This is a big script and when it dies the error messages can be quite
# misleading to the actual source.  So we print some milestone messages.
milestone() {
	echo ":::: $* ::::"
}


# Pull in our local sources and patch sets.
# We need this first in order to work out which tarballs to pull from kernel.org
# create symlink to git repository
build_git_symlink "${BUILD_PACKAGE}"
[ ! -L Distn/kernel ] && ( cd Distn ; ln -s kernel-mk2x 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}/${KPATCHDIR}${PATCHFILE}"

    milestone "Applying official patches"
    ( 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
ABS_D="`pwd`/${D}"


# Patch in the YAFFS2 sources
milestone "Patching in YAFFS2"
( cd $S/yaffs2 ; ./patch-ker.sh c "${ABS_D}/" )


milestone "Applying local patches"
# 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"


milestone "Adding Guralp sources"
# 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/



# Clobber the automatic SCM versioning confused by our enclosing git environment
# which then adds a trailing + to the version number.
echo "" > $D/.scmversion


# Copy the default kernel config into place and build
# All DCM/EAM use the new EABI for kernels so drop -eabi from the name.
cp $S/defconfig.${BUILD_MACHINE%-eabi} $D/.config

case $BUILD_MACHINE in
CMG-DCM-mk2x)
    export ARCH="arm"
    export CROSS_COMPILE="arm-softfloat-linux-gnu-"
    ;;
CMG-DCM-mk4*)
    export ARCH="arm"
    export CROSS_COMPILE="arm-softfloat-linux-gnueabi-"
    ;;
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

milestone "Updating .config"
build_make "${PKGDIR}" "ARCH=$ARCH" "CROSS_COMPILE=$CROSS_COMPILE" "oldconfig"
milestone "Kernel build"
build_make "${PKGDIR}" "ARCH=$ARCH" "CROSS_COMPILE=$CROSS_COMPILE" CONFIG_DEBUG_SECTION_MISMATCH=y


# Now install the kernel image and modules
milestone "Kernel install"
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
INCDIR=$CROSSLIB/include/linux
if cmp $INCDIR/pps.h $D/include/linux/pps.h > /dev/null 2>&1
then
    : File exists and is identical
else
    milestone "Copying new pps header files to crosslib directory"
    mkdir -p $INCDIR
    cp $D/include/linux/pps.h $INCDIR/
fi


milestone "Final cleanup"
# 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
