if [ "$GITSVNLMDZ" != "true" ];then
    export GITSVNLMDZ=true
else
    exit 0
fi
RED='\033[0;31m'
NC='\033[0m' # No Color

# check if on master
local_branch=$(git branch --show-current)
ROOT_DIR=$( git rev-parse --show-toplevel )

echo Branch: $local_branch

echo done
if [ ${local_branch} != "master" ]; then
    # exit 0 # if on other branch than master, do not sync with svn
    use_hook=false
    while read local_ref local_oid remote_ref remote_oid; do
        # echo $local_ref $local_oid $remote_ref $remote_oid
        if test $local_ref = "refs/heads/master"; then
            use_hook=true
        fi
    done
    if test $use_hook = "false" ;then
        echo "Sync with svn not necessary, no changes in master to be pushed."
        echo "Continuing with push on $local_branch."
        exit 0 # push only on local branch
    fi
    git checkout master
fi


### check if git svn exists
git svn info>/dev/null 2>&1
GITSVN=$?
if [ $GITSVN != 0 ]
then
    printf "${RED}You need to install git svn to use this script.${NC}\n"
    git checkout ${local_branch}
    exit $GITSVN
fi

if [ ! -f ${ROOT_DIR}/.git/refs/remotes/git-svn ];then
    git svn init https://svn.lmd.jussieu.fr/Planeto/trunk
    git update-ref refs/remotes/git-svn refs/remotes/origin/master # this could fail if git is not up-to-date with svn...
fi

# git update-ref refs/remotes/git-svn refs/remotes/origin/master # tell git-svn it's up-to-date with git remote master branch

HEAD=$(git rev-list master|head -n 1)
CUR=$(git rev-list master | wc -l)
echo "Before svn rebase, master had $CUR commits"

### get changes from svn
git svn rebase
REBASED=$?
if [ $REBASED != 0 ] # check if rebase worked
then
    echo "${RED}git svn rebase failed.${NC}\n"
    git checkout ${local_branch}
    exit $REBASED
fi
POST=$(git rev-list master | wc -l)
echo "After svn rebase, master has $POST commits"
echo "Object counts were $CUR::$POST"
echo "git svn rebase worked."

if [ ! $CUR = $POST ] # check if there were changes from svn
then
    echo "${RED}You will need to push now.${NC}"
fi

