#! /bin/sh
# Script to grab a given kernel version (stolen from Virtual Anton).

barf()
{
    echo "$@" >&2
    exit 1
}

PATCHDIR=~/devel/kernel/kernel-patches
MIRRORS="proxy::kernel rsync.planetmirror.com::ftp.kernel.org/pub/linux/kernel master.kernel.org:/pub/linux/kernel"

# Takes destination directory, and filename, tries each mirror.
get_file()
{
    get_file_dir=$1
    get_file_name=$2

    # Patch there already?
    if [ -f "$get_file_dir"/"$(basename "$get_file_name")" ]; then
	echo " Already have patch..."
	return 0
    fi

    for get_file_server in $MIRRORS; do
	rsync $get_file_server/$get_file_name $get_file_dir && return 0
    done
    barf "Couldn't fetch $1"
}

if [ $# -ne 2 ]; then
    echo Usage: $0 version kerneldir >&2
    exit 1
fi

VERSION=$1
KDIR=$2

if [ -d $KDIR/linux-$VERSION ]; then exit 0; fi
MAJOR=`echo $VERSION | cut -d. -f1-2`

echo Grabbing $VERSION
case "$VERSION" in
    *-ac*)
	# First make sure we have base tree
	BASE_KERNEL=`echo "$VERSION" | sed -e 's/-ac.*//'`
	$0 "$BASE_KERNEL" $KDIR || barf "Couldn't get $BASE_KERNEL to build $VERSION"
	rm -rf $KDIR/.linux-$VERSION
	cp -al $KDIR/linux-$BASE_KERNEL $KDIR/.linux-$VERSION ||
	    barf "Couldn't create -ac from $BASE_KERNEL"
	NO_PRE=`echo "$BASE_KERNEL" | sed 's/-\(pre\|rc\).*//'`
	get_file $PATCHDIR people/alan/linux-$MAJOR/$NO_PRE/patch-$VERSION.bz2 
	bunzip2 $PATCHDIR/patch-$VERSION.bz2 ||
	    barf "Problem unpacking patch for $VERSION"
	patch -p1 -s -d $KDIR/.linux-$VERSION < $PATCHDIR/patch-$VERSION ||
	    barf "Problem patching AC kernel $VERSION"
	mv $KDIR/.linux-$VERSION $KDIR/linux-$VERSION
	bzip2 ~/patch-$VERSION
	;;

    *-dj*)
	# First make sure we have base tree
	BASE_KERNEL=`echo "$VERSION" | sed 's/-dj.*//'`
	$0 "$BASE_KERNEL" $KDIR || barf "Couldn't get $BASE_KERNEL to build $VERSION"
	rm -rf $KDIR/.linux-$VERSION
	cp -al $KDIR/linux-$BASE_KERNEL $KDIR/.linux-$VERSION ||
	    barf "Couldn't create -dj from $BASE_KERNEL"
	NO_PRE=`echo "$BASE_KERNEL" | sed 's/-\(pre\|rc\).*//'`
	get_file $PATCHDIR people/davej/patches/$MAJOR/$NO_PRE/patch-$VERSION.diff.bz2 
	bunzip2 $PATCHDIR/patch-$VERSION.diff.bz2 ||
	    barf "Problem unpacking patch for $VERSION"
	patch -p1 -s -d $KDIR/.linux-$VERSION < $PATCHDIR/patch-$VERSION.diff ||
	    barf "Problem patching DJ kernel $VERSION"
	mv $KDIR/.linux-$VERSION $KDIR/linux-$VERSION
	bzip2 $PATCHDIR/patch-$VERSION.diff
	;;

    ##   *-pre*: This refers to the Linus or Marcelo pre-patches: rsyncing
    ##     the appropriate patch from proxy.
    *-pre*|*-rc*)
	# Linus or Marcelo pre-patch.
	if [ -d $KDIR/linux-$VERSION ]; then exit 0; fi

	PREVIOUS=`echo "$VERSION" | sed 's/-\(pre\|rc\).*//'`
	LAST_VER=`echo "$PREVIOUS" | cut -d. -f3`
	PREVIOUS=`echo "$PREVIOUS" | cut -d. -f1,2`.`expr $LAST_VER - 1`
	$0 "$PREVIOUS" $KDIR || barf "Couldn't get $PREVIOUS to build $VERSION"

	# Grab patch
	get_file $PATCHDIR v$MAJOR/testing/patch-$VERSION.bz2 
	rm -rf $KDIR/.linux-$VERSION
	cp -al $KDIR/linux-$PREVIOUS $KDIR/.linux-$VERSION ||
	    barf "Couldn't create -pre from $PREVIOUS"
	bunzip2 $PATCHDIR/patch-$VERSION.bz2 || barf "Problem unpacking pre patch for $VERSION"
	patch -s -d $KDIR/.linux-$VERSION -p1 < $PATCHDIR/patch-$VERSION ||
	    barf "Problem patching new kernel $VERSION"
	mv $KDIR/.linux-$VERSION $KDIR/linux-$VERSION
	bzip2 $PATCHDIR/patch-$VERSION
	;;

    ##   *.0: This refers to the base of the Linus kernels, fetched whole
    ##     from proxy.
    *.0)
	# Linus' root of all trees.
	get_file $PATCHDIR v$MAJOR/linux-$VERSION.tar.bz2
	bzcat $PATCHDIR/linux-$VERSION.tar.bz2 | (cd $KDIR && tar xf -) ||
	    barf "Problem unpacking linux-$VERSION.tar.bz2"
	mv $KDIR/linux $KDIR/linux-$VERSION
	;;

    ##   [1-9].*.*: This refers to the any other Linus kernel: patches from
    ##     the previous version are rsync'ed from proxy.
    [1-9].*.*)
	# Linus standard ?
	if [ -d $KDIR/linux-$VERSION ]; then exit 0; fi

	LAST_VER=`echo "$VERSION" | cut -d. -f3`
	PREVIOUS=`echo "$VERSION" | cut -d. -f1,2`.`expr $LAST_VER - 1`
	$0 "$PREVIOUS" $KDIR || barf "Couldn't get $PREVIOUS to build $VERSION"

	# Grab patch
	get_file $PATCHDIR v$MAJOR/patch-$VERSION.bz2
	rm -rf $KDIR/.linux-$VERSION
	cp -al $KDIR/linux-$PREVIOUS $KDIR/.linux-$VERSION ||
	    barf "Couldn't create new kernel from $PREVIOUS"
	bunzip2 $PATCHDIR/patch-$VERSION.bz2 || barf "Problem unpacking patch for $VERSION"
	patch -d $KDIR/.linux-$VERSION -p1 < $PATCHDIR/patch-$VERSION ||
	    barf "Problem patching new kernel $VERSION"
	mv $KDIR/.linux-$VERSION $KDIR/linux-$VERSION
	bzip2 $PATCHDIR/patch-$VERSION
	;;
    *)
	barf "Unknown kernel version $VERSION"
	;;
esac
