#!/bin/sh -e
#
# This script is put in the public domain by Henrik Nordström
# <hno@squid-cache.org>. It 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.
#
# If you find this script useful then you are encouraged to send feedback
# to the author, but you have absolutely no obligation to do so.
#
# Author:
#   Henrik Nordström <hno@squid-cache.org>
#   http://hem.passagen.se/hno/
#
# Distribution URL:
#   http://devel.squid-cache.org/CVS.html
#
# Version history:
#   2001-09-25	First public domain release
#   2001-12-25  Added the ability to move to a new base branch
#
if [ $# -ne 1 -a $# -ne 3 ] || [ ! -f CVS/Repository ]; then
    echo "Usage: $0 branchname [oldbase newbase]"
    echo ""
    echo "Creates a new branch from the current working directory"
    echo
    echo "The new branch is normally based on it's natural ancestor"
    echo "but can optionally be grafted onto another branch"
    exit 1
fi
if [ -f CVS/Tag ]; then
    thistag=`cat CVS/Tag|cut -c2-`
else
    thistag=HEAD
fi
if [ $# -eq 3 ]; then
    oldbase=$2
    newbase=$3
    moved=1
else
    oldbase=$thistag
    newbase=$thistag
    moved=
fi
rootdir=`cat CVS/Root|sed -e 's/.*://'`
module=`cat CVS/Repository|sed -e "s!^$rootdir!!"`
branch=`echo $1|sed -e 's/\./_/g'`
workdir="../$1"
if [ -d $workdir ]; then
   echo "Error! $workdir already exists"
   exit 1
fi
cp -rp . $workdir &
mergetag="Z-${branch}_merge_${newbase}"
thismergetag="Z-${thistag}_merge_${oldbase}"
cvs -q rtag -r $thistag -b $branch $module
if [ $moved ]; then
    cvs -q rtag -r $thismergetag $mergetag $module
else
    cvs -q rtag -r $branch $mergetag $module
fi
wait
cd $workdir
cvs update -P -d -r $branch
echo "Branch created. Now go to $workdir and hack away"