#!/bin/bash
source $HOME/.bash_profile
ssh-add $HOME/.ssh/id_cron

function version() # DIR SVN_OPTIONS
{
    cd $1
    svn info $2 | grep Revision | cut -c 11- 
}

function update() # DIR
{
    echo "Updating $1"
    cd $1
    svn up || ( echo ; echo "***************** Update $1 FAILED ******************" ; exit 1)
    cd -
}

cd $WORKDIR/testing

DYN_CUR=$(version DYNAMICO)
DYN_HEAD=$(version DYNAMICO '-r HEAD')
XIOS_CUR=$(version XIOS)
XIOS_HEAD=$(version XIOS '-r HEAD')
echo 'DYNAMICO : ' $DYN_CUR $DYN_HEAD
echo 'XIOS : ' $XIOS_CUR $XIOS_HEAD

UPDATE=n
if [[ $DYN_CUR != $DYN_HEAD ]]; then
    echo "DYNAMICO needs an update from r$DYN_CUR to r$DYN_HEAD"
    UPDATE=y
fi
if [[ $XIOS_CUR != $XIOS_HEAD ]]; then
    echo "XIOS needs an update from r$XIOS_CUR to r$XIOS_HEAD"
    UPDATE=y
fi

if [[ $UPDATE == y ]]; then
    VERSION="${DYN_HEAD}_${XIOS_HEAD})"
    update XIOS || exit 1
    update DYNAMICO || exit 1
    ./build.sh || exit 1
    ./create_runs.sh || exit 1
    ./submit.sh || exit 1
fi
