#!/bin/bash

set -x

last_version=$1
new_version=$2
new_version_name=$3
name=$4
 
#- My projet url
svn_repo_url=svn+ssh://$name@forge.ipsl.jussieu.fr/ipsl/forge/projets/mentor/svn/

#- EXTERNAL directory in which we want to merge vendors
svn_external=svn+ssh://$name@forge.ipsl.jussieu.fr/ipsl/forge/projets/mentor/svn/trunk/NEMOGCM/EXTERNAL

#- Vendor branch
vendor_branch=AGRIF

#- Vendor project url
vendor_url=http://forge.ipsl.jussieu.fr/nemo/svn/trunk

merge=1
 
if [ $# -ne 4 ]; then
    echo "Usage: $0 old_version_on_vendor new_version_on_remote new_version_name_on_vendor user"
    echo " *_on_vendor is the tag on the local vendor branch "
    echo " *_on_remote is the reference on remote model (revision number only):  REVNUMBER"
    exit 1
fi
 
[ ! -d tmp_update_nemo ] && mkdir tmp_update_${vendor_branch}
tmp_dir=tmp_update_${vendor_branch}
cd $tmp_dir

svn export -r $new_version $vendor_url/AGRIF  r_$new_version

/sw/share/svn/contrib/client-side/svn_load_dirs/svn_load_dirs.pl $svn_repo_url/vendors/$vendor_branch -t r_$new_version_name current r_$new_version

echo "Downloading and extracting AGRIF version $new_version..." 
svn co $svn_external/AGRIF

if [ $merge == '1' ]; then
    svn merge "$svn_repo_url/vendors/$vendor_branch/$last_version" "$svn_repo_url/vendors/$vendor_branch/current" AGRIF
fi
 
## NOTE : after a merge we have conflicts
## to resole them it is necessary to do, for example : svn resolved AGRIF/directory with conflits

## NOTE : if you have a good merge then you have to committ:
## cd $tmp_dir/$vendor_branch
## svn status ../AGRIF/
## svn ci --username $name --message "merge AGRIF version $new_version_name"
 
exit 0
1