#! /bin/bash
#
# ================================================[ Copyright Section ]===
# Patchk: Patch the Linux Kernel to a Newer Version.
# Copyright (C) 1996-97 Ryan McGuire. 
# Version 2.1.40.
# ========================================================================
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# [ Load config file, if found ]-----------------------------------------=

if [ -e ./.ksconfig ]; then
        . ./.ksconfig
  else
        echo "*ERROR* Configuration file not found! Run the 'configure'
script that came with this package."
        exit;
fi

# [ Version Stuff ]------------------------------------------------------=

version() {

cat << EOF

Kernel Scripts Version $VER.$SUB.$PAT. Patchk Script.
Written by: Ryan McGuire <rmcguire@usa.net>
Please send bug reports or ideas to me. Thanks!

EOF

}

# [ Help Stuff ]---------------------------------------------------------=

help() {

cat << EOF

Kernel Scripts Version $VER.$SUB.$PAT. Patchk Script.
Written by: Ryan McGuire <rmcguire@usa.net>
Please send bug reports or ideas to me. Thanks!

Just run this script to patch your Linux Source Tree to a newer version.
You must run the 'configure' script to use any of these scripts.

=------------------------------------------------------------------------=

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

EOF
}

# [ Command Line Stuff ]-------------------------------------------------=

if [ "$1" = "--version" ]; then
version;
exit;
fi

if [ "$1" = "--help" ]; then
help;
exit;
fi

# [ User ID and Stuff ]--------------------------------------------------=

if [ ! "$UID" = "0" ]; then
echo "You must logged in as \"root\" to run these scripts.... Exiting!"
echo
exit;
fi

# [ Look for the rest of the scripts ]-----------------------------------=

if [ ! -e $KSDIR/backup ]; then 
	echo "*ERROR* Backup script missing! Exiting!"
	exit
fi

if [ ! -e $KSDIR/cleanup ]; then 
	echo "*ERROR* Cleanup script missing! Exiting!"
	exit
fi

if [ ! -e $KSDIR/compile ]; then 
	echo "*ERROR* Compile script missing! Exiting!"
	exit
fi

# [ Look for kernel patch ]----------------------------------------------=

echo -n "Finding a Linux kernel patch... "
SUBLEVEL=`expr $SUBLEVEL + 1`

if [ "$SUBLEVEL" -eq "100" ]; then
	SUBLEVEL=0
	PATCHLEVEL=`expr $PATCHLEVEL + 1`
fi

if [ -e $PATCHDIR/patch-$VERSION.$PATCHLEVEL.$SUBLEVEL.gz ]; then
	echo "found."
else 
	echo "missing!"
echo "Looking for the file 'patch-$VERSION.$PATCHLEVEL.$SUBLEVEL.gz'. You could also rerun the configure 
script that came with this package and change the location of the patch."
	cd $KSDIR
	exit;
fi

# [ Move the patch ]-----------------------------------------------------=

mv $PATCHDIR/patch-$VERSION.$PATCHLEVEL.$SUBLEVEL.gz $LINUXDIR/.. 
cd $LINUXDIR/..

# [ Uncompress the patch ]-----------------------------------------------=

echo "Uncompressing and patching kernel."
gzip -d < patch-$VERSION.$PATCHLEVEL.$SUBLEVEL.gz | patch -p0  

# [ Delete '*.rej' files? ]----------------------------------------------=

if [ "$DELREJ" = "yes" ]; then
	cd $LINUXDIR
	echo "Deleting '*.rej' files."
	find . -name '*.rej' -exec rm -f {} ';'	
else
	echo "Checking to see if patch worked..."
	if [ "`find $LINUXDIR/ '(' -name '*.rej' -o -name '.*.rej' ')' -print`" ]; then
        echo 
	echo "*ERROR* Reject files found!"
	echo
	echo "Would you like to undo the patch?"
	echo
	echo -n "-=> "
	read undo_patch
	if [ "$undo_patch" = "yes" ]; then
		echo
		echo "Unappling patch..."
		echo
		cd $LINUXDIR/..
		patch -R patch-$VERSION.$PATCHLEVEL.$SUBLEVEL
		echo
		echo "Finished."
		echo
		echo "Exiting!"
		exit;
	else
		echo 
		echo "Proceeding...."
	fi
fi        

# [ Delete the patch? ]--------------------------------------------------=

if [ "$DELPATCH" = "yes" ]; then
	echo
	echo "Deleting patch."
	cd $LINUXDIR/..
	rm patch-$VERSION.$PATCHLEVEL.$SUBLEVEL.gz 
fi

# [ Delete '*.orig' and other useless files? ]---------------------------=

if [ "$DELORIG" = "yes" ]; then
	cd  $LINUXDIR
	echo
	echo "Finding '*.orig' and all other useless files and deleting them."
	find . -name '*.orig' -exec rm -f {} ';'
	find . -name '.*.orig' -exec rm -f {} ';'
	find . -name '.*.old' -exec rm -f {} ';'
fi

# [ Done ]---------------------------------------------------------------=

cd $KSDIR

echo
echo "Done."
echo
