#!/bin/sh
# Build and install linux kernel - Mongoose <stu7440@westga.edu>

# Configs
KERNEL_DIR="/usr/src/linux"

if [ ! $1 ]
then
    echo kernel.sh KERNEL_VERSION
    echo Example: kernel.sh 1.2.13-ac3
    exit 0
fi

echo -n "Would you like to build kernel?  [y/N] "
read BUILD_KERNEL

echo -n "Would you like to build and install kernel modules?  [y/N] "
read BUILD_KERNEL_MODULES

echo -n "Would you like to build with threads?  [y/N] "
read BUILD_WITH_J3

cd $KERNEL_DIR

case "$BUILD_WITH_J3" in
   y)
      MAKE="make -j3"
      ;;
   *)
      MAKE="make"
      ;;
esac

case "$BUILD_KERNEL" in
   y)
      $MAKE dep && $MAKE clean && $MAKE bzImage
      ;;
   *)
      ;;
esac

case "$BUILD_KERNEL_MODULES" in
   y)
      $MAKE modules && $MAKE modules_install      
      ;;
   *)
      ;;
esac


echo Copying new kernel to /boot...
cp -f /usr/src/linux/System.map /boot/System.map-${1}
cp -f /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz-${1}

echo Removing old kernel links...
rm -f /boot/vmlinuz
rm -f /boot/System.map

echo Linking to new kernel...
ln -s /boot/vmlinuz-${1} /boot/vmlinuz
ln -s /boot/System.map-${1} /boot/System.map

echo -n "Would you like to edit /etc/lilo.conf?  [y/N]"
read LILO_SETUP

case "$LILO_SETUP" in
   y)
      $EDITOR /etc/lilo.conf
      ;;   
   *)
      ;;
esac

echo Running lilo...
/sbin/lilo
