#!/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:
#   2002-06-23	First public domain release
#
if [ -f CVS/Tag ]; then
    localtag=`cat CVS/Tag|cut -c2-`
fi
if [ "$1" = "-b" ]; then
    localtag="$2"
    shift
    shift
fi
if [ $# -ne 2 ]; then
   exec >&2
   echo "Usage: $0 [-b branch] base-tag patchversion"
   echo "    where base-tag is the base branch the diff should start from"
   echo "    (usually HEAD, but you should know...) and patchversion is"
   echo "    is the same patch version as used when creating the patch"
   echo "    with cvsmkpatch"
   exit 1
fi
if [ ! -f CVS/Root ] || [ ! -f CVS/Repository ]; then
    exec >&2
    echo "ERROR: The script must be run from a CVS working directory"
    exit 1
fi
if [ -z "$localtag" ]; then
    exec >&2
    echo "ERROR: The script can only be run on branches. Running"
    echo "it on the HEAD version does not make sense."
    exit 1
fi
rootdir=`cat CVS/Root|sed -e 's/.*://'`
module=`cat CVS/Repository|sed -e "s#^$rootdir##"`

mergefrom="$1"
patchversion="$2"

ecvs() {
    echo cvs $* >&2
    cvs "$@"
}
o () {
    echo "# $*..."
}

tag="${localtag}-${patchversion}"
mergetag="Z-${tag}_merge_${mergefrom}-$patchversion"

echo "Patch file generated `date` from"
if [ "$tag" != "$localtag" ]; then
    echo "CVS tag: ${tag}"
fi
echo "CVS branch ${localtag}"
echo "CVS base branch ${mergefrom}"
echo "CVS repository: `cat CVS/Root`"
echo "CVS module: $module"
echo
echo "cvs -q rdiff -u -kk -r ${mergetag} -r ${tag} ${module}"
ecvs -q rdiff -u -kk -r ${mergetag} -r ${tag} ${module} | cleanpatch