#!/bin/sh

## Create minimal developement environment for NEMO
###################################################

## Optional arguments (developer ID, 'repo' to replicate the layout of the whole repository)

dir='NEMO_dev'; repo='0'; root='/NEMO'
ver='4.0.1'

year=$( date +%Y )

while (( $# )); do
    [ "$1" == 'repo' ] && { dir='NEMO_repo' && repo='1' && root='/'; shift; }
    id=$1
    shift
done

[ -d $dir ] && { echo $dir 'already exists => QUIT'; exit 2; }

## Copy of the repository with empty root directories
echo 'Download a void working copy of' $root 'from the repository ("svn co --depth empty")'
svn co --depth empty http://forge.ipsl.jussieu.fr/nemo/svn/$root $dir
echo


## Switch to developer role if ID is provided
if [ -n "$id" ]; then
    echo 'Switch to developer role by relocating the URL scheme from HTTP to SVN+SSH'
    echo '("svn sw --relocate http://... svn+ssh://...")'
    ssh-copy-id -i ~/.ssh/id_rsa.pub $id@forge.ipsl.jussieu.fr 2> /dev/null
    svn sw --relocate    http://forge.ipsl.jussieu.fr                        \
                      svn+ssh://$id@forge.ipsl.jussieu.fr/ipsl/forge/projets \
        $dir
    echo
fi

## If requested by 'repo',
if [ "$repo" == '1' ]; then
    echo "With 'repo' arg, create a lightweight copy of the repository"
    echo 'for the developement of the sources defined as "externals"'

    for item in utils/build/{arch,makenemo,mk} utils/tools       \
                vendors/{AGRIF/dev,AGRIF/release-$ver,FCM,IOIPSL}  ; do
        echo '¤' $item
        svn up -q --parents $dir/$item
    done

    dir+=/'NEMO'
    echo
fi


## Get the 1st level tree under 'branches' and 'releases'
echo "Get empty folders under '/NEMO/branches' (\"svn up --depth empty --parents\"):" \
    {UKMO,$((year - 1)),$year}
svn up -q --set-depth empty --parents $dir/branches/{UKMO,$((year - 1)),$year}
echo


## Download trunk and last release
echo 'Full download of 2 main branches ("svn up"): trunk and last stable'
echo "¤ '/NEMO/trunk'"
svn up -q           $dir/trunk

echo "¤ '/NEMO/releases/release-$ver'"
svn up -q --parents $dir/releases/release-$ver

exit 0
