#!/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) check for LOCAL_BOOT
#--------------------------------------
if [ $LOCAL_BOOT = "yes" ];then
	exit 0
fi

#======================================
# 2) update mount table
#--------------------------------------
updateMTAB

#======================================
# 3) create initrd on diskful
#--------------------------------------
if test -z $KIWI_INITRD;then
if test ! -z $DISK;then
	if test $systemIntegrity = "clean";then
		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"
			systemIntegrity=unknown
		fi
	fi
fi
fi

#======================================
# 4) Install boot loader on diskful
#--------------------------------------
if test ! -z $DISK;then
if test $systemIntegrity = "clean";then
	if test -f "/usr/sbin/grub";then
		Echo "Installing boot loader..."
		/usr/sbin/grub --batch --no-floppy < /etc/grub.conf >/dev/null 2>&1
		echo "alias eth0 $networkModule" >> /etc/modules.conf
	else
		Echo "Image doesn't provide grub support"
		Echo "Can't install boot loader"
	fi
fi
fi

#======================================
# 5) Reset systemIntegrity
#--------------------------------------
if test ! -z $DISK;then
if test ! -z $RELOAD_CONFIG;then
	systemIntegrity=$systemIntegrity_save
fi
fi

#======================================
# 6) check for valid mount points 
#--------------------------------------
if test ! -z $DISK;then
	IFS=":" ; for i in $PART_MOUNT;do
	if test ! -z "$i";then
	if test ! -f "$i";then
		mkdir -p $i
	fi
	fi
	done
fi

#======================================
# 7) Update /etc/ImageVersion files
#--------------------------------------
if test ! -z $DISK;then
	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
fi

#======================================
# 8) setup network for nfs boot
#--------------------------------------
if test ! -z $NFSROOT;then
	mount -o nolock -t proc  proc    /proc
	mount -o nolock -t sysfs sysfs   /sys
	mount -o nolock -t devpts devpts /dev/pts
	/etc/init.d/portmap start
fi
