#!/bin/bash
#
# update OBS from SVN
# 
# License: GPL
#
# Author: Schlomo Schapiro <rear@schlomo.schapiro.org>
#
# Note: If osc dies with an error about password make sure that osc can actually use your stored password.
#       In my case this was working from my login shell and not working from CRON and the reason
#       was that when running from CRON osc could not use my password that was stored in the GNOME keyring.
#       After disabling keyring/kwallet integration in ~/.oscrc I worked again.

# debug help
#declare -p
#set -x -- force
#pwd

SVN=https://rear.svn.sourceforge.net/svnroot/rear/trunk

# read revision
IFS=": " read junk REV < <(svn info https://rear.svn.sourceforge.net/svnroot/rear/trunk | grep ^Revision)

# read OSB 
IFS=":. " read j1 j2 j3 OBSREV < <(osc cat home:sschapiro rear-snapshot rear-snapshot.spec | grep Version)

if [[ "$REV" != "$OBSREV" || ( "$1" && "$1" = force ) ]] ; then
	echo "SVN revision is $REV, OBS revision is $OBSREV, building in OBS"
	BUILD_DIR=/tmp/rear.$$
	trap "rm -rf $BUILD_DIR" 0
	mkdir -p $BUILD_DIR/obs

	cd $BUILD_DIR

	svn export -r $REV -q "$SVN" svn
	osc co -c home:sschapiro rear-snapshot
	osc del rear-snapshot/*.tar.gz
	prod_name=rear-snapshot VERSION=0.0.$REV DESTDIR=rear-snapshot svn/usr/share/rear/contrib/svn2tar
	osc add rear-snapshot/*.tar.gz
	sed -i -e "/^Version:/s/^Version.*$/Version: 0.0.$REV/" rear-snapshot/rear-snapshot.dsc rear-snapshot/rear-snapshot.spec rear-snapshot/debian.control
	sed -i -e "/rear-snapshot/s/0.0.[0-9]*/0.0.$REV/g" rear-snapshot/debian.changelog
	osc ci --skip-validation -m "SVN -> OBS rev $REV" rear-snapshot
else
	echo "ReaR revision in SVN and OBS match already ($REV)"
fi
