#!/bin/bash
# N.B. run this script on vendors directory!!!
set -x

last_version=$1
new_version=$2
name=$3

#- 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=XMLF90

#- Vendor project url
vendor_url=http://forge.ipsl.jussieu.fr/ioserver/svn/XMLF90/src

merge=1
 
if [ $# -ne 3 ]; then
    echo "Usage: $0 old_version new_version user"
    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  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 current r_$new_version

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

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

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