#!/bin/bash

# Usage : This script should be used in modipsl/util directory 
# ./recup_my_ORCHIDEE MYUSERNAME MYBRANCHE [REV]
#    MYUSERNAME  : your personal login(firstname.lastname) or sechiba
#    MYBRANCH    : your personal directory, for exemple perso/martial.mancip
#                  or another branch, for example branches/Hydrology
#    REV         : optional revison number on branch, default is HEAD revision
#
# For exemple :
# ./recup_my_ORCHIDEE martial.mancip perso/martial.mancip
# ./recup_my_ORCHIDEE martial.mancip branches/Hydrology
# ./recup_my_ORCHIDEE sechiba        trunk


MYUSERNAME=$1
MYBRANCH=$2
REV=$3

svn revert mod.def

sed -e "s/--username sechiba/--username ${MYUSERNAME}/" mod.def > mod.def.tmp
mv mod.def.tmp mod.def
sed -e "s&perso/yourlogin&${MYBRANCH}&" mod.def > mod.def.tmp
mv mod.def.tmp mod.def
sed -e "s&#-H- ORCHIDEE_SVN_DEV  ORCHIDEE trunk&#-H- ORCHIDEE_SVN_DEV  ORCHIDEE ${MYBRANCH} &" mod.def > mod.def.tmp
mv mod.def.tmp mod.def

# Extract a specific revision number if argument REV is given
if [ X$REV != X ] ; then
    # Test first if REV is a positive number or HEAD
    if [ $REV == HEAD ] || [ $REV -gt 0 ] ; then
	echo "Extract revision $REV"
    else
	echo "ERROR in third argument. It should be a revison number or HEAD"
	exit
    fi
    sed -e "s&#-C- ORCHIDEE_SVN_DEV  \(.*\)?&#-C- ORCHIDEE_SVN_DEV  \1${REV}&" mod.def > mod.def.tmp
    mv mod.def.tmp mod.def
fi

echo "diff changes in mod.def :"
svn diff mod.def

./model ORCHIDEE_SVN_DEV

