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

Just run this script to cleanout your Linux 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/patchk ]; then 
	echo "*ERROR* Patchk script missing! Exiting!"
	exit
fi

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

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

# [ Get clean type ]-----------------------------------------------------=

if [ "$CLNTYPE" = "mrproper" ] ; then 
	echo
	echo "Cleaning (mrproper)..."
	cd $LINUXDIR
		if [ "$MVCONFIG" = "yes" ]; then
		echo -n "Moving configuration file (.config) for later use..."
		if [ -e $LINUXDIR/.config ]; then
			mv .config $TMPDIR/.config.$VER.$SUB.$PAT
			echo " done."
			else
			echo " missing!"
			echo
			echo "*ERROR* - Could not find config file! Exiting!"
			exit;
			fi
		fi
				
else
	echo "Cleaning (regular)..."
fi

# [ Move system.map? ]---------------------------------------------------=

if [ "$MVSYSMAP" = "yes" ]; then
	echo -n "Moving 'System.map' for later use..."
		if [ -e $LINUXDIR/System.map ]; then 
		mv $LINUXDIR/System.map $TMPDIR/.System.map.$VER.$SUB.$PAT
		echo " done."
		else 
		echo " missing!." 
		echo
		echo "*ERROR* - Rerun the 'configure' script. Exiting!"
		if [ -e $TMPDIR/.config.$VER.$SUB.$PAT ]; then
			mv $TMPDIR/.config.$VER.$SUB.$PAT $LINUXDIR/.config
		else
			exit;
		fi
		echo
		echo "Moving back config file." 
		exit;	
	fi
fi

# [ Scrub really hard ]--------------------------------------------------=	

cd $LINUXDIR
if [ "$CLNTYPE" = "mrproper" ]; then
	make mrproper > $LINUXDIR/.PiXELiTE_Software_rulez
	mkdir $LINUXDIR/modules
else
	make clean > $LINUXDIR/.PiXELiTE_Software_rulez
	mkdir $LINUXDIR/modules
fi

rm -f $LINUXDIR/.PiXELiTE_Software_rulez

# [ Move back System.map, if defined ]-----------------------------------=

if [ "$MVSYSMAP" = "yes" ]; then
	
	if [ -e $TMPDIR/.System.map.$VER.$SUB.$PAT ]; then
		echo -n "Moving back System.map..."
		mv $TMPDIR/.System.map.$VER.$SUB.$PAT $LINUXDIR/System.map
		echo " done."
	else
		echo " missing!"
		echo
		echo "*ERROR* - Could not find config file...."
		echo
		echo "Exiting!"
		exit
	fi
fi

# [ Move back config file, if defined ]----------------------------------=

if [ "$MVCONFIG" = "yes" ]; then
	if [ -e $TMPDIR/.config.$VER.$SUB.$PAT ]; then
		echo -n "Moving back configuration file (.config)..."
		mv $TMPDIR/.config.$VER.$SUB.$PAT $LINUXDIR/.config
		echo " done."
	else
		echo " missing!"
		echo
		echo "*ERROR* - Could not find config file!"
		echo "Exiting!"
		exit
	fi
fi

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

cd $KSDIR

echo
echo "Finished."		
echo
