#!/bin/sh
# tla-partner -- Perform a tla operation with a `partner branch'
# Usage: tla-partner [--help|--version] [TLA_ARGS...]
#
#  Copyright (C) 2004, 2006  Miles Bader <miles@gnu.org>
#
# 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.
#
# Written by Miles Bader <miles@gnu.org>
#-
#       --help       Display a help message and exit
#       --version    Display a release identifier string and exit
#
# tla-partner supports performing tla operations with a `partner branch'.
# The partner branch is determined by reading the file {arch}/+partner
# (note that this file is never committed to the archive), and defaulting
# any missing components (archive or version) using the tree-version of the
# current tree.
#
# tla-partner has two forms of invocation:
#
#   (1) Without any arguments, it prints the full version identifier of
#       this tree's partner-version to stdout, and exits.
#
#   (2) With arguments TLA_ARGS... it invokes tla with those arguments and
#       the partner version as an additional last argument.  This is
#       equivalent to doing:
#
#          tla TLA_ARGS... `tla-partner`
#
#       [The reason for this style of tla-partner usage is to make
#       error-handling better when there's no {arch}/+partner file --
#       it will exit with an error message whereas the above command
#       would continue and invoke tla with only TLA_ARGS...]
#       
# The concept of a partner version is useful when merging back and forth
# between different archives or different versions within the same archive,
# for instance, to see what needs to be merged, you can just do:
#
#    tla-partner missing -s
#
# and to actually merge:
#
#    tla-partner star-merge

# (---- beginning of hdr.shpp ----)
# hdr.shpp

me=`basename $0`

bindir='/usr/bin'
AWK='/usr/bin/nawk'; export AWK
TLA='tla'; export TLA
SED='/bin/sed'; export SED
UUIDGEN='uuidgen'; export UUIDGEN

# (---- TLA_TOOLS_VERSION defined from ,tla-tools-version ----)
TLA_TOOLS_VERSION='unknown-version
'
# (---- end of TLA_TOOLS_VERSION defined from ,tla-tools-version ----)

TLA_TOOL_PFX="${bindir+$bindir/}"
export TLA_TOOL_PFX

TLA_ESCAPE='yes'

if test "$TLA_ESCAPE" = yes; then
  TLA_UNESCAPED_OPT='--unescaped'
else
  TLA_UNESCAPED_OPT=''
fi

# Some tools get completely confused in stupid ways by non-default
# settings of LANG (like gawk, which fucks up regexp character ranges).
LANG=C; export LANG

# (---- end of hdr.shpp ----)
# (---- beginning of cmd-line.shpp ----)
# cmd-line.shpp -- Command-line helper functions for shell scripts

script="$0"
case "$script" in
  */*) ;;
  *)   script="${TLA_TOOL_PFX}$script";;
esac

usage ()
{
  $SED -n -e '/^\([^#]\|#-* *$\)/{s@.*@Usage: '"$me"' [--help|--version]@p;q;}'	\
         -e '/^# *Usage:/,/^# *$/{s/^# //p;q;}'				\
     < "$script"
}

short_help ()
{
  $SED -n -e '/^\([^#]\|-*# *$\|# *Usage:\)/q'				\
	 -e '/^#!/d;s/^.*-- */# /;s/^#[ 	]*//p'			\
     < "$script" | fmt
}

help_body ()
{
  $SED -n '/^ *$/q;/^#-/,/^[^#]/s/^#\( \|$\)//p' < "$script"
}

help ()
{
  usage
  short_help
  echo ''
  help_body
}

version ()
{
  local no_nl_vers=`echo "$TLA_TOOLS_VERSION"`
  echo "$me (tla-tools) $no_nl_vers"
  $SED -n '/^[^#]/q;/^#-/q;s/^# *\(Written by\)/\
\1/p' < "$script"
  $SED -n '/^[^#]/q;/^#-/q;s/^# *\(Copyright\)/\
\1/p' < "$script"
}

unrec_opt ()
{
  echo 1>&2 "$me: unrecognized option "\`"$1'"
  echo 1>&2 "Try "\`"$me --help' for more information."
}

cmd_line_err ()
{
  usage 1>&2
  echo 1>&2 "Try "\`"$me --help' for more information."
}

long_opt_val ()
{
  echo "$1" | $SED 's/^[^=]*=//'
}

short_opt_val ()
{
  echo "$1" | $SED 's/^-.//'
}

# (---- end of cmd-line.shpp ----)

# Parse command-line options
while :; do
  case "$1" in
    --help|-h|-H)
      help; exit 0;;
    --version|-V)
      version; exit 0;;
    -[!-]?*)
      # split concatenated single-letter options apart
      FIRST="$1"; shift
      set -- `echo $FIRST | $SED 's/-\(.\)\(.*\)/-\1 -\2/'` "$@"
      ;;
    -*)
      unrec_opt "$1"; exit 1;;
    *)
      break;
  esac
done

if ! test -r {arch}/+partner; then
  echo 1>&2 "$me: {arch}/+partner: partner file not found"
  exit 10
fi

tree_ver=`$TLA tree-version`

partner=`cat {arch}/+partner`

# Maybe add default components to $partner
case "$partner" in
  *@*/*)
    ;;
  *@*/)
    partner="$partner"`basename "$tree_ver"`;;
  *@*)
    partner="$partner/"`basename "$tree_ver"`;;
  /*)
    partner=`dirname "$tree_ver"`"$partner";;
  *)
    partner=`dirname "$tree_ver"`"/$partner";;
esac

if test $# = 0; then
  echo "$partner"
else
  exec $TLA "$@" "$partner"
fi

