#!/bin/sh
#
# compile_kernel.sh
# 2014-02-15
# by Gernot WALZL
#
# downloads, compiles and installs the linux kernel
#
# http://slackware.at/data/slackware64-current/source/k/config-x86_64/
#
# Usage: OLDCONFIG=/root/source/k/config-x86_64/config-huge-3.10.17.x64 ./compile_kernel.sh

VERSION=${VERSION:-3.10.30}
SOURCE="linux-${VERSION}.tar.xz"
DOWNLOAD="http://www.kernel.org/pub/linux/kernel/v3.x/${SOURCE}"
SIGNATURE="linux-${VERSION}.tar.sign"
SIGNATURE_DOWNLOAD="http://www.kernel.org/pub/linux/kernel/v3.x/${SIGNATURE}"
GPG_KEY="0x6092693E"
OLDCONFIG=${OLDCONFIG:-/boot/config}


# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
ARCH=$(uname -m)


download () {
  if [ ! -f $CWD/$SOURCE ]; then
    wget -O $CWD/$SOURCE $DOWNLOAD || exit 1
  fi
  if [ ! -f $CWD/$SIGNATURE ]; then
    wget -O $CWD/$SIGNATURE $SIGNATURE_DOWNLOAD || exit 1
  fi

  # import gpg key
  gpg --list-keys "$GPG_KEY"
  if [ $? -ne 0 ]; then
    gpg --keyserver wwwkeys.pgp.net --recv-keys "$GPG_KEY"
  fi

  # decompress
  cd $TMP || exit 1
  xzcat $CWD/$SOURCE > "linux-${VERSION}.tar"
  cat $CWD/$SIGNATURE > $SIGNATURE

  # check signature
  gpg --verify $SIGNATURE

  echo ""
  echo "Press Enter to continue."
  read CONTINUE

  # extract
  tar xvf "linux-${VERSION}.tar" || exit 1

  # clean up
  rm "linux-${VERSION}.tar"
  rm "$SIGNATURE"

  # move to correct location
  mv linux-$VERSION /usr/src || exit 1
}


compile () {
  cd /usr/src/linux-$VERSION || exit 1

  # copy old config
  cp "$OLDCONFIG" .config
  yes "" | make oldconfig

  # print notice
  echo ""
  echo "   NOTICE   "
  echo ""
  echo "Using config from $OLDCONFIG"
  echo ""
  echo "If you are upgrading from 2.6.x to 3.x,"
  echo "you need to enable:"
  echo "Device Drivers ---> Real Time Clock"
  echo ""
  echo "Press Enter to continue."
  read CONTINUE

  # configure kernel
  make menuconfig

  # compile the kernel
  make bzImage
  make modules
}


install () {
  cd /usr/src/linux-$VERSION || exit 1

  make modules_install   # modules get copied to /lib/modules/$VERSION/

  # copy compiled kernel to /boot
  cp .config /boot/config-$VERSION
  cp System.map /boot/System.map-$VERSION
  cp arch/$ARCH/boot/bzImage /boot/vmlinuz-$VERSION

  # update symlinks
  rm /usr/src/linux
  ln -s /usr/src/linux-$VERSION /usr/src/linux
  cd /boot
  rm config System.map vmlinuz
  ln -s config-$VERSION config
  ln -s System.map-$VERSION System.map
  ln -s vmlinuz-$VERSION vmlinuz

  # install bootloader
  /sbin/lilo
}


make_init_ramdisk () {
  CMD=$(/usr/share/mkinitrd/mkinitrd_command_generator.sh | tail -n 1 | sed 's/-k [^ ]*/-k '$VERSION'/')
  echo $CMD
  $CMD
}


uninstall () {
  OLDVER="$1"
  if [ -z "$OLDVER" ]; then
    exit 1
  fi
  if [ ! -z "$(readlink /boot/vmlinuz | grep $OLDVER)" ]; then
    echo "Error: Can not uninstall current kernel."
    exit 1
  fi
  rm -rf /boot/*-$OLDVER
  rm -rf /lib/modules/$OLDVER
  rm -rf /usr/src/linux-$OLDVER
}


case "$1" in
'download')
  download
  ;;
'compile')
  compile
  ;;
'install')
  install
  ;;
'uninstall')
  uninstall "$2"
  ;;
*)
  download
  compile
  install
  ;;
esac