#!/bin/sh

# ABINIT-Forge-Branch
# Helper script to access the ABINIT Forge
#
# Copyright (C) 2007-2009 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 two-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 two-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.

About the arguments:

    two-digit-version ---> the first two digits of an ABINIT version
    repository        ---> the committer's repository (usually the login)
    branch            ---> the branch to consider

    Example:

        $0 get 5.5 gmatteo devel-public

      will fetch the public development branch of Matteo Giantomassi
      for the 5.5 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
archive_user=""
archive_host="archives.abinit.org"
archive_path="/abinit"

test "${archive_user}" = "" || archive_host="${archive_user}@${archive_host}"

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

location="bzr+ssh://${archive_host}/${archive_path}/${version}/${repository}/${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
