#!/bin/sh

# ABINIT-Forge-Branch
# Helper script to access the ABINIT Forge
#
# Copyright (C) 2007-2014 ABINIT Group (Yann Pouillon).
# Originally written by Yann Pouillon.
#

#
# This program 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.
#
# This program 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

#
# TUNE AT YOUR OWN RISKS!
#

# Stop at first error
#set -e

        # ------------------------------------------------------------ #

# Check command-line options
if test "${#}" -lt "4"; then

cat <<EOF
Usage:

    $0 get three-digit-version repository branch [directory]
    ---> GET: download a branch from the ABINIT forge using the
              bzr-over-ssh protocol.

              A destination directory may be specified at the end
              of the command line. It should not exist when you run
              the command.

    $0 put three-digit-version repository branch [directory]
    ---> PUT: first-time upload of your changes to the ABINIT forge
              using the bzr-over-ssh protocol.

              The location of an ABINIT source tree may be specified
              at the end of the command line.

         Note: For subsequent runs, "bzr push" should suffice.
         Note: Working only with version 6.x or better.

About the arguments:

    three-digit-version ---> the first three digits of an ABINIT version
    repository        ---> the committer's repository (usually the login)
    branch            ---> the branch to consider [public, private, training,...]

    Example:

        $0 get 6.8.1 gmatteo public

      will fetch the public branch of Matteo Giantomassi for the 6.8.1 version of ABINIT.

For more information, please see the Bazaar Quick Reference for ABINIT
located at:

    http://www.abinit.org/bzr/bzr-quickref.pdf

EOF
exit 0

fi

        # ------------------------------------------------------------ #

# Init environment
forge_user=""
forge_host="forge.abinit.org"
forge_path="abinit"

test "${forge_user}" = "" || forge_host="${forge_user}@${forge_host}"

# Get arguments
action="${1}"
version="${2}"
repository="${3}"
branch="${4}"
directory="${5}"

location="bzr+ssh://${forge_host}/${forge_path}/${repository}/${version}-${branch}/"

# Select what to do
case "${action}" in

  get)
    bzr branch "${location}" ${directory}
    test "${directory}" = "" && directory="${branch}"
    cd "${directory}"
    test -s ".bzrignore" || bzr checkout
    ;;

  put)
    bzr_options="--remember"
    test "${directory}" = "" || \
      bzr_options="${bzr_options} --directory=${directory}"
    bzr push ${bzr_options} "${location}"
    ;;

  *)
    echo "Error: unknown action (${action})"
    exit 1
    ;;

esac
