#!/bin/sh
# sudo sh rt4us-natty

# This script is for Ubuntu Studio 11.04 64-bit
# rt4us version 0.3.20112704-mailing_list-20110506 Ralf Mardorf

apt-get update
apt-get install fakeroot build-essential crash kexec-tools makedumpfile kernel-package kernel-wedge
apt-get build-dep linux
apt-get install libncurses5 libncurses5-dev libelf-dev asciidoc binutils-dev

## Variables to edit
 # The booted kernel and architecture that by default should fit to this script
 # GENERIC=2.6.38-8-generic
 GENERIC=2.6.38-8-lowlatency
 ARCH=x86_64

 # Kernel-rt to build is version
 KMAJOR=2.6.33
 KMINOR=.9
 KMICRO=-rt31

 # Directory where the source codes should be saved
 SRC_DIR=/usr/src

## Variables not to edit
 export KERNEL_UNAME=${KMAJOR}${KMINOR}${KMICRO}
 export SYSSRC=/usr/src/linux-headers-${KERNEL_UNAME}
 export KERNEL_MODLIB=/lib/modules/${KERNEL_UNAME}
 export MODULE_ROOT=/lib/modules/${KERNEL_UNAME}
 export SYSOUT=/usr/src/linux-headers-${KERNEL_UNAME}
 export CC=cc
 KERNEL_ORG=http://www.kernel.org/pub/linux/kernel/

clear
if [ $USER = "root" ] ; then
 if [ $(uname -m) != ${ARCH} ] ; then
  echo "#######################################"
  echo "You're running the wrong architecture"
  echo "Continuing anyway"
  echo "#######################################"
 fi
 if [ $(uname -r) != ${GENERIC} ] ; then
  echo "#######################################"
  echo "You're running the wrong kernel"
  echo "Continuing anyway"
  echo "#######################################"
 fi

 # Backup configuration, delete sources and packages in SRC_DIR
 cd $SRC_DIR
 cp linux-${KERNEL_UNAME}/.config config-${KERNEL_UNAME}_$(date +"%b-%d-%Y_%H-%M-%S")
 rm -r linux-${KERNEL_UNAME}
 rm linux-headers-${KERNEL_UNAME}_${KERNEL_UNAME}-10.00.Custom_*.deb
 rm linux-image-${KERNEL_UNAME}_${KERNEL_UNAME}-10.00.Custom_*.deb
 rm linux-${KMAJOR}${KMINOR}.tar.bz2 patch-${KERNEL_UNAME}.bz2
 
 # Download all sources to source directory
 wget ${KERNEL_ORG}v2.6/longterm/v${KMAJOR}/linux-${KMAJOR}${KMINOR}.tar.bz2
 wget ${KERNEL_ORG}projects/rt/patch-${KERNEL_UNAME}.bz2
 
 # Extracting and patching
 tar -jxf linux-${KMAJOR}${KMINOR}.tar.bz2
 mv linux-${KMAJOR}${KMINOR} linux-${KERNEL_UNAME}
 cd linux-${KERNEL_UNAME}
 bzip2 -dc ../patch-${KERNEL_UNAME}.bz2 | patch -p1
 rm ../linux-${KMAJOR}${KMINOR}.tar.bz2 ../patch-${KERNEL_UNAME}.bz2

 # Configuration
 # Add or remove options here
 # - Staging very often disables to build a rt patched kernel
 # - RT_GROUP_SCHED has to be disabled
 # - Debug and trace options might slow down the machine
 # - The default CPU frequency scaling might be important too
 # For processor type and features there should be enabled --->
 # - Tickless System (Dynamic Ticks)
 # - High Resolution Timer Support
 # - HPET Timer Support
 # - Preemption Mode (Complete Preemption (Real-Time))
 # - Timer frequency (1000 HZ)
 cp /boot/config-$(uname -r) .config
 echo "CONFIG_STAGING=n" >> .config
 echo "CONFIG_RT_GROUP_SCHED=n" >> .config
 echo "CONFIG_DEBUG_INFO=n" >> .config
 echo "CONFIG_DEBUG_KERNEL=n" >> .config
 echo "CONFIG_FTRACE=n" >> .config
 echo "CONFIG_PREEMPT_RT=y" >> .config
 echo "CONFIG_HZ_1000=y" >> .config
 # Optimize to CPU
 # It's possible to optimize to CPUs
 # e.g. set to CONFIG_MK8=y (Opteron/Athlon64/Hammer/K8), command out for default
 # echo "CONFIG_MK8=y" >> .config
 
 # 32-bit only
 # HIGHMEM from 4G to 64G
 # echo "CONFIG_HIGHMEM4G is not set" >> .config
 # echo "CONFIG_HIGHMEM64G=y" >> .config
 
 # [...]
 make oldconfig

 # Building the kernel
 make-kpkg clean
 make-kpkg --rootcmd fakeroot --initrd kernel-image kernel-headers
 ## End

 # Additional commands
 # Add or remove any command you wish to run or not to run
 # Installing new packages
 make-kpkg clean
 dpkg -i ../linux-image-${KERNEL_UNAME}_${KERNEL_UNAME}-10.00.Custom_*.deb
 # dpkg -i ../linux-headers-${KERNEL_UNAME}_${KERNEL_UNAME}-10.00.Custom_*.deb

else
 echo "Run \"sudo sh rt4us-natty\""
fi
exit 0
