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

Just run this script to compile a Linux kernel. 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 ]-----------------------------------=

cd $KSDIR

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

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

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

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

cd $LINUXDIR

if [ -e $LINUXDIR/init/main.o ] ; then
	echo
	echo "You have started to compile a kernel, or forgot to clean. Do
you want to run the 'cleanup' script, or continue compiling? "
	echo
	echo "cleanup = Run the cleanup script."
	echo "compile = Continue compiling."
	echo
	echo -n "-=> "
	read continue_or_clean
	if [ "$continue_or_clean" = "cleanup" ]; then
		echo
		echo "Running the 'cleanup' script."
		. $KSDIR/cleanup
fi
if [ "$continue_or_clean" = "compile" ]; then	
		echo
		echo "Making kernel image using $COMPILEOPT."
		echo 
		echo "Note: After the kernel is compiled, this script will quit. It will not 
make modules or anything else you wanted, because you started in the
middle of a compile." 
		echo
		sleep 2s
		cd $LINUXDIR
		make $COMPILEOPT
		exit
fi
fi
# [ determine if we need to run 'make oldconfig' or 'make config' ]------=

if [ -e $LINUXDIR/.config ] ; then 
	echo
	echo "Configuring this kernel with old config file."
	echo
	cd $LINUXDIR
	make oldconfig
else
	echo "You will need to enter information about your kernel yourself."
	cd $LINUXDIR
	make config
fi

# [ Run make dep? ]------------------------------------------------------=

if [ "$RUNDEP" = "yes" ] ; then

	echo "Running 'make dep'."
	echo
	cd $LINUXDIR 
	make dep
	if [ "$COMPILEOPT" = "zdisk" ]; then
		echo
		echo "Remember to place a disk in the drive."
		/bin/sleep 4s
	fi
	echo
	echo "Making a Kernel Image using 'make $COMPILEOPT'." 
	echo
	cd $LINUXDIR
	make $COMPILEOPT
	echo
	echo "Done Compiling"
else
	if [ "$COMPILEOPT" = "zdisk" ]; then
                echo
                echo "Remember to place a disk in the drive."
                /bin/sleep 4s
        fi
	echo
	echo "Making a Kernel Image using 'make $COMPILEOPT'."
	echo
	cd $LINUXDIR
	make $COMPILEOPT
	echo
	echo "Done Compiling"
fi

# [ Make modules? ]------------------------------------------------------=

if [ "$MKMOD" = "yes" ]; then 
	echo
	echo "Making modules."
	echo
	make modules

# [ Install Modules? ]---------------------------------------------------=

	if [ "$MKMODINS" = "yes" ]; then 
		echo
		echo "Installing modules."
		echo
		make modules_install
	fi

# [ Don't make modules. ]------------------------------------------------=

else
	echo
	echo "Not making modules."
	echo
fi

# [ If use use Loadlin, move to the DOS/Win95 directory. ]---------------=

	if [ "$MOVELOADLINKERNEL" = "yes" ]; then
		if [ "$LOADLINMOVEOLD" = "yes" ]; then
			if [ -e $LOADLINDIR/zimage ]; then
				rm -f $LOADLINDIR/zimage.old
				mv $LOADLINDIR/zimage $LOADLINDIR/zimage.old
			else
				rm -f $LOADLINDIR/bzimage.old
				mv $LOADLINDIR/bzimage $LOADLINDIR/bzimage.old
			fi
		fi
		
				if [ -e $LINUXDIR/arch/i386/boot/zImage ]; then
					echo "Moving kernel image to DOS/Win95 partition."
						mv $LINUXDIR/arch/i386/boot/zImage $LOADLINDIR/zimage
				else
					echo "Moving kernel image to DOS/Win95 partition."
						mv $LINUXDIR/arch/i386/boot/bzImage $LOADLINDIR/bzimage
				fi
	fi

# [ Delete System.map? ]-------------------------------------------------=

if [ "$DELSYSMAP" = "yes" ]; then
	echo -n "Deleting unwanted files... "
	rm -f /System.map 
	if [ "$DELVMOLD" = "yes" ]; then
		rm -f /vmlinuz.old
		echo "done."
	else
		echo "done."
	fi
fi

# [ Delete the file /System.old? ]---------------------------------------=

if [ "$SYSTEMOLD" = "yes" ]; then
	rm -f /System.old
fi

# [ Do you want to run the 'backup' script after compile is finished ]---=

if [ "$RUNBACKUP" = "yes" ]; then
	echo "Running the 'backup' script."
	$KSDIR/backup
fi

# [ Reboot the computer? ]-----------------------------------------------=

cd $KSDIR

if [ "$REBOOT" = "yes" ]; then 
	echo
	echo "Rebooting computer with new kernel image."

# [ See if the files required to reboot the computer are there ]---------=	

	if [ -x /sbin/reboot ]; then
		/sbin/reboot
	else
		/sbin/shutdown -r now
	fi
else

# [ Exit. Don't Reboot. ]------------------------------------------------=

	echo
	echo "Not rebooting computer." 
	echo 
	echo "Finished."
	echo
fi
