#! /bin/bash
#
# ========================================================================
# Configure: Script to configure Kernel Scripts.
# 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.
#
# ========================================================================

VER=2
SUB=1
PAT=40


if [ "$1" = "--version" ]; then
echo "Kernel Scripts Configure Script Version $VER.$SUB.$PAT." 
echo "By: Ryan McGuire <rmcguire@freenet.columbus.oh.us>"
echo
exit
fi

if [ "$1" = "--help" ]; then
echo "Kernel Scripts Configure Script Version $VER.$SUB.$PAT."
echo "By: Ryan McGuire <rmcguire@freenet.columbus.oh.us>"
echo
echo "This script needs to run to configure Kernel Scripts."
echo
exit
fi

if [ "$1" = "--debug" ]; then
	echo
	echo "Debug mode on!"
	echo
	cp /usr/src/linux/compile .
	cp /usr/src/linux/backup .
	cp /usr/src/linux/cleanup .
	cp /usr/src/linux/patchk .
	echo "Files copied!"
	echo
fi


echo -n "Deleting old configuration file..."
rm -f ./.ksconfig
echo " Done."

echo -n "Creating configuration file header..."
echo "#! /bin/bash" >> .ksconfig 
echo "#" >> .ksconfig
echo "# ========================================================================">>.ksconfig 
echo "# ksconfig: Configuration file for kscripts.">> .ksconfig
echo "# Copyright (C) 1996-97 Ryan McGuire.">> .ksconfig
echo "# Version $VER.$SUB.$PAT.">> .ksconfig
echo "# ========================================================================">>.ksconfig 
echo "#">> .ksconfig
echo "#    This program is free software; you can redistribute it and/or modify">> .ksconfig
echo "#    it under the terms of the GNU General Public License as published by" >> .ksconfig
echo "#    the Free Software Foundation; either version 2 of the License, or" >> .ksconfig
echo "#    (at your option) any later version.">> .ksconfig
echo "#">> .ksconfig
echo "#    This program is distributed in the hope that it will be useful,">> .ksconfig
echo "#    but WITHOUT ANY WARRANTY; without even the implied warranty of">>.ksconfig
echo "#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the">>.ksconfig
echo "#    GNU General Public License for more details.">> .ksconfig
echo "#">> .ksconfig
echo "#    You should have received a copy of the GNU General Public License" >> .ksconfig
echo "#    along with this program; if not, write to the Free Software">> .ksconfig
echo "#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA." >> .ksconfig
echo "#">> .ksconfig
echo "# ========================================================================">>.ksconfig
echo "" >> .ksconfig
echo "VER=$VER">>./.ksconfig
echo "SUB=$SUB">>./.ksconfig
echo "PAT=$PAT">>./.ksconfig
echo " Done."
echo
echo "=============================================================================="
echo "Getting variables for the 'backup' script. This script will backup the kernel."
echo "=============================================================================="
echo
if [ -d /usr/src/linux ]; then
	echo "Found Linux Source Tree in /usr/src/linux. Do you want to
use this as the location of your Linux Source Tree?"
	echo
	echo -n "-=> "
	read found_linux_usr_src
	if [ $found_linux_usr_src = no ]; then
	echo
	echo "Please enter the location of your Linux Source Tree."
echo
	echo -n "-=> "
	read linux_directory
	echo LINUXDIR=$linux_directory >> ./.ksconfig
	else
	echo LINUXDIR=/usr/src/linux>>./.ksconfig
	fi
else
echo "Please enter the location of your Linux Source Tree."
echo
echo -n "-=> "
read linux_directory
echo LINUXDIR=$linux_directory >> ./.ksconfig
fi

echo "eval \`sed -n 's/^\([A-Z]*\) = \([0-9]*\)$/\1=\2/p' \$LINUXDIR/Makefile\`">>./.ksconfig
echo "if [ -z \"\$VERSION\" -o -z \"\$PATCHLEVEL\" -o -z \"\$SUBLEVEL\" ]; then">>./.ksconfig
echo "    echo \"*ERROR* Could not get kernel version. Run 'configure'.\" ">>./.ksconfig
echo "    exit 1;">>./.ksconfig
echo " fi">>./.ksconfig

echo
echo "Do you want to move the backup kernel source to a different directory?"
echo
echo -n "-=> "
read move_to_back
echo MVTOBACK=$move_to_back >>./.ksconfig

if [ $move_to_back = yes ]; then
echo
echo "What directory would you like to place the kernel backup?" 
echo
echo -n "-=> "
read move_new_kernel_backup_dir
echo BACKUPDIR=$move_new_kernel_backup_dir>>./.ksconfig

if [ ! -d $move_new_kernel_backup_dir ]; then 
	echo 
	echo "*ERROR* Directory does not exist."
	echo "Exiting!"
	exit;
fi

else

echo BACKUPDIR=~>>./.ksconfig

fi

echo
echo "Do you want to delete the old kernel backups?"
echo
echo -n "-=> "
read delete_old_kernel_backups
echo DELETEOLD=$delete_old_kernel_backups>>./.ksconfig

echo
echo "=========================================================================="
echo "Getting variables for the 'cleanup' script. This script will clean-out the 
Linux Source Tree."
echo "=========================================================================="
echo

echo "What type of clean do you want to use? Could be just 'clean' or 'mrproper'." 
echo
echo -n "-=> "
read clean_type
echo CLNTYPE=$clean_type>>./.ksconfig

if [ $clean_type = mrproper ]; then
echo
echo "When cleaning, do you want to move your Linux config file to a different" 
echo "location? It will be moved back after the cleaning has been finished."
echo
echo -n "-=> "
read move_config_file
echo MVCONFIG=$move_config_file>>./.ksconfig

echo
echo "When cleaning, do you want to move your 'System.map' file to a different"
echo "location? It will be moved back after the cleaning has been finished." 
echo
echo -n "-=> "
read move_system_map
echo MVSYSMAP=$move_system_map>>./.ksconfig
else
echo MVSYSMAP=no>>./.ksconfig
echo MVCONFIG=no>>./.ksconfig

fi

if [ -d /tmp ]; then
	echo
	echo "Temp directory found in /tmp. Use this for temp directory?"
	echo
	echo -n "-=> "
	read found_tmp_in_root_slash_tmp      # Cool name, don't you think?
	if [ $found_tmp_in_root_slash_tmp = no ]; then
	echo
	echo "Where is you temp directory?"
	echo
	echo -n "-=> "
	read temp_directory
	echo TMPDIR=$temp_directory>>./.ksconfig
	else
	echo TMPDIR=/tmp>>./.ksconfig
	fi
else
echo "Where is you temp directory?"
echo
echo -n "-=> "
read temp_directory
echo TMPDIR=$temp_directory>>./.ksconfig
fi
echo
echo "============================================================================="
echo "Getting variable for the 'compile' script. This script will compile the 
Linux kernel."
echo "============================================================================="

echo
echo "When you compile the kernel, what option do you want? You could choose" 
echo "zlilo, zImage, bzImage, or bzlilo." 
echo
echo -n "-=> "
read compile_the_kernel
echo COMPILEOPT=$compile_the_kernel>>./.ksconfig

if [ "$compile_the_kernel" = "zimage" ]; then
	echo COMPILEOPT=zImage>>./.ksconfig
fi

if [ "$compile_the_kernel" = "bzimage" ]; then
        echo COMPILEOPT=bzImage>>./.ksconfig
fi


if [ "$compile_the_kernel" = "zlilo" ]; then
	echo
echo "When you compile the kernel, the System.map is copied to the root directory. 
Do you want to delete that?"
	echo
	echo -n "-=> "
	read delete_system_map
	echo DELSYSMAP=$delete_system_map>>./.ksconfig

echo
echo "If there is an old System.map in the root directory, it is renamed
to 'System.old'. Do you want to delete that?"

	echo
	echo -n "-=> "
	read delete_system_old
	echo SYSTEMOLD=$delete_system_old>>./.ksconfig

	echo
	echo " After the kernel is compiled, the old 'vmlinuz' file to
'vmlinuz.old'. Do you want to delete the 'vmlinuz.old'?"
	echo
	echo -n "-=> "
	read delete_vmlinuz_old
	echo DELVMOLD=$delete_vmlinuz_old>>./.ksconfig

else
echo DELSYSMAP=no>>./.ksconfig
echo DELVMOLD=no>>./.ksconfig
echo SYSTEMOLD=no>>./.ksconfig
fi


if [ "$compile_the_kernel" = "bzlilo" ]; then
	echo
	echo "When you compile the kernel, the System.map is copied to the root 
directory. Do you want to delete that?"
	echo
	echo -n "-=> "
	read delete_system_map
	echo DELSYSMAP=$delete_system_map>>./.ksconfig

	echo "If there is an old System.map in the root directory, it is
 renamed to 'System.old'. Do you want to delete that?"

	echo
	echo -n "-=> "
	read delete_system_old
	echo SYSTEMOLD=$delete_system_old>>./.ksconfig

	echo
	echo " After the kernel is compiled, the old 'vmlinuz' file to 'vmlinuz.old'. Do you want 
to delete the 'vmlinuz.old'?"
	echo
	echo -n "-=> "
	read delete_vmlinuz_old
	echo DELVMOLD=$delete_vmlinuz_old>>./.ksconfig

	fi

if [ "$compile_the_kernel" = "zImage" ]; then

	echo
	echo "Do you use Loadlin to boot Linux?"
	echo
	echo -n "-=> "
	read use_loadlin_yes_no

	if [ "$use_loadlin_yes_no" = "yes" ]; then
		echo
echo "After the kernel is compiled, would you like to move the new kernel to the 
DOS/Win95 directory where you keep the kernel?"
		echo
		echo -n "-=> "
		read loadlin_move_to_dos_win95_directory
		echo USELOADLIN=$use_loadlin_yes_no>>./.ksconfig
		echo MOVELOADLINKERNEL=$loadlin_move_to_dos_win95_directory>>./.ksconfig
		if [ "$loadlin_move_to_dos_win95_directory" = "yes" ]; then
			echo
echo "What directory on your DOS/Win95 partition would you like to move the kernel to?"
			echo
			echo -n "-=> "
			read loadlin_dir
			echo LOADLINDIR=$loadlin_dir>>./.ksconfig
		fi
	fi
fi
	
if [ "$compile_the_kernel" = "bzImage" ]; then
	echo
	echo "Do you use Loadlin to boot Linux?"
	echo
	echo -n "-=> "
	read use_loadlin_yes_no
        if [ "$use_loadlin_yes_no" = "yes" ]; then
        	echo
		echo "After the kernel is compiled, would you like to move the new kernel to the 
DOS/Win95 directory where you keep the kernel?"
                echo
                echo -n "-=> "
                read loadlin_move_to_dos_win95_directory
        	if [ "$loadlin_move_to_dos_win95_directory" = "yes" ]; then
                	echo
			echo "What directory on your DOS/Win95 partition would you like to move the kernel to?"
                	echo
                	echo -n "-=> "
                	read loadlin_dir
                	echo MOVELOADLINKERNEL=$loadlin_move_to_dos_win95_directory>>./.ksconfig
	        	echo LOADLINDIR=$loadlin_dir>>./.ksconfig
        	fi
	fi
fi

if [ "$compile_the_kernel" = "zimage" ]; then

        echo
        echo "Do you use Loadlin to boot Linux?"
        echo
        echo -n "-=> "
        read use_loadlin_yes_no

        if [ "$use_loadlin_yes_no" = "yes" ]; then
                echo
echo "After the kernel is compiled, would you like to move the new kernel to the
DOS/Win95 directory where you keep the kernel?"
                echo
                echo -n "-=> "
                read loadlin_move_to_dos_win95_directory
                echo USELOADLIN=$use_loadlin_yes_no>>./.ksconfig
                echo MOVELOADLINKERNEL=$loadlin_move_to_dos_win95_directory>>./.ksconfig
		if [ "$loadlin_move_to_dos_win95_directory" = "yes" ]; then
                        echo
echo "What directory on your DOS/Win95 partition would you like to move the kernel."                        
			echo
                        echo -n "-=> "
                        read loadlin_dir
                        echo LOADLINDIR=$loadlin_dir>>./.ksconfig
                fi
        fi
fi

if [ "$compile_the_kernel" = "bzimage" ]; then

        echo
        echo "Do you use Loadlin to boot Linux?"
        echo
        echo -n "-=> "
        read use_loadlin_yes_no

        if [ "$use_loadlin_yes_no" = "yes" ]; then
                echo
echo "After the kernel is compiled, would you like to move the new kernel to the 
DOS/Win95 directory where you keep the kernel?"
                echo
                echo -n "-=> "
                read loadlin_move_to_dos_win95_directory
                echo USELOADLIN=$use_loadlin_yes_no>>./.ksconfig
        	echo  MOVELOADLINKERNEL=$loadlin_move_to_dos_win95_directory>>./.ksconfig                
if [ "$loadlin_move_to_dos_win95_directory" = "yes" ]; then
                        echo
                        echo
echo "What directory on your DOS/Win95 partition would you like to move the kernel."
echo -n "-=> "
                        read loadlin_dir
                        echo LOADLINDIR=$loadlin_dir>>./.ksconfig
                fi
        fi
fi

if [ "$use_loadlin_yes_no" = "yes" ]; then
	echo
	echo "Once the kernel is compiled, would you like to keep the old
kernel? It will be renamed 'zImage.old'"
echo
echo -n "-=> "
read move_loadlin_vmlinuz_old
if [ "$move_loadlin_vmlinuz_old" = "yes" ]; then
	echo LOADLINMOVEOLD=yes>>./.ksconfig
fi
fi

echo 
echo "Do you want to run 'make dep' ?"
echo
echo -n "-=> "
read run_dep
echo RUNDEP=$run_dep>>./.ksconfig

echo
echo "Do you want to run 'make modules' ?"
echo
echo -n "-=> "
read make_modules
echo MKMOD=$make_modules>>./.ksconfig

if [ $make_modules = yes ]; then
	echo
	echo "Do you want to run 'make modules_install' ?"
	echo
	echo -n "-=> "
	read make_modules_install
	echo MKMODINS=$make_modules_install>>./.ksconfig
else
	echo MKMODINS=no>>./.ksconfig
fi

echo
echo "After kernel compilation is finished, do you want to reboot the computer?"
echo
echo -n "-=> "
read reboot_computer
echo REBOOT=$reboot_computer>>./.ksconfig


echo 
echo "After the kernel is compiled, run the 'cleanup' script? This option is
not really useful. Just say 'no'."
echo
echo -n "-=> "
read run_cleanup
echo RUNCLEANUP=$run_cleanup>>./.ksconfig

echo
echo "After the kernel is compiled, run the 'backup' script? This option is
not really useful. Just say 'no'."
echo
echo -n "-=> "
read run_backup
echo RUNBACKUP=$run_backup>>./.ksconfig

echo
echo "==============================================================================="
echo "Now getting variables for the 'patchk' script. The 'patchk' will patch the 
Linux kernel to a newer release. Providing of course you have the correct files."
echo "==============================================================================="
echo
echo "Where is the location of the new patch?"
echo
echo -n "-=> "
read new_patch_directory
if [ ! -d $new_patch_directory ]; then
	echo
	echo "Okay buddy, all I want is a directory that works. Check your
sources!"
	exit;
else 
	echo PATCHDIR=$new_patch_directory>>./.ksconfig
fi
 
echo
echo "After kernel is patched, delete the old patch?"
echo
echo -n "-=> "
read delete_old_patch
echo DELPATCH=$delete_old_patch>>./.ksconfig

echo
echo "Delete '*.orig'. These files are a result of the patching the kernel.
They are not needed, unless the patch did not work."
echo
echo -n "-=> "
read delete_orig
echo DELORIG=$delete_orig>>./.ksconfig

echo
echo "Delete '*.rej'. These files are a result of the patching the kernel and some 
parts, or all of it, not working. If you say 'no', then this it will display 
the '*.rej' files, but not delete."
echo
echo -n "-=> "
read delete_rej_files
echo DELREJ=$delete_rej_files>>./.ksconfig

echo
echo "Kernel Scripts is now configured."
. ./.ksconfig
echo
echo "Would you like to install them to $LINUXDIR?"
echo
echo -n "-=> "
read install_yes_no
if [ $install_yes_no = yes ]; then
	echo KSDIR=$LINUXDIR>>./.ksconfig
	. ./.ksconfig
	cp ./backup ./compile ./cleanup ./patchk ./.ksconfig $KSDIR
 	echo
	echo "Finished."
	echo
	exit
else
	echo
	echo "What directory would you like to install kscripts?"
	echo
	echo -n "-=> "
	read kscripts_install_directory_1
	if [ ! -d $kscripts_install_directory_1 ]; then
		echo
		echo "Directory does not exist. Would you like to create it?"
		echo
		echo -n "-=> "
		read dir_create_yes_no
		if [ "$dir_create_yes_no" = "no" ]; then
			echo			
			echo "Installation not complete! Kscripts will not work unless you input a install directory."
			exit;
		else
			mkdir $kscripts_install_directory_1
		fi
		echo KSDIR=$kscripts_install_directory_1>>./.ksconfig
		. ./.ksconfig
		cp ./compile ./cleanup ./backup ./patchk ./.ksconfig $KSDIR
	fi	


	echo
	echo "Finished."
	exit
fi
