# packages/iptables/auxbuild
#

# download and unpack tarball
TARVERSION="1.4.5"
TARDIR="${BUILD_PACKAGE}-${TARVERSION}"
TARBALL="${TARDIR}.tar.bz2"
build_download "http://iptables.org/projects/iptables/files/${TARBALL}"
build_unpack "${TARBALL}"
PKGDIR="${TARDIR}"

## apply patches matching pattern (repository, patch prefix)
#build_patch "${PKGDIR}" "${BUILD_PACKAGE}-ALL-"
#build_patch "${PKGDIR}" "${BUILD_PACKAGE}-${TARVERSION}-"


# We need a set of kernel sources to match the the kernel for $BUILD_MACHINE.
# We can't use those in ../kernel as they may be setup for a different machine.
# We pull and configure our own copy of the sources although we do cheat and
# try to link the source tarballs in from ../kernel/Distn

# Pull in our local kernel 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 kernel 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"

KTARDIR="linux-${BASEVERSION}"
KTARBALL="${KTARDIR}.tar.bz2"
if [ ! -e Distn/$KTARBALL ]
then
    if [ -e ../kernel/Distn/$KTARBALL ]
    then
	ln ../kernel/Distn/$KTARBALL Distn/
    else
	build_download "${SRCDIRURL}/${KTARBALL}"
    fi
fi
build_unpack "${KTARBALL}"

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

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

KPKGDIR="linux-${VERSION}"
S=Distn/kernel
D=Distn/$KPKGDIR

# We don't bother to patch the kernel sources with our drivers as we don't
# play to compile a kernel here.  The assumption being that we do nothing to
# affect iptables with our code.

# Copy the default kernel config into place and apply
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 "${KPKGDIR}" "ARCH=$ARCH" "CROSS_COMPILE=$CROSS_COMPILE" "oldconfig"


# We're now done building the kernel source.  Get back to iptables.


export KERNEL_DIR="../${KPKGDIR}"
export KBUILD_OUTPUT="${KERNEL_DIR}"
(
    cd "$D/include"
    [ ! -e "asm" ] && ln -s "asm-generic" "asm"
    true
)

#sed -e "s, sctp,," -i "Distn/$PKGDIR/extensions/Makefile"

# invoke configure (directory, prefix, extra arguments)
export CC=${CROSS_COMPILE}gcc
build_autoconf "${PKGDIR}" "/usr"

# clobber any badly added -L/usr/lib directives
find "Distn/${PKGDIR}" -type f -name 'Makefile' -exec \
	sed -i {} -e "s, -L/usr/lib , ," \;

# run make, make install (directory, extra arguments)
build_make "${PKGDIR}"

# stop libtool from adding -L/usr/lib; see http://www.metastatic.org/text/libtool.html
find "Distn/${PKGDIR}" -type f -name '*.la' -exec \
    sed -i {} -e "s,^libdir=.*\$,libdir=${BUILDROOT}/usr/lib," \;

build_make "${PKGDIR}" "install"

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