#!/bin/sh -e
# NAME=Partimage
# DESCRIPTION=Actually this is not a real test. It can be used to write prepared raw disk image using Partimage utility.
# DESTROYS_HDD=true
# IS_INTERACTIVE=false
# POWEROFF_DURING_TEST=false
# VERSION=0.1
# TAGS=
# DEPENDS=HDD,Disk Controller
# VAR=SOURCE:string:raw_disk_image:Absolute of relative path to source raw disk image to be written
# VAR=TARGET:string:sda:Target device name that will be overwritten
# VAR=COPY_TO_RAMDISK:bool:false:Copy an image to ramdisk before partimage call

. _inq-config-global; . $SHARE_DIR/functions-test

exit_handler()
{
	[ -e "$RAMDISK_IMAGE" ] && rm -f $RAMDISK_IMAGE
}

RAMDISK_IMAGE=`mktemp`

pushdq $SHARE_DIR/firmwares

# Some protection from fools to produce test fail comments
[ -e "$SOURCE" ] || test_failed 'Source file does not exist'
[ -b "/dev/$TARGET" ] || test_failed 'Target device is not block device'

# Do we need to precopy image to RAM
if [ "$COPY_TO_RAMDISK" = "false" ]; then
        true
else
	echo -n "Precopying image to ramdisk..."
	cp "$SOURCE" $RAMDISK_IMAGE
	echo_success
	SOURCE=$RAMDISK_IMAGE
fi

# Perform disk image restoring itself
partimage restore --batch /dev/"$TARGET" "$SOURCE" || test_failed 'Partimage failed'

popdq
test_succeeded
