#!/bin/bash
# Test a remote machine such as Fedora 20
#

#DISTRO=rhel-6.2-x86_64
DISTRO=rhel-7-x86_64
#DISTRO=rhel-5-x86_64
#DISTRO=fedora-20-x86_64

NAME=`pbdistrocheck $DISTRO | grep -E '^Name:' | awk '{print $2}'`
VER=`pbdistrocheck $DISTRO | grep -E '^Ver:' | awk '{print $2}'`
ARCH=`pbdistrocheck $DISTRO | grep -E '^Arch:' | awk '{print $2}'`
D=$DISTRO
DEST=/users/mondo/small
DIR=images
TEMP=temp
CACHE=cache
MNT=/mnt
KEY=$HOME/.ssh/pb_rsa
port=`ps auxww | grep qemu | grep -w $D | grep -v grep | perl -p -e 's|.*127.0.0.1:([0-9]+)-:.*|$1|'`
if [ _"$port" == _"" ]; then
	pb -p mondorescue -r branches/3.3 -m $D launchvm
fi

pb -p mondorescue -r branches/3.3 -m $D sbx2vm MondoRescue mondo mindi mindi-busybox
cat > /tmp/test << EOF
cd /etc/yum.repos.d
yum install -y curl
if [ ! -f /etc/yum.repos.d/mondorescue.repo ]; then
	curl ftp://ftp.mondorescue.org/$NAME/$VER/$ARCH/mondorescue.repo -o mondorescue.repo
fi
if [ ! -f /etc/yum.repos.d/mondorescue-test.repo ]; then
	curl ftp://ftp.mondorescue.org/test/$NAME/$VER/$ARCH/mondorescue-test.repo -o mondorescue-test.repo
fi
yum makecache fast
yum install mondo -y
yum update mondo mindi perl-MondoRescue mindi-busybox -y
yum install nfs-utils -y
modprobe nfsv3
mount -o vers=3 10.0.2.2:$DEST $MNT
mkdir -p $MNT/$DIR $MNT/$TEMP $MNT/$CACHE
mondoarchive -N -G -K 99 -E '/home|/var/log|/usr/share/doc|/usr/src|/usr/share/texmf|/mnt' -s 4480m -n 10.0.2.2:$DEST -d $DIR -p $D -z -O -S $MNT/$CACHE -T $MNT/$TEMP 
umount $MNT/
EOF
# Now should be OK
port=`ps auxww | grep qemu | grep -w $D | grep -v grep | perl -p -e 's|.*127.0.0.1:([0-9]+)-:.*|$1|'`

ssh -i $KEY -p $port root@localhost "rm -rf ./test"
scp -i $KEY -P $port /tmp/test root@localhost:
ssh -i $KEY -p $port root@localhost "chmod 755 ./test ; ./test"

sudo chown bruno $DEST/temp
Q=$DEST/temp/test.qemu
qemu-img create -f qcow2 $Q 7G
cmd="qemu-kvm -m 2048 -boot d -cdrom $DEST/$DIR/$D-1.iso $Q"
echo "Calling restore test: $cmd"
$cmd
