#!/bin/sh -e

VERSION=$1

if [ -z "$VERSION" ]; then
   echo "No version given"
   exit 1
fi

svn update

perl -pi -e "s/our \\\$VERSION =.*/our \\\$VERSION = '$VERSION';/" LATMOS-Accounts/lib/LATMOS/Accounts.pm
perl -pi -e "s/our \\\$VERSION =.*/our \\\$VERSION = '$VERSION';/" LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web.pm

perl -pi -e "s/^XXX/$VERSION/m" LATMOS-Accounts/Changes
perl -pi -e "s/^XXX/$VERSION/m" LATMOS-Accounts-Web/Changes

find -name "*.tar.gz" -o -name "*.rpm" | xargs rm

(
    cd LATMOS-Accounts
    perl Makefile.PL
    make rpm
)
(
    cd LATMOS-Accounts-Web/;
    perl Makefile.PL;
    make rpm
)

(
    cd ..
    svn cp trunk tags/$VERSION
)

rsync -avPH ./LATMOS-Accounts/noarch/latmos-accounts-*-*.noarch.rpm ./LATMOS-Accounts-Web/noarch/latmos-accounts-web-*-*.noarch.rpm root@yuko.aero.jussieu.fr:/tmp
rsync -avPH ./LATMOS-Accounts/noarch/latmos-accounts-*-*.noarch.rpm ./LATMOS-Accounts-Web/noarch/latmos-accounts-web-*-*.noarch.rpm root@la-template.latmos.upmc.fr:/tmp

rsync -avPH ./LATMOS-Accounts/noarch/latmos-accounts-*-*.noarch.rpm \
            ./LATMOS-Accounts-Web/noarch/latmos-accounts-web-*-*.noarch.rpm \
            ./LATMOS-Accounts/*.src.rpm \
            ./LATMOS-Accounts-Web/*.src.rpm \
            ./LATMOS-Accounts/*.tar.gz \
            ./LATMOS-Accounts-Web/*.tar.gz \
            nanardon@forge.ipsl.jussieu.fr:/ipsl/forge/projets/link-accounts/web/release/${VERSION}/

ssh -t root@la-template.latmos.upmc.fr urpmi /tmp/*.noarch.rpm

svn commit -m "- ${VERSION}"

SVNVERSION="${VERSION}.0"

perl -pi -e "s/our \\\$VERSION =.*/our \\\$VERSION = '$SVNVERSION';/" LATMOS-Accounts/lib/LATMOS/Accounts.pm
perl -pi -e "s/our \\\$VERSION =.*/our \\\$VERSION = '$SVNVERSION';/" LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web.pm

svn commit -m "- Next SVN version"
