#!/bin/sh
# tla-svn-sync -- Propagate changes between arch and Subversion
#
#  Copyright (C) 2004  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>
#
#-
#   -A, --no-arch-commit  Do not commit Subversion changes to arch,
#                         only commit arch changes to Subversion
#
#   -C, --cvs             Set peer SCM to CVS
#   -S, --svn             Set peer SCM to Subversion (default)
#
#       --id-hint-tree=TREE_ROOT
#                         When adding an id-tag to a new file, search TREE_ROOT
#                         for the same file (by name) and use its id-tag if
#                         found.  Multiple --id-hint-tree options may be
#                         specified, in which case they are searched in the
#                         order given.
#
#       --help            Display a help message and exit
#       --version         Display a release identifier string and exit
#
# This command does bi-directional gatewaying between a Subversion
# repository and an arch branch.  It expects to be run in a source tree
# which is simultaneously an arch project tree and a Subversion working
# directory (and so has both `{arch}' and `.svn' special directories;
# ~/.cvsignore may be useful).
#
# * In the Subversion->arch direction, an `svn update' will be done, and
#   any resulting changes committed to the project tree's arch branch
#   with the log message `Update from Subversion' (preserving changeset
#   boundaries in the Subversion->arch direction is significantly more
#   difficult than only doing so in the arch->Subversion directly, so no
#   attempt is made to that).
#
#   Before committing to arch, it will automatically add taglines and
#   handle explicit tag changes (add/delete/move) as best it can, using
#   the `tla-update-ids' program; use `tla-update-ids --help' for more
#   information on this (in particular, the rules used to add taglines
#   may be customized using an {arch}/=tagline-rules file).
#
# * In the arch->Subversion direction, pending changesets will be
#   applied one by one, and each committed to Subversion with a log
#   message derived from the arch patch log.
#
# If at any point a problem arises -- a conflict or error is detected,
# an arch tree-lint fails, etc. -- an error message is printed and the
# tla-svn-sync aborts.  Once the problem has been corrected, just
# re-invoke tla-svn-sync, and it should continue correctly.
#
# For convenience, command-line options are also read from the file
# {arch}/+cvs-sync-options (in which shell quoting and variable
# expansion are allowed).

# (---- 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='srivasta@debian.org--etch/tla-tools--devo--0--patch-4
'
# (---- 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 ----)

# Take a very quick look at the command line options to see if we should
# give a help message.
case "$1" in
  --help|-h|-H|-[?])
    help; exit 0;;
  --version|-V)
    version; exit 0;;
esac

exec "${TLA_TOOL_PFX}tla-cvs-sync" -S "$@"

