#!/bin/bash
#
# GetKernel 1.1 for linux kernels (any version)
# Copyright Richard Harman Jr, 1998 <warewolf@xabean2.ml.org>
# Comments, Suggestions flames.. etc. are welcome.
# GPL
#
# If anyone wants to rewrite this in perl, please do!
# 
function count () {
if [ "$1" = "" ];then
        export START=1
        else
        export START=$1
fi
if [ "$2" = "to" ];then
        export END=$3
        else
        export END=100
fi
while [ "$START" != "$[$END+1]" ]
        do echo $START
        export START=$[$START+1]
done
}
# Configuration is here
AUTOAPPLY="yes"
USEMAKEFILE="yes"
MAKEFILE=/usr/src/linux/Makefile
FTP="ncftp -a"
# Config ends here.
##############################################################################
##############################################################################
if ! [ -r /usr/src/linux/Makefile ];then
echo Please chmod 444 /usr/src/linux/Makefile so I can read it!
USEMAKEFILE="no"
fi

if [ "$USEMAKEFILE" = "yes" ] ;then
	VERSION="`head -3 $MAKEFILE| grep VERSION | cut -f2 -d=|cut -f2 -d\ `"
	PATCHLEVEL="`head -3 $MAKEFILE| grep PATCHLEVEL | cut -f2 -d=|cut -f2 -d\ `"
	SUBLEVEL="`head -3 $MAKEFILE| grep SUBLEVEL | cut -f2 -d=|cut -f2 -d\ `"
	echo Current version in /usr/src/linux is $VERSION.$PATCHLEVEL.$SUBLEVEL
else
	VERSION=`uname -r | cut -f1 -d.`
	PATCHLEVEL=`uname -r | cut -f2 -d.`
	SUBLEVEL=`uname -r | cut -f3 -d.`
	echo Current \(running\) version is `uname -r`
fi

# Finger at linux.kernel.org to get the latest STABLE version
FINGERSTRING="`finger @linux.kernel.org | grep stable| cut -f2 -d:|cut -f2 -d\ `"

#################################################################
#   testing only. Uncomment if you want this program to BRAKE.  #
#FINGERSTRING="2.0.33"                                          #
#################################################################

# Parse out the version, patch and sublevel
GOTVERSION="`echo $FINGERSTRING | cut -f1 -d.`"
GOTPATCHLEVEL="`echo $FINGERSTRING | cut -f2 -d.`"
GOTSUBLEVEL="`echo $FINGERSTRING | cut -f3 -d.`"

# Make it easy to download the entire targz
FULL=/pub/linux/kernel/v$DIRECTORY/linux-$GOTVERSION.$GOTPATCHLEVEL.$GOTSUBLEVEL.tar.gz


# Oops! We've already got it!
if [ "$SUBLEVEL" = "$GOTSUBLEVEL" ];then
	echo Kernel is up to date, no need to download.
	exit 0
else
if [ "`echo $1| tr :upper: :lower:`" = "targz" ];then
$FTP "ftp://linux.kernel.org$FULL"
exit 0
fi
fi


# Alert user that we're gonna fill up their hard drive.
echo Kernel is out of date by $[$GOTSUBLEVEL-$SUBLEVEL]

# Optimize this idle loop.
for GETTINGSUBLEVEL in `count $SUBLEVEL to $GOTSUBLEVEL`
	do
		echo getting kernel patch version \
		$GOTVERSION.$GOTPATCHLEVEL.$GETTINGSUBLEVEL
 		PATCH=/pub/linux/kernel/v$VERSION.$PATCHLEVEL/patch-$GOTVERSION.$GOTPATCHLEVEL.$GETTINGSUBLEVEL.gz
		$FTP "ftp://linux.kernel.org$PATCH"
	done
fi

# auto apply to the kernel..
if [ "$AUTOAPPLY" = "yes" ];then
cp patch* /usr/src/linux
chmod 755 /usr/src/linux/scripts/patch-kernel
cd /usr/src/linux
scripts/patch-kernel
echo Kernel has been updated to \
$GOTVERSION.$GOTPATCHLEVEL.$GETTINGSUBLEVEL | mail $USER
fi

