# Build preparation
# - copy tar.gz to the vm
# - unpack



source_tar=$1
version=$2
edition=$3
target_host=mysqldev@$4
caller=`basename $5`
target_build_dir="~mysqldev/build"

sshcmd="ssh -i /guibuilds/scripts/ssh_identity_for_vms"
scpcmd="scp -i /guibuilds/scripts/ssh_identity_for_vms"

upload_srcs=1


if test "$source_tar" = ""; then
	echo "ERROR: You must pass source tar file and target host as argument!"
	exit 1
fi

if test "$version" = ""; then
	echo "ERROR: You must specify the version"
	exit 1
fi

if test "$target_host" = ""; then
	echo "ERROR: You must give a target host (eg 10.1.2.3)"
	exit 1
fi


echo "Remove old stuff in $target_build_dir"
# wait a little to give time to change mind
sleep 3
$sshcmd $target_host rm -fr $target_build_dir

$sshcmd $target_host mkdir -p $target_build_dir



if [ $upload_srcs -eq 1 ]; then
	echo "Uploading Sources"
	echo "$source_tar --> $target_host:$target_build_dir/.."
	$scpcmd $source_tar $target_host:$target_build_dir/..
	if [ $? -ne 0 ]; then
		echo failed
		exit 1
	fi
else
	echo "Skip upload srcs"	
fi


echo  "Unpacking sources"
$sshcmd $target_host "cd $target_build_dir; tar xzf ../`basename $source_tar`"
if [ $? -ne 0 ]; then
        echo failed
        exit 1
fi


