#
# Copyright (c) 1999 Madge Networks Ltd.
#
# Script to patch the kernel
#
# TEG 14/09/1999
#/********************************************************************
#*	THE USE BY YOU OF THIS SOFTWARE IS GOVERNED IN ALL
#*	RESPECTS BY THE MADGE NETWORKS LTD OPEN SOURCE LICENSE.		
#*	WE ADVISE YOU TO READ THE TERMS OF THE OPEN SOURCE LICENSE
#*	WHICH CAN BE FOUND WITHIN THE LICENSE.TXT FILE WHICH IS PART OF
#*	THE SOFTWARE OR BY VISITING THE URL
#*	http://www.madge.com/Connect/Downloads/Software/.		
#*	MADGE NETWORKS LTD RESERVES THE RIGHT TO SEEK REDRESS FOR
#*	ANY VIOLATION OF THE LICENSE'S TERMS					
#*********************************************************************/


FUNCS=`dirname $0`/functions

if [ ! -r $FUNCS ] ; then 
	echo 'Couldnt find file functions'
	echo 'Broken Madge Linux Token Ring Driver distribution?'
	exit 1
fi

. $FUNCS

echo ""
echo Madge Kernel Patching Script
echo ""
echo Once a kernel source tree is patched using this script the 
echo patch cannot be removed easily.
echo You can deselect Madge support if necessary to build a kernel
echo as if it wasn\'t patched.
echo ""

KERNEL=/usr/src/linux

ask_str "Enter location of kernel to patch" KERNEL nowrite

if [ -f $KERNEL/.madge-patch ] ; then
	echo This source tree appears to already have been patched
	exit 1
fi

if ! [ -f $KERNEL/Makefile ] ; then
	echo Could not find a Linux source tree in $KERNEL
	exit 1
fi

for TAG in VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION ; do
	eval $(sed -ne "/^$TAG/s/ //gp" $KERNEL/Makefile)
done

echo Found a $VERSION.$PATCHLEVEL.$SUBLEVEL$EXTRAVERSION kernel in $KERNEL

PATCH=patch-$VERSION.$PATCHLEVEL.$SUBLEVEL$EXTRAVERSION
if [ ! -f $PATCH ] ; then
	if [ $(version $VERSION $PATCHLEVEL $SUBLEVEL) -gt $(version 2 0 0) ] && \
   		[ $(version $VERSION $PATCHLEVEL $SUBLEVEL) -lt $(version 2 0 39) ] ; then
		PATCH=patch-2.0.36
	elif [ $(version $VERSION $PATCHLEVEL $SUBLEVEL) -gt $(version 2 2 0) ] && \
	     [ $(version $VERSION $PATCHLEVEL $SUBLEVEL) -lt $(version 2 2 12) ] ; then
		PATCH=patch-2.2.10
	elif [ $(version $VERSION $PATCHLEVEL $SUBLEVEL) -ge $(version 2 2 12) ] ; then
		PATCH=patch-2.2.12
	else
		echo "ERROR: Trying to patch unknown Linux version"
		echo "Please use Kernel 2.0.36 or 2.2.10 for best results"
		exit 1
	fi
fi

echo Using $PATCH to patch the kernel in $KERNEL

#
# Copy all the required files into the source tree
#
mkdir -p $KERNEL/drivers/net/madge
cp udt/* $KERNEL/drivers/net/madge
cp linux/Makefile $KERNEL/drivers/net/madge
cp linux/Config.in $KERNEL/drivers/net/madge
cp madgetr.c $KERNEL/drivers/net
cp *.h *.upd $KERNEL/drivers/net

echo ""
echo You may see warnings from the patch program.  These can generally be
echo ignored if you are patching a kernel version close to 2.0.36 or 2.2.10
echo If your kernel is a wildly different version to 2.0.36 or 2.2.10 then
echo exercise caution when using your new kernel as it may not work.
echo ""
OLDDIR=$(pwd)
cd $KERNEL
patch -p0 < $OLDDIR/$PATCH
cd $OLDDIR

touch $KERNEL/.madge-patch

echo "Please reconfigure your kernel using \"make config\", \"make menuconfig\","
echo "or \"make xconfig\" to include support for Madge Token Ring Adapters."
echo ""
