#!/bin/bash
##########start functions##########
unamekern=`uname -r`
checkforroot ()
{
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ]
then
	echo ""
	echo "**************** ERROR !! **************"
	echo "You must be logged in as root to run this script"
	echo "Please log in as root and re-run this script."
	echo "**************** ERROR !! **************"
	exit
fi
} 
function asksrc ()
 {
 read -p "what directory would you like to use as your kernel source directory (no trailing slash): " kerndir &&
 iskernsrc
 }

function ask1 ()
{
 if [[ ${#answer} = 0 ]]
 then
 	ask2
 fi
 while [ $answer != "No" ] && [ $answer != "no" ] && [ $answer != "Yes" ] && [ $answer != "yes" ] && [ $answer != "y" ] && [ $answer != "n" ] && [ $answer != "Y" ] && [ $answer != "N" ]
 do
	 ask2
 done
 if [ $answer != "No" ] && [ $answer != "no" ] && [ $answer != "n" ] && [ $answer != "N" ]
 then
 	echo "Using /usr/src/linux as your kernel source directory" && 
 	kerndir="/usr/src/linux" &&
	confexist
 elif [ $answer != "Yes" ] && [ $answer != "yes" ] && [ $answer != "Y" ]&& [ $answer != "y" ]
 then
 	read -p "what directory would you like to use as your kernel source directory (no trailing slash): " kerndir &&
 	iskernsrc
 fi
}

function ask2 ()
{
 echo "That is not a valid answer"
 echo ""
 ask3
}

function ask3 ()
 {
 read -p "/usr/src/linux exists, would you like to use this as your kernel directory (yes/no): " answer
 if [[ ${#answer} = 0 ]]
 then
 	ask2
 fi
 if [[ $answer != "Y" ]] && [[ $answer != "y" ]] && [[ $answer != "yes" ]] && [[ $answer != "Yes" ]]
 then
 	read -p "what directory would you like to use as your kernel source directory (no trailing slash): " kerndir &&
 	iskernsrc
 elif [[ $answer != "N" ]] && [[ $answer != "n" ]] && [[ $answer != "no" ]] && [[ $answer != "No" ]]
 then
 	echo "Using /usr/src/linux as your kernel source directory" && 
 	kerndir="/usr/src/linux" &&
	confexist
 fi
 }

function iskernsrc ()
 {
 if [[ -d $kerndir/arch ]]
 then
	echo "$kerndir is a kernel source directory" && confexist
 else
	echo "$kerndir is not a kernel source directory, or you entered a trailing slash"  &&
	asksrc
 fi
 }

function kerndir ()
 {
 checkforroot
 if [[ -d /usr/src/linux ]]
 then
	read -p "/usr/src/linux exists, would you like to use this as your kernel directory (yes/no): " answer && ask1
 else
	asksrc
 fi
}
function confexist ()
 {
 cd $kerndir
 if [[ -e .config ]]
 then
	echo ".config found, running make clean" &&
	make clean
	echo "Now running make oldconfig" &&
	sleep 3 &&
	make oldconfig
	echo "Now running make"
	make && 
	touch .makecompleted && 
	instmods
 else
	bootconf
 fi
 }
function instmods ()
 {
 if [[ -e .makecompleted ]]
 then
	echo "Make completed succesfully, running make modules_install" &&
	make modules_install &&
	echo "Make modules_install finished. Now go into arch/<your arch - eg i386>/boot"
	echo "and copy bzImage to /boot/kernel-$unamekern"
 else
	echo "Make failed for some reason, quitting" &&
	exit 0
 fi
 }
function usrlinuxdir ()
 {
 if [[ -d /usr/src/linux/arch ]]
 then 
	kerndir
 else
	echo "/usr/src/linux doesn't exist or is not a kernel source directory"
	asksrc
 fi
 }
function bootconf ()
 { 
 if [[ -e /boot/config-$unamekern ]]
 then
	echo "Config file found in /boot, it will now be used to compile the kernel" &&
	cp /boot/config-$unamekern $kerndir/.config
	confexist
 else
	echo "No kernel config found anywhere, running make config" &&
	make config
	confexist
 fi
 }
##########End functions##########
##########Start Script Here##########	
echo "Warning: This script is for 2.6 kernels ONLY, 2.4 kernels will not compile under this script!"
sleep 4
usrlinuxdir
exit 0
