#!/bin/sh
#
# restore image cdom cdrom
#
# Copyright (c) 2004-2006 Hubert Feyrer <hubert@feyrer.de>
#

image=$1
disk=$2

if [ "$disk" = "" ]; then
        disk=wd0                   # Change this to "sd0" for a SCSI disk
fi

if [ "$image" = "" ]; then
        image=r${disk}d.gz
fi

if [ "$image" = "-h" -o "$image" = "help" ]; then
        echo "Usage: $0 [image] [disk]"
        echo "e.g.:  $0 myimage.gz"
        echo "       $0 rwd0d.gz wd1"
        echo ""
        echo "Defaults: image=$image, disk=$disk"
        exit 1
fi

echo "Searching for CD/DVD-drive to restore image '$image' to $disk ..."


for cd in cd0 cd1 cd2 cd3 cd4 cd5
do
	echo -n "Trying ${cd}, files: "
	mount -t cd9660 /dev/${cd}a /mnt >/dev/null 2>&1
	( cd /mnt ; echo * )
	if [ -f /mnt/$image ]; then
		echo "Found image '$image' on $cd."

		# Restore
		echo "Starting restore in 5 seconds, press ^C seconds to abort!"
		for n in 5 4 3 2 1
		do
			echo $n
			sleep 1
		done

		echo Restoring...
		# dd if=/mnt/$image bs=1m \
		# | progress -z dd obs=1m of=/dev/r${disk}d
		dd if=/mnt/$imags of=/dev/r${disk}d obs=1m

		#HF#echo HF: /mnt still mounted
		umount /mnt >/dev/null 2>&1
		done=1
		break

	fi
	umount /mnt >/dev/null 2>&1
done

if [ "$done" = "1" ]; then
	echo done.
else
	echo "Image '$image' not found, aborting."
fi
