 #!/bin/sh
 sdcard_dev=/dev/sdb
 sdcard_file_local=/tmp/sdcard.dd.gz
 sdcard_dir_remote=.
 sdcard_file_remote=$sdcard_dir_remote/$1
 
 
 help() {
 	 echo "$0 - Restore an BeagleBone image from $sdcard_dir_remote to $sdcard_dev".
 	 echo "Select image file on command line, you can do one of"
 	 for f in $sdcard_dir_remote/*dd.gz ; do
 	 	 echo "sudo $0 `basename $f`"
 	 done
 	 exit ;
 }
 
 
if [ $# -ne 1 ] ; then
 	 help
fi

# is $1 a valid file?
if [ ! -f "$sdcard_file_remote" -o ! -s "$sdcard_file_remote" ] ; then
	help 
fi 	


 echo "Restoring SDCard in $sdcard_dev from file $sdcard_file_remote"
 ls -l $sdcard_file_remote 
 
 cp -v $sdcard_file_remote $sdcard_file_local
 pv $sdcard_file_local | gunzip | sudo time dd of=$sdcard_dev bs=1M
 rm $sdcard_file_local
 
 echo "READY!"
 
 
 
 
