#!/bin/sh -e
#
# make_patch.sh
#
# Make a patch from CVS given revision numbers
# and file patch relative to "ccp4"
# This uses the rdiff CVS command and requires
# that CVSROOT is set appropriately
#
# Usage: make_patch [-d <cvsroot>] -r<1> -r<2> <dir> <file>
usage="make_patch [-d <cvsroot>] -r<1> -r<2> <dir> <file>"
# Initialise
cvsroot=""
r1=""
r2=""
dir=""
filen=""
# Parse command line
arg=$1
while [ ! -z "$arg" ] ; do
  if [ "1$arg" == "1-d" ] ; then
    # Add extra character in test for -d to
    # avoid problem with /bin/sh on IRIX 6.5
    shift
    cvsroot=$1
  elif [ -z "$r1" ] ; then
    echo "r1=$arg"
    r1=$arg
  elif [ -z "$r2" ] ; then
    echo "r2=$arg"
    r2=$arg
  elif [ -z "$dir" ] ; then
    echo "dir=$arg"
    dir=$arg
  elif [ -z "$filen" ] ; then
    echo "filen=$arg"
    filen=$arg
  else
    echo "Bad or extra command line argument: $arg "
    echo "Usage: $usage"
    exit 1
  fi
  shift
  arg=$1
done
# Check everything
if [ -z "$cvsroot" ] ; then
  if [ ! -z "$CVSROOT" ] ; then
    cvsroot=$CVSROOT
  else
    echo "Usage: $usage"
    echo "No CVSROOT specified"
    exit 1
  fi
elif [ -z "$r1" ] ; then
  echo "No old revision <r1> specified"
  exit 1
elif [ -z "$r2" ] ; then
  echo "No new revision <r2> specified"
  exit 1
elif [ -z "$dir" ] ; then
  echo "No subdirectory specified"
  exit 1
elif [ -z "$filen" ] ; then
  echo "No file specified"
  exit 1
fi
# Echo settings
echo "CVSROOT      = $cvsroot"
echo "Old revision = $r1"
echo "New revision = $r2"
echo "dir          = $dir"
echo "file         = $filen"
# Make patch file
patchfile="${filen}-r${r1}-r${r2}.diff"
echo "Generating patch file: $patchfile"
if [ -f $patchfile ] ; then
  echo "WARNING output file already exists, deleting"
  /bin/rm -f $patchfile
fi
cvs -d $cvsroot rdiff -c -r$r1 -r$r2 ccp4/${dir}/$filen > $patchfile
# Update the index file
if [ -f index.patches ] ; then
  grep -v "# End of patch list" index.patches | grep -v "${patchfile}" > index.tmp
  echo "${patchfile} ${dir}" >> index.tmp
  echo "# End of patch list" >> index.tmp
  /bin/mv -f index.tmp index.patches
  echo "Updated index.patches file"
else
  # Index file not found
  echo "Add the following line to index.patches:"
  echo "${patchfile} ${dir}"
  echo "Fields MUST be separated by A SINGLE SPACE"
fi
exit 0
#


