#!/usr/bin/ksh
#
# Description
#   Simple script to create the symbolic links for the kernel files in /boot
#
# This script creates current link in the directory /boot for the kernel files, e.g.
#
# root@t61p:/boot# ls -l *current*generic
# lrwxrwxrwx 1 root root 23 2008-11-27 22:03 abi-current-generic -> ./abi-2.6.24-22-generic
# lrwxrwxrwx 1 root root 26 2008-11-27 22:03 config-current-generic -> ./config-2.6.24-22-generic
# lrwxrwxrwx 1 root root 30 2008-11-27 22:03 initrd.img-current-generic -> ./initrd.img-2.6.24-22-generic
# lrwxrwxrwx 1 root root 30 2008-11-27 22:03 System.map-current-generic -> ./System.map-2.6.24-22-generic
# lrwxrwxrwx 1 root root 27 2008-11-27 22:03 vmlinuz-current-generic -> ./vmlinuz-2.6.24-22-generic
#
# The GRUB menu entry to use this links might look like
#
#	title		Ubuntu 8.04.1, kernel current-generic
#	root		(hd0,6)
#	kernel		/boot/vmlinuz-current-generic root=/dev/sda7
#	initrd		/boot/initrd.img-current-generic
#	quiet
#
# History
#   28.10.2008 v1.0.0  /bs 
#     initial release
#   21.11.2008 v1.0.1 /bs
#     added support for *386 kernel files
#
# Tested  with
#	Ubuntu 6.06 (x86), Ubuntu 8.04 (x86_64), Ubuntu 8.10 (x86_64)
#
# Usage:: 
#
#   $0 [kernel_revision]
#		
# to configure the kernel revision "kernel_revision" or
#
#   $0
#
# to interactive select the kernel revision to use
#
# Depending on the name of the script the script creates either current links
# for the non-xen kernel or the xen kernel:
#
#	create_current_link.sh   - create the current link for the normal kernel
#     create_current_xen_link.sh - create the current link for the Xen kernel
#
# 
THIS_SCRIPT="$( basename $0 )"

PREFIX=
# for testing
# PREFIX="echo "

KERNEL_DIR="/boot"

if [ "${THIS_SCRIPT}"x = "create_current_link.sh"x ] ; then
	ls "${KERNEL_DIR}/"*386 2>/dev/null >/dev/null       
	[ $? -eq 0 ] && POSTFIX="386" || POSTFIX="generic"
else
	POSTFIX="xen"
fi

echo "${THIS_SCRIPT} - create a current link for the ${POSTFIX} kernel"

if [ "$1"x  =  "-h"x  -o $# -gt 1 ] ; then
	echo "Usage: $0 kernel_revision"
	exit 5
fi

cd "${KERNEL_DIR}"
if [ $? -ne 0 ] ; then
	echo "ERROR: Directory \"${KERNEL_DIR}\" not found"
	exit 10
fi

CUR_KERNEL_REVISION="$( ls -l vmlinuz-current-${POSTFIX} 2>/dev/null | cut -f 7,8 -d "-"  )"
[ "${CUR_KERNEL_REVISION}"x = ""x ] && CUR_KERNEL_REVISION="not available"

if [ $# -eq 1 ] ; then
	KERNEL_REV="$1"
else
	KERNEL_REVISIONS="$( ls initrd.img-*${POSTFIX} 2>/dev/null | grep -v current | cut -f 2,3 -d "-"  )"
	if [ $? -ne 0 ] ; then
		echo "ERROR: No kernels found in the directory \"${KERNEL_DIR}\" "
		exit 15
	fi

	PS3="Select the kernel to use (Current is ${CUR_KERNEL_REVISION}) : "
	select NEW_REV in ${KERNEL_REVISIONS} ; do
		[ "${NEW_REV}"x != ""x ] && break
		REPLY=""
	done
	KERNEL_REV="${NEW_REV}"
fi

echo "Creating the current link for the kernel revision \"${KERNEL_REV}\" ..."

INITRD="initrd.img-${KERNEL_REV}-${POSTFIX}"
VMLINUZ="vmlinuz-${KERNEL_REV}-${POSTFIX}"
SYSTEMMAP="System.map-${KERNEL_REV}-${POSTFIX}"
CONFIG="config-${KERNEL_REV}-${POSTFIX}"
ABI="abi-${KERNEL_REV}-${POSTFIX}"

INITRD_LINK="initrd.img-current-${POSTFIX}"
VMLINUZ_LINK="vmlinuz-current-${POSTFIX}"
SYSTEMMAP_LINK="System.map-current-${POSTFIX}"
CONFIG_LINK="config-current-${POSTFIX}"
ABI_LINK="abi-current-${POSTFIX}"

if [ "${POSTFIX}"x  = "xen"x ] ; then
	KERNEL_FILES="INITRD VMLINUZ SYSTEMMAP CONFIG" 
else
	KERNEL_FILES="INITRD VMLINUZ SYSTEMMAP CONFIG ABI" 
fi

KERNEL_OK=0

for i in ${KERNEL_FILES} ; do
	eval CUR_KERNEL_FILE="${KERNEL_DIR}/\${$i}"
	if [ ! -f  "${CUR_KERNEL_FILE}"  ] ; then
		echo "ERROR: \"${CUR_KERNEL_FILE}\" not found"
		KERNEL_OK=1
	fi
done

[ ${KERNEL_OK} != 0 ] && exit 5

cd "${KERNEL_DIR}"

NEW_LINKS=""
for CURFILE in ${KERNEL_FILES} ; do
	eval CUR_LINK="\${${CURFILE}_LINK}"
	NEW_LINKS="${NEW_LINKS} ${CUR_LINK}"
	eval CUR_LINK_TARGET="\$${CURFILE}"
	( set -v ;  [ -L  "${CUR_LINK}" ] && ${PREFIX} rm "${CUR_LINK}" )
	( set -v ; ${PREFIX} ln -s "./${CUR_LINK_TARGET}" "${CUR_LINK}" )
done

echo "New links created: "
echo ""
ls -l ${NEW_LINKS}

