#!/bin/bash

hookName=$1     # name of hook script calling this code
mountPrefix=$2  # mount path of the image
destsPrefix=$3  # base dir for the config files
unum=$4         # boot partition ID
rdev=$5         # root partition
ufix=$6         # title postfix
swap=$7         # optional swap partition
conf=$destsPrefix/boot/boot.script
kernel=""
initrd=""

#======================================
# make sure conf dir exists
#--------------------------------------
mkdir -p $destsPrefix/boot
#======================================
# check for device by ID
#--------------------------------------
local diskByID=`getDiskID $rdev`
local swapByID=`getDiskID $swap swap`
#======================================
# check for system image .profile
#--------------------------------------
if [ -f $mountPrefix/image/.profile ];then
	importFile < $mountPrefix/image/.profile
fi
#======================================
# check for kernel options
#--------------------------------------
if [ ! -z "$kiwi_cmdline" ];then
	KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
fi
#======================================
# create standard entry
#--------------------------------------
IFS="," ; for i in $KERNEL_LIST;do
	if test -z "$i";then
		continue
	fi
	# /.../
	# On other systems which supports a boot menu we add
	# the linux kernel and initrd with its file names
	# like this:
	#
	#   kernel=`echo $i | cut -f1 -d:`
	#   initrd=`echo $i | cut -f2 -d:`
	#
	# But on arm systems there can only be one kernel because
	# uboot doesn't support a menu. Thus perl-bootloader also
	# doesn't support updating the uboot configuration and
	# that's the reason why we use the links created by
	# mkinitrd to point to the kernel and initrd in order
	# to support kernel updates
	# ----
	kernel=uImage
	initrd=initrd
	echo "setenv ramdisk $initrd" > $conf
	echo "setenv kernel $kernel" >> $conf
	echo "setenv initrd_high \"0xffffffff\"" >> $conf
	echo "setenv fdt_high \"0xffffffff\""    >> $conf
	echo 'printenv kerneladdr || setenv kerneladdr ${kernel_addr_r}' \
		>> $conf
	echo 'printenv ramdiskaddr|| setenv ramdiskaddr ${ramdisk_addr_r}' \
		>> $conf
	echo -n "setenv bootcmd \"" >> $conf
	echo -n "ext2load mmc 0:1 \${kerneladdr} $kernel; " >> $conf
	echo -n "ext2load mmc 0:1 \${ramdiskaddr} $initrd; " >> $conf
	echo "bootm \${kerneladdr} \${ramdiskaddr}\"" >> $conf
	echo -n "setenv bootargs root=$diskByID loader=$loader" >> $conf
	if [ ! -z "$imageDiskDevice" ];then
		echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
	fi
	if [ ! -z "$swap" ];then
		echo -n " resume=$swapByID" >> $conf
	fi
	if [ "$haveLVM" = "yes" ];then
		echo -n " VGROUP=$VGROUP" >> $conf
	fi
	echo -n " $KIWI_INITRD_PARAMS"  >> $conf
	echo -n " $KIWI_KERNEL_OPTIONS" >> $conf
	echo ";"    >> $conf
	echo "boot" >> $conf
	# only one entry...
	break
done

