#!/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) start error log
#--------------------------------------
Echo "Calling pre-init stage in system image"
errorLogStart

#======================================
# 2) check for LOCAL_BOOT
#--------------------------------------
if [ $LOCAL_BOOT = "yes" ];then
	exit 0
fi

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

#======================================
# 4) create framebuffer devices
#--------------------------------------
createFramebufferDevices

#======================================
# 5) create initrd on diskful
#--------------------------------------
if [ ! -z "$OEM_RECOVERY" ];then
	#======================================
	# use kiwi initrd as it is right now
	#--------------------------------------
	pushd /boot &>/dev/null
	rm -f vmlinuz && ln -s linux.vmx  vmlinuz
	rm -f initrd  && ln -s initrd.vmx initrd
	popd &>/dev/null
	bootLoaderOK=1
elif [ -z "$UNIONFS_CONFIG" ] && [ -z "$COMBINED_IMAGE" ]; then
	#======================================
	# use distro initrd via mkinitrd
	#--------------------------------------
	setupSUSEInitrd
else
	#======================================
	# use kiwi initrd from RW partition
	#--------------------------------------
	if [ -d /read-write/boot ];then
		rm -rf /boot && ln -s /read-write/boot /boot
	fi
	pushd /boot &>/dev/null
	rm -f vmlinuz && ln -s linux.vmx  vmlinuz
	rm -f initrd  && ln -s initrd.vmx initrd
	popd &>/dev/null
	kernelList /
	if [ ! -z "$COMBINED_IMAGE" ];then
		Echo "Creating bootloader configuration: [ split system ]"
	else
		Echo "Creating bootloader configuration: [ unified system ]"
	fi
	if [ $HAVE_SWAP -eq 1 ];then
		setupBootLoader / / 1 ${deviceDisk}2 $OEM_BOOT_TITLE $deviceSwap
	else
		setupBootLoader / / 1 ${deviceDisk}2 $OEM_BOOT_TITLE
	fi
	bootLoaderOK=1
fi

#======================================
# 6) Install boot loader if ok
#--------------------------------------
if [ $bootLoaderOK = 1 ];then
	installBootLoader
fi

#======================================
# 7) create /etc/ImagePackages
#--------------------------------------
if [ -x /bin/rpm ];then
	Echo "Creating initial image package info file"
	rpm -qa --last > /etc/ImagePackages
fi
