#!/bin/bash

cd /usr/lib/newkernel

ARCH=`uname -m`

echo
echo 'Welcome!'
echo
echo "This script will install the 2.2 kernel on your Fermi"
echo "Red Hat Linux 5.2.1 machine."
echo
echo "This script will also replace several other packages"
echo "that need to be upgraded for kernel 2.2.  The script"
echo "will tell you what packages are being upgraded as it"
echo "is running."
echo
SMP=""
if [ "$ARCH" = "i586" -o "$ARCH" = "i686" ] ; then
   echo "An SMP kernel is available."
   echo
   echo "You'll only want to install this kernel if you have"
   echo "more than one processor in your machine."
   echo
   echo "NOTE: Installing a SMP kernel will make PCMCIA devices"
   echo "no longer work.  However, you probably only care about"
   echo "this if you have a laptop, in which case you most likely"
   echo "only have one processor."
   echo
   echo "Do you want to use an SMP kernel (y/n)?"
   read
   if [ "$REPLY" = "y" -o "$REPLY" = "Y" ] ; then
      SMP="smp-"
   fi
fi

# Create the file that AutoRPM reads
echo $SMP > smp

echo
echo "Using AutoRPM to get updated packages..."
autorpm --config autorpm.conf

# Replace ipfwadm with ipchains package...
if [ "$(rpm -qa | grep -c ipfwadm)" != "0" ] ; then
   rpm -e ipfwadm
   rpm -ivh ipchains-*
fi
rm -f ipchains-*

# Replace nfs-server-clients with knfsd-clients package...
if [ "$(rpm -qa | grep -c nfs-server-clients)" != "0" ] ; then
   rpm -e nfs-server-clients
   rpm -ivh knfsd-clients*
fi
rm -f knfsd-clients*

# Replace nfs-server with knfsd package...
if [ "$(rpm -qa | grep -c nfs-server)" != "0" ] ; then
   rpm -e nfs-server
   # Use --force because of conflict with rpc.rquotad from 'quota' package
   rpm -ivh --force knfsd*
fi
rm -f knfsd*




# First, determine new kernel location
KERNEL=`rpm -qlp kernel-${SMP}2.* | grep vmlinuz`
VERSION=`echo $KERNEL | sed 's/\/boot\/vmlinuz-//'`

echo "Installing new kernel..."
rpm -ivh kernel-${SMP}2.* kernel-headers-*
rm -f kernel-${SMP}2.* kernel-headers-*

echo "Making initial ramdisk..."
cd /boot
/sbin/mkinitrd initrd-$VERSION.img $VERSION

echo "Modifying lilo.conf..."
cp /etc/lilo.conf /etc/lilo.conf-bak
echo "   old one backed up as /etc/lilo.conf-bak"
/usr/lib/newkernel/dolilo.pl $KERNEL /boot/initrd-$VERSION.img

echo "Running lilo... (watch for errors)"
/sbin/lilo

echo
echo "The LILO name for the new kernel is: linux22"
echo 
echo "Unless you saw any errors above (which you shouldn't have)"
echo "you just need to reboot your machine to begin running the"
echo "new Linux 2.2 kernel."
echo
