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

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=IOIPSL

#- Vendor project url
vendor_url=http://forge.ipsl.jussieu.fr/igcmg/svn/IOIPSL/tags

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 $vendor_url/$new_version $new_version

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

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

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

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

exit 0

