#!/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.


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=1.3

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

KERNELHOST=ftp.cs.helsinki.fi
KERNELDIR=/pub/Software/Linux/Kernel/v$MAJORVER

PATCH_FTPDIR=${PATCH_FTPDIR:-$HOME/kernel-patches}
TMPFILE=$PATCH_FTPDIR/data
export KERNELHOST KERNELDIR TMPFILE PATH PATCH_FTPDIR

ftpget(){
  cd $PATCH_FTPDIR
  echo "getting $* => $PATCH_FTPDIR"
  ftp -n $KERNELHOST << DONE
  user anonymous $USER@`hostname`
  cd $KERNELDIR
  prompt
  mget $*
DONE
}

get_current_release(){
ftp -n $KERNELHOST > $TMPFILE <<CEASE_ALREADY
user anonymous $USER@`hostname`
cd $KERNELDIR
dir
CEASE_ALREADY

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

if [ ! -d $PATCH_FTPDIR ] ; then
  mkdir $PATCH_FTPDIR || {
    echo "can't mkdir $PATCH_FTPDIR?!"
    exit 1
  }
fi

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
