#!/usr/bin/bash

. /ka2/replication.conf

echo Looking for default kernel
IMG=`cat $CHROOT/etc/lilo.conf | grep default | cut -d "=" -f 2 | sed -e 's/\"//g'`
KERN="x"
INITRD=""
LABEL="x"

modprobe loop

for i in `cat $CHROOT/etc/lilo.conf`; do

	if echo $i | grep image >/dev/null; then
		KERN=$CHROOT`echo $i | cut -d "=" -f 2`
		INITRD=""
		LABEL="x"
	fi
	if echo $i | grep label >/dev/null; then
		LABEL=`echo $i | cut -d "=" -f 2 | sed -e 's/\"//g'`
	fi
	if echo $i | grep initrd >/dev/null; then
		INITRD=`echo $i | cut -d "=" -f 2`
	fi
	if [ ${LABEL} == ${IMG} ] && [ ! -z ${INITRD} ]; then
		echo "Kernel name is $KERN"
		echo "Initrd name is $INITRD"
		KERN_VERSION=`/usr/bin/strings $KERN | grep "^2\.[2.6]"| cut -d " " -f 1`
		if [ -z $KERN_VERSION ]; then
			echo "No kernel version found !"
		fi
#		chroot $CHROOT /sbin/makedev /dev
	        chroot $CHROOT /sbin/mkinitrd -v -f $1 $INITRD $KERN_VERSION
		chroot $CHROOT /sbin/lilo -v
		exit
	fi
done

