#!/bin/sh
VERSION=260103
SCRIPT=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPT)
SCRIPTDIR=$(dirname $SCRIPT)
if [ "$1" = "-v" ] ; then echo "$VERSION $SCRIPTNAME $SCRIPTDIR";exit 0; fi
if [ -e "$SCRIPTDIR/esc.sh" ] ; then . "$SCRIPTDIR/esc.sh"; fi
LOG=$LOGDIR/log-$SCRIPTNAME.log

function listds(){
echo "listing availiable datastores..."
# work with: esxcli storage filesystem list
for entry in $(ls /vmfs/volumes  | grep -vE "BOOTBANK*|LOCKER*|OSDATA*") ; do
        # ignore guid
        #reg="^([a-zA-Z0-9]{8})-([a-zA-Z0-9]{8})$"
        reg="^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$"
        echo "$entry" | grep -Eq $reg
        if [ $? == 0 ] ; then
                if [ "$debug" == 1 ] ; then echo "ignoring (f:automnt): $entry"; fi
                continue;
        fi
        echo -e "\t$entry"
done
}

rf=$SCRIPTDIR/restore.txt
echo "excluding: $excludes"
if [ -z "$1" ]; then
	listds
	echo "enter target datastore name: "
	read dst
else
	dst=$1
fi

#echo -n "type storage"
#echo "#<DIRECTORY or .TGZ>;<DATASTORE_TO_RESTORE_TO>;<DISK_FORMAT_TO_RESTORE>;<OPTIONAL_RESTORED_VM_DISPLAY_NAME>"
#echo "#DISK_FORMATS"
#echo "# 1 = zeroedthick"
#echo "# 2 = 2gbsparse"
#echo "# 3 = thin"
#echo "# 4 = eagerzeroedthick"

for vm in $(ls -1 $bkpdir) ; do
        echo "vm:$vm"
        latest=$(ls -1t $bkpdir/$vm | head -n1)
        src=$bkpdir/$vm/$latest
        echo -e "\t$src"
        echo "$src;/vmfs/volumes/$entry;3" >> $rf
done

echo "created restore list $rf (datastore: $dst)"
echo "call: ./ghettoVCB-restore.sh -c $rf -l $SCRIPTDIR/logs/restore.log -d 1"
exit

