#
#
# Does a build in a RPMbased distro VM by
# - copy tar.gz to the vm
# - unpack and run autogen to get the spec generated
# - copy tar to rpm build dir
#w - build rpm 
# - copy back the generated deb package to outgoing

rpm_basedir="~/rpmbuild"

. scripts/bldlib
. scripts/build_package_prepare_upload_unpack

outgoing=$outdir
if test "$outgoing" = ""; then
	outgoing="outgoing"
fi

logpath=log
if test "$outdir" != ""; then
	logpath="$outdir/log"
fi

rpm_name_prefix=`basename $source_tar .tar.gz`

echo "Copying tar to $rpm_basedir/SOURCES"
$sshcmd $target_host "cp `basename $source_tar` $rpm_basedir/SOURCES"
if [ $? -ne 0 ]; then
	error "failed"
	exit 1
fi

#echo "Generate spec"
#ssh $target_host "cd $target_build_dir/`basename $source_tar .tar.gz`; sh autogen.sh --prefix=/usr"
#if [ $? -ne 0 ]; then
#	echo "failed"
#	exit 1
#fi

specname="mysql-workbench"
if test "$edition" == "se"; then
	specname="$specname"
fi


#trap quit_ssh SIGHUP SIGINT
echo "Build RPM at $target_host"
if test "$autobuild" == 1; then
scripts/runcmd -l=$logpath.$caller.`whoami` $sshcmd $target_host \"sed -e s/@VERSION@/$version/ $target_build_dir/$rpm_name_prefix/build/$specname.spec.in \> $specname.spec\; rpmbuild -ba $specname.spec \"
else
scripts/runcmd -n=$caller -m=".* Entering directory" -l=$logpath.$caller.`whoami` $sshcmd $target_host \"sed -e s/@VERSION@/$version/ $target_build_dir/$rpm_name_prefix/build/$specname.spec.in \> $specname.spec\; rpmbuild -ba $specname.spec \"
fi
if [ $? -ne 0 ]; then
	error "$0 failed"
	exit 1
fi



echo "Downloading RPMS"
$scpcmd $target_host:$rpm_basedir/RPMS/*/$rpm_name_prefix-*.*.rpm $outgoing/
$scpcmd $target_host:$rpm_basedir/SRPMS/$rpm_name_prefix-*.src.rpm $outgoing/

echo "Done"

