#!/bin/bash
#================
# FILE          : preinit
#----------------
# PROJECT       : OpenSuSE KIWI Image System
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH. All rights reserved
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : This file is called after the image root
#               : has changed by the linuxrc script
#               :
# STATUS        : BETA
#----------------
#======================================
# Functions...
#--------------------------------------
. /include

#=====================================
# 1) Update /etc/ImageVersion files
#-------------------------------------
count=0
IFS="," ; for i in $IMAGE;do
	count=$(($count + 1))
	field=0
	IFS=";" ; for n in $i;do
		case $field in
			0) field=1 ;;
			1) imageName=$n   ; field=2 ;;
			2) imageVersion=$n; field=3 ;;
			3) imageServer=$n ; field=4 ;;
			4) imageBlkSize=$n
		esac
	done
	atversion="$imageName-$imageVersion"
	versionFile="/etc/ImageVersion-$atversion"
	md5sum=`getSystemMD5Status $count`
	if test `getSystemIntegrity $count` = "clean";then
		echo "$atversion $md5sum" > $versionFile
	fi
done

#======================================
# 2) create initrd
#--------------------------------------
if test -L /boot/boot;then
	mount -t proc proc /proc
	mount -t sysfs sysfs /sys
	mkinitrd 
	umount /sys
	umount /proc
else
	Echo "Image doesn't provide kernel system map"
	Echo "Can't create initrd"
fi

#======================================
# 3) Install boot loader
#--------------------------------------
if test -f "/usr/sbin/grub";then
	Echo "Installing boot loader..."
	/usr/sbin/grub --batch --no-floppy < /etc/grub.conf >/dev/null 2>&1
else
	Echo "Image doesn't provide grub support"
	Echo "Can't install boot loader"
fi
