#!/bin/sh
# tla-fork -- Create an arch branch forking off an existing branch
# Usage: tla-fork [OPTION...] NEW_BRANCH
#
#  Copyright (C) 2003, 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>
#
#-
#       --no-cacherev   Do not cacherev tag even if different archive
# 
#       --dest DEST     Instead of modifying the project tree in-place, make a
#                       copy of it to DEST and apply the result to that
#   -d, --dir DIR       Use project tree in DIR
#   -A, --archive       Override `my-default-archive'
# 
#   -h, --help          Display a help message and exit
#   -V, --version       Display a release identifier string and exit
# 
# This command should be run in an arch project tree.  It will create a
# new branched called BRANCH, which is a tag of the project tree's
# branch, and move the project tree to the new branch.

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

me=`basename $0`

bindir='/usr/bin'
AWK='/bin/awk'; 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='no'

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 ----)

DIR='.'
DEST=''
DEFAULT_ARCHIVE=''
TAG_OPTS=''

# Parse command-line options
while :; do
  case "$1" in
    --dest)
      DEST="$2"; shift 2;;
    --dest=*)
      DEST=`long_opt_val "$1"`; shift;;
    -A|--archive)
      DEFAULT_ARCHIVE="$2"; shift 2;;
    --archive=*)
      DEFAULT_ARCHIVE=`long_opt_val "$1"`; shift;;
    --no-cacherev)
      TAG_OPTS="$TAG_OPTS $1"; shift;;
    -A*)
      DEFAULT_ARCHIVE=`short_opt_val "$1"`; shift;;
    -d|--dir)
      DIR="$2"; shift 2;;
    --dir=*)
      DIR=`long_opt_val "$1"`; shift;;
    -d*)
      DIR=`short_opt_val "$1"`; shift;;
    --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

test "$#" = 1 || { cmd_line_err; exit 1; }

BRANCH="$1"
CUR_REVISION=`$TLA logs -rf -d "$DIR" | $SED 1q`

if test x"$DEFAULT_ARCHIVE" = x; then
  DEFAULT_ARCHIVE=`echo "$CUR_REVISION" | $SED 's@/.*@@'`
fi

$TLA tag --setup -A"$DEFAULT_ARCHIVE" $TAG_OPTS "$CUR_REVISION" "$BRANCH"

if test x"$DEST" = x; then
  $TLA join-branch -A"$DEFAULT_ARCHIVE" -d"$DIR" "$BRANCH"
  $TLA set-tree-version -A"$DEFAULT_ARCHIVE" -d"$DIR" "$BRANCH"
else
  $TLA join-branch -A"$DEFAULT_ARCHIVE" -d"$DIR" --dest="$DEST" "$BRANCH"
  $TLA set-tree-version -A"$DEFAULT_ARCHIVE" -d"$DEST" "$BRANCH"
fi

