#!/usr/bin/env bash

# Copyright (C) 2015, ENPC
#     Author(s): Sylvain Doré
#
# This file is part of the air quality modeling system Polyphemus.
#
# Polyphemus is developed in the INRIA - ENPC joint project-team CLIME and in
# the ENPC - EDF R&D joint laboratory CEREA.
#
# Polyphemus is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Polyphemus is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# For more information, visit the Polyphemus web site:
#      http://cerea.enpc.fr/polyphemus/

######################
# This script clones Polyphemus into the current
# directory and sets up the development environment.
#

## Sets Variables.
repository_server=scm.gforge.inria.fr
repository_path=/gitroot/polyphemus/user/kimy/polyphemus
repository=ssh://$repository_server$repository_path

## Tests the repository access:
ssh $repository_server true || {
    echo "Cannot connect to \"$repository_server\"."
    echo "(Have you got an account with your public key registered?)"
    exit 1
}
ssh $repository_server cd $repository_path || {
    echo "Cannot list the content of \"$repository_path\" in \"$repository_server\"."
    exit 1
}

## Selects a manifest name and branch.
manifest=
branch=default

function choose_manifest {
    if ! git clone $repository/manifests tmp_manifests >& /dev/null; then
        echo "Cannot clone $repository/manifests."
        exit 1
    fi

    manifest_list=$(cd tmp_manifests; find -name "*.xml" \! -name "default.xml" |
                        sort | sed 's@^./@@g' | sed 's@.xml$@@g')
    rm -fr tmp_manifests

    echo "Please select a Polyphemus version to install:"

    select manifest in $manifest_list; do break; done
}

if [[ $1 ]]; then
    manifest=$1
    shift
else
    choose_manifest
fi

manifest=$manifest.xml

## Gets the repo executable.
repo=./.repo/repo/repo
if ! type $repo 2>/dev/null; then
    repo=./repo
    if [[ ! -f $repo ]]; then
        git clone https://gerrit.googlesource.com/git-repo
        mv ./git-repo/repo .
        rm -fr ./git-repo
    fi
fi

## Downloads Polyphemus.
$repo init -u $repository/manifests -m $manifest -b $branch $@ || exit 1
$repo sync || exit 1

## Initializes Polyphemus.
$repo forall -c $(pwd)/.dev-toolbox/bin/init-project.sh || exit 1
$repo forall -c "git repo start \$REPO_RREV \$REPO_PROJECT" || exit 1

if [[ -f ./repo ]]; then
    rm ./repo
fi


cat <<EOF

=======================================================
Polyphemus is installed.

Polyphemus is made of multiple Git repositories managed
by a tool called Repo from Google. Repo is accessible through Git:
$ git repo <repo arguments...>

For further information:
$ git repo help

To update your copy of Polyphemus with new development
from the rest of the world, you can use the 'update' Git alias:
$ git update

Once you have committed your code using git, you can
submit your code to the Polyphemus maintainers with the
'submit' Git alias:
$ git submit

Everything is documented in Polyphemus guides, see:
http://cerea.enpc.fr/polyphemus/

For further help, send a message to our mailing list:
polyphemus-help@lists.gforge.inria.fr

=======================================================

EOF
