#! /bin/bash
#
# ================================================[ Copyright Section ]===
# Backup: Linux Source Tree Backup Script.
# 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.
Backup 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. Backup Script.
Written by: Ryan McGuire <rmcguire@usa.net>
Please send bug reports or ideas to me. Thanks!

This will script will backup your Linux Kernel Source Tree. 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/cleanup ]; then 
	echo "*ERROR* Cleanup script missing! Exiting!" 
	exit
fi

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

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

# [ Lets make sure the 'BACKUPDIR' and the 'LINUXDIR' exist ]------------=

if [ ! -e $BACKUPDIR ]; then
	  
	echo	
	echo "*ERROR* - The directory '$BACKUPDIR' does not exist." 
	echo "Rerun the 'configure' script!"
	echo

if [ ! -e $LINUXDIR ]; then
        echo
        echo "*ERROR* - The directory '$LINUXDIR' does not exist."
	echo "Rerun the 'configure' script!"
	echo
	exit;
  fi
echo
exit
fi

# [ Do we need to run the 'cleanup' script ]-----------------------------=

cd $LINUXDIR

if [ -e $LINUXDIR/init/version.o ]; then 
	echo "Running cleanup script."
	. $KSDIR/cleanup
fi	

# [ Look for old Linux kernel backups ]----------------------------------=

if [ "$DELETEOLD" = "yes" ]; then
	echo
	echo "Deleting old kernel backup."
	cd $BACKUPDIR
	rm -f linux-*.tar.gz
fi

cd $LINUXDIR/..

# [ Compress the kernel using GNU Tar ]----------------------------------=


echo
echo "Making a backup of kernel version $VERSION.$PATCHLEVEL.$SUBLEVEL."
echo
echo "Using 'tar' to compress Linux."
echo
tar -cvf linux-$VERSION.$PATCHLEVEL.$SUBLEVEL.tar linux 
echo

# [ Compress the kernel using GNU Zip (gzip) ]---------------------------=

echo "Using 'gzip -9' also to compress Linux."
gzip -9 linux-$VERSION.$PATCHLEVEL.$SUBLEVEL.tar
echo

# [ To move or not to move, that is the question ]-----------------------=

if [ "$MVTOBACK" = "yes" ]; then
	echo "Moving the new Linux kernel backup to $BACKUPDIR."
	mv linux-$VERSION.$PATCHLEVEL.$SUBLEVEL.tar.gz $BACKUPDIR
fi

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

cd $KSDIR

echo
echo "Finished."     
echo
