#!/bin/sh -e

module=${1-hurd}
dir=${2-/afs/sipb.mit.edu/project/hurddev/roland}

CVSROOT=/afs/sipb.mit.edu/project/hurddev/cvsroot
export CVSROOT


yrptn='[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
ptn="${module}-${yrptn}"
dptn="${ptn}-${yrptn}.diff"

date=`date +%Y%m%d`

set dummy `cd $dir; ls $ptn.tar.gz $dptn $dptn.gz 2>/dev/null |
		    sed -e "s/^.*\\(${yrptn}\\)[.a-z]*$/\\1/" |
		    sort -nr | head -2`
lastsnap="$2"
prevsnap="$3"

lastdiff=`cd $dir; ls $dptn $dptn.gz 2>/dev/null |
		    sed -e "s/^.*\\(${yrptn}\\)[.a-z]*$/\\1/" |
		    sort -nr | head -1`

notag=

if [ "x$lastsnap" = "x$date" ]; then
  if [ "x$lastdiff" = "x$lastsnap" ]; then
    echo "$0: Snapshot ${module}-${date} already made, skipping."
    exit 0
  elif [ "x$lastdiff" = x ]; then
    # There is no old diff, so use the previous snapshot.
    lastdiff="$prevsnap"
  fi

  # There is already a full snapshot for this date, but no diffs.
  # Don't re-tag, just make diffs.
  notag=yes
  lastsnap=$lastdiff
fi

if [ "x$lastdiff" = x ]; then
  echo >&2 "$0: nothing to diff against!"
  exit 1
fi

diff="${module}-${lastdiff}-${date}.diff"

cvs -q rdiff -s -r $module-$lastdiff $module > ${diff}-summary
if [ -s ${diff}-summary ]; then
  rm ${diff}-summary
else
  # No changes since last snapshot.
  exit 0
fi

test -z "$notag" && cvs -q rtag -F $module-$date $module
cvs -q rdiff -u -r $module-$lastdiff -r $module-$date $module |
  gzip -9c > $diff.gz
