#!/bin/sh
# tla-fork -- Create an arch branch forking off an existing branch
# Usage: tla-fork [OPTION...] [NEW_BRANCH]
#
#  Copyright (C) 2003, 2004, 2005, 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>
#
#-
#       --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.
#
# If NEW_BRANCH only contains an archive name, the new branch will be made in
# that archive with the same name as the current version.  If NEW_BRANCH is
# not specified at all, the default archive is used; this form is an easy way
# to fork an external project into one's personal archive.

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

DIR='.'
DEST=''
ARCHIVE=`$TLA my-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)
      ARCHIVE="$2"; shift 2;;
    --archive=*)
      ARCHIVE=`long_opt_val "$1"`; shift;;
    --no-cacherev)
      TAG_OPTS="$TAG_OPTS $1"; shift;;
    -A*)
      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

case "$#" in
  1)
    # Fork using specified name
    BRANCH="$1"
    ;;
  0)
    # Fork into default archive
    BRANCH="$ARCHIVE"
    ;;
  *)
    cmd_line_err
    exit 1
esac

CUR_REVISION=`$TLA logs -rf -d "$DIR" | $SED 1q`
CUR_VERSION=`$TLA tree-version`

# Removing trailing slash from "archive-only" names
case "$BRANCH" in
  *@*/)
    BRANCH=`echo "$BRANCH" | sed 's@/$@@'`;;
esac

# Check for the special case of _only_ an archive name being specified,
# in which case we fork the same version as the current tree,
# in the new archive.
case "$BRANCH" in
  *@*/*--*--[0-9]*)
    ARCHIVE=`echo "$BRANCH" | sed 's@/.*@@'`
    ;;
  */*)
    echo 1>&2 "$me: $BRANCH: Invalid branch name"
    exit 22
    ;;
  *--*--[0-9]*)
    BRANCH="$ARCHIVE/$BRANCH"
    ;;
  *@*)
    ARCHIVE="$BRANCH"
    BRANCH="$BRANCH/`basename "$CUR_VERSION"`"
    ;;
  *)
    echo 1>&2 "$me: $BRANCH: Invalid branch name"
    exit 23
esac

if test x"$BRANCH" = x"$CUR_VERSION"; then
  echo 1>&2 "$me: $BRANCH: Fork branch has same name as current branch; nothing done"
  exit 21
fi

$TLA tag --setup $TAG_OPTS "$CUR_REVISION" "$BRANCH" || exit $?

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

