#!/bin/bash
#
# kerncompile							2003/2004
#
# (GPL) Marno van der Molen - marno.vandermolen@gmail.com
#
# This script compiles a kernel, adds it to your bootloader and asks if you
# want to reboot. You need to be in /usr/src/linux for this to work, and you
# need to be root. You also want to edit the CONFIG OPTIONS below, to suit
# your setup
# Config options
# Filesystem may be dev or devfs: dev=/dev/hda1, devfs=/dev/discs/disc0/part1
FILESYSTEM=dev
# bootloader may be either lilo or grub
BOOTLOADER=lilo
# path to the bootloader config files
GRUB=/boot/grub/menu.lst
LILO=/etc/lilo.conf
# path to the logfile
LOG=/root/kernel_compile_log
# wether you want to view kernel compilation or not (requires X to be running)
VERBOSE=n
# Laptop config aplies to GRUB instal only. (adds a "dock" statement, only usefull if you use a docking station and addjust your init scripts )
LAPTOP=y

# The real script
if [ `echo $UID` -ne "0" ]
then
	echo "You should really run this as root."
	exit
fi

if [ `pwd` != "/usr/src/linux" ]
then
	echo "You are not in /usr/src/linux!"
	echo -n "Where are your kernel sources installed? [FULL_PATH]: "
	read $src_dir
	cd $src_dir
fi

echo -n "Have you configured your kernel src allready? [Y/n]:"
read CONF_YN

if [ $CONF_YN = "n" ]
then
	make menuconfig
else if [ $CONF_YN = "N" ]
then
	make menuconfig
fi
fi

clear
touch $LOG
echo "" > $LOG
echo "This shows the output of the kernel compile." >> $LOG
echo "Closing this window will not stop the kernel compile." >> $LOG

if [ $LAPTOP = "y" ]
then
	dock=dock
fi

if [ $VERBOSE = "y" ]
then
	/usr/X11R6/bin/xterm -title "Kernel Compile - Verbose" -e tail -f $LOG &
fi

# Starting the compile!

	echo "=============================" >> $LOG
	echo "   STARTING KERNEL COMPILE " >> $LOG
	echo "=============================" >> $LOG
	echo ""
head -n 4 Makefile 
	echo ""
	echo "Kernel name will be: Linux $1."
	echo "Using the $BOOTLOADER bootloader."
	echo ""
make bzImage >> $LOG
	echo "Compiling kernel modules."
	echo ""
	echo "=============================" >> $LOG
	echo "  COMPILING KERNEL MODULES " >> $LOG
	echo "=============================" >> $LOG
make modules >> $LOG
	echo "Installing kernel modules."
	echo ""
	echo "=============================" >> $LOG
	echo "  INSTALLING KERNEL MODULES" >> $LOG
	echo "=============================" >> $LOG
make modules_install >> $LOG
	echo "=============================" >> $LOG
	echo "         MOVING FILES" >> $LOG
	echo "=============================" >> $LOG

if [ $FILESYSTEM = "devfs" ]
then

	if [ $BOOTLOADER = "lilo" ]
	then
		cp arch/i386/boot/bzImage /boot/Linux$1 -v >> $LOG
		rm /boot/System.map -v >> $LOG
		cp System.map /boot/System.map$1 -v >> $LOG
		ln -sf /boot/System.map$1 /boot/System.map -v >> $LOG
			echo "Backing up old lilo config to $LILO.backup"
		cp $LILO $LILO.backup
		chattr -i $LILO
			echo "# ENTRY ADDED BY kernel_compile SCRIPT ON `date`" >> $LILO
			echo image=/boot/$1 >> $LILO
			echo label=TestKernel:$1 >> $LILO
			echo root=/dev/discs/disc0/part2 >> $LILO
			echo read-only >> $LILO
			echo "Writing LILO MBR."
		# lilo -v >> $LOG
			echo "=============================" >> $LOG
			echo " GRUB installation COMPLETED" >> $LOG
			echo "=============================" >> $LOG
			echo "LILO installation done."
		chattr +i $LILO; chmod 600 $LILO
	else if [ $BOOTLOADER = "grub" ]
	then
		cp arch/i386/boot/bzImage /boot/Linux$1 -v >> $LOG
		rm /boot/System.map -v >> $LOG
		cp System.map /boot/System.map$1 -v >> $LOG
		ln -sf /boot/System.map$1 /boot/System.map -v >> $LOG
			echo "Backing up old grub config to $GRUB.backup."
		cp $GRUB $GRUB.backup
			echo "# (x) Arch Linux - ENTRY ADDED BY kernel_compile SCRIPT ON `date`" >> $GRUB
			echo "title Arch Linux - testkernel" >> $GRUB
			echo "root (hd0,1)" >> $GRUB
			echo "kernel /boot/Linux$1 root=/dev/discs/disc0/part2 ro $dock vga=0x316 acpi=force" >> $GRUB
			echo "=============================" >> $LOG
			echo " GRUB installation COMPLETED" >> $LOG
			echo "=============================" >> $LOG
			echo "Grub installation done."
	else
			echo "Unknown bootloader. Try updating your bootloader config manually."
			echo "=============================" >> $LOG
			echo "   BOOT LOADER NOT UPDATED  " >> $LOG
			echo "=============================" >> $LOG
			echo "Reason: unknown bootloader" >> $LOG
	fi
	fi

fi

if [ $FILESYSTEM = "dev" ]
then

	if [ $BOOTLOADER = "lilo" ]
	then
		cp arch/i386/boot/bzImage /boot/Linux$1 -v >> $LOG
		rm /boot/System.map -v >> $LOG
		cp System.map /boot/System.map$1 -v >> $LOG
		ln -sf /boot/System.map$1 /boot/System.map -v >> $LOG
			echo "Backing up old lilo config to $LILO.backup."
		cp $LILO $LILO.backup
		chattr -i $LILO
			echo "# ENTRY ADDED BY kernel_compile SCRIPT ON `date`">> $LILO
			echo image=/boot/$1 >> $LILO
			echo label=TestKernel:$1 >> $LILO
			echo root=/dev/hda1 >> $LILO
			echo read-only >> $LILO
			echo "Writing LILO MBR."
		# lilo -v >> $LOG
			echo "=============================" >> $LOG
			echo " LILO installation COMPLETED" >> $LOG
			echo "=============================" >> $LOG
			echo "LILO installation done"
		chattr +i $LILO; chmod 600 $LILO
	else if [ $BOOTLOADER = "grub" ]
	then
		cp arch/i386/boot/bzImage /boot/Linux$1 -v >> $LOG
		rm /boot/System.map -v >> $LOG
		cp System.map /boot/System.map$1 -v >> $LOG
		ln -sf /boot/System.map$1 /boot/System.map -v >> $LOG
			echo "Backing up old grub config to $GRUB.backup ."
		cp $GRUB $GRUB.backup
			echo "# (x) Arch Linux - ENTRY ADDED BY kernel_compile SCRIPT ON `date`" >> $GRUB
			echo "title Arch Linux - testkernel" >> $GRUB
			echo "root (hd0,1)" >> $GRUB
			echo "kernel /boot/Linux$1 root=/dev/hda1 ro $dock vga=0x316 acpi=force" >> $GRUB
			echo "=============================" >> $LOG
			echo " GRUB installation COMPLETED" >> $LOG
			echo "=============================" >> $LOG
			echo "Grub installation done."
	else
			echo "Unknown bootloader. Try updating your bootloader config manually."
			echo "No files to move, unknown bootloader!"
			echo "" >> $LOG
			echo "=============================" >> $LOG
			echo "   BOOT LOADER NOT UPDATED  " >> $LOG
			echo "=============================" >> $LOG
			echo "Reason: unknown bootloader" >> $LOG
	fi
	fi
fi
	echo "" >> $LOG	
	echo "Kernel installation succesfull." >> $LOG
	echo ""
	echo -n "Kernel installation succesfull. Reboot now: "
read answer

if [ $answer == "Y" ]
then
	echo "Rebooting, and "
	init 6
else if [ $answer == "y" ]
then
	echo "Rebooting, and "
	init 6
else
	echo -n "Not rebooting, "
fi
fi

	echo "killing Kernel Compile - Verbose terminal."
kill `ps -ef | grep Kernel\ Compile\ | awk '{ print $2 }' | head -n 1 `
