#!/bin/sh
#
# $Id: getpatch,v 2.0 1996/01/15 06:07:04 hamilton Exp hamilton $
#
#
# getpatch written by Jon Hamilton (hamilton@mixcom.com) in a fit of
# boredom.  A crude hack, but it works.
#
# This will check to see which version of the kernel you are running, and
# get all the patches between there and the current release.  If you're
# not running linux, or if you want to specify some other "current version",
# you can say so on the command line, e.g.  ``getpatch 1.3.51'' will cause
# it to behave as if you are currently running 1.3.51.
#
# $PATCH_FTPDIR can be set if you don't like the default location, which
# is ~/kernel_patches.  The script will make sure that you don't already
# have a patch before ftping it.
#
# Be careful; there's not much error checking in this script.  Use at
# your own risk, etc.
#
# There are also some limitations, namely in the way $GETLIST is built, but
# it works for most "sane" uses.
#
# You may do with this as you like, but please leave the attribution and
# the instructional comments above in anything you modify or distribute.
#
###############################################################################
# (This is getpatch v2.1 by Kent Robotti 7-5-96) (The same as getpatch v2.0 
# by jon hamilton (I just configured it for you, so that it's ready to run.)
#
# ~# chmod u+x getpatch   <-Make getpatch executable.)
#
# You should have a /root/.netrc  <-Create this file and put this line in it.>
#
# default login anonymous password joe@soho.ios.com   <-your@email.address.)
#
# ~# getpatch        <-Start getpatch, after your connected slip/ppp etc.)
# The kernel patches will be put in.-> /usr/src/linux/scripts
# /usr/src/linux/scripts# chmod u+x patch-kernel   <-Make executable.)
# # patch-kernel     <-Start applying those patches.)
#
# It's set to start with kernel 2.0, if you have kernel 1.3.something, you'll
# have to change below.> MAJORVER=1.3 (It's best to update to 2.0.something.)  
###############################################################################

PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin
# You'll need to change this if you're after the 1.2.x kernels (or 1.5.x, when
# those come out)

MAJORVER=2.0

# Might be good net.citizenship to change these to reflect a mirror, too.

KERNELHOST=ftp.funet.fi
KERNELDIR=/pub/Linux/kernel/src/v$MAJORVER

PATCH_FTPDIR=${PATCH_FTPDIR:-/usr/src/linux/scripts}
TMPFILE=$PATCH_FTPDIR/data
export KERNELHOST KERNELDIR TMPFILE PATH PATCH_FTPDIR

ftpget(){
  cd $PATCH_FTPDIR
  echo "getting $* => $PATCH_FTPDIR"
  ftp -v $KERNELHOST << DONE
  cd $KERNELDIR
  prompt
  mget $*
DONE
}

get_current_release(){
ftp -v $KERNELHOST > $TMPFILE <<CEASE_ALREADY
cd $KERNELDIR
dir
CEASE_ALREADY

CURRENT_RELEASE=`grep LATEST-IS $TMPFILE | awk '{print $9}' | cut -c11-`
echo $CURRENT_RELEASE
}

if [ -n "$1" ] ; then
  CURRENT_VERSION=$1
elif [ "`uname -s`" = "Linux" ] ; then
  CURRENT_VERSION=`uname -r`
else
  echo "You're not running linux on this machine; please specify"
  echo "which kernel version you have on the command line."
  exit 1
fi

CURRENT_RELEASE=`get_current_release`
echo "Current release is $CURRENT_RELEASE, you're running $CURRENT_VERSION."

if [ "$CURRENT_RELEASE" != "$CURRENT_VERSION" ] ; then
# build a list of which patches we need to get
# start with the current release and work backwards
  if [ ! -f "$PATCH_FTPDIR/patch-$CURRENT_RELEASE.gz" ] ; then
    GETLIST=patch-$CURRENT_RELEASE.gz
  else
    unset GETLIST # might as well be paranoid
  fi
  SUCKED_MINOR=`echo $CURRENT_RELEASE | cut -c5-`
  HAVE_MINOR=`echo $CURRENT_VERSION | cut -c5-`
  while [ "$HAVE_MINOR" -ne "$SUCKED_MINOR" ] ; do
    SUCKED_MINOR=`expr $SUCKED_MINOR - 1`
    if [ ! -f $PATCH_FTPDIR/patch-$MAJORVER.$SUCKED_MINOR.gz ] ; then
      GETLIST="$GETLIST patch-$MAJORVER.$SUCKED_MINOR.gz"
    fi
  done
  if [ -z "$GETLIST" ] ; then
    echo "You already have all the patches up to $CURRENT_RELEASE."
  else
       ftpget $GETLIST
  fi
fi
