#!/bin/sh -e

DVHTOOL=/usr/sbin/dvhtool
ARCBOOT_IMG=/usr/lib/arcboot/arcboot.raw
ARCBOOT_CONF=/etc/arcboot.conf

if [ ! -x $DVHTOOL ]; then
	echo "Can't find dvhtool - giving up!"
	exit 1
fi

if [ -z "$1" ]; then
	echo "Usage: arcboot <name_of_disk>"
	exit 1
fi

$DVHTOOL -d $1 --unix-to-vh $ARCBOOT_IMG arcboot

# check if the "image=" lines in $ARCBOOT_IMG refer to valid ELF images
for i in `grep "^[[:space:]]*image="  $ARCBOOT_CONF`; do
 	IMAGE=`echo $i | cut -d'=' -f2`;
	if [ -L $IMAGE ]; then	# if it's a symlink, follow it
		IMAGE=`dirname $IMAGE`/`readlink $IMAGE`
	fi
	if ! ( file $IMAGE | grep ": ELF 32-bit MSB executable" >/dev/null 2>&1 ); then
		echo "Warning: $IMAGE is not an ELF image. Booting it will fail!"
	fi
done

# TODO: better sanity checking of $ARCBOOT_CONF
