#!/bin/sh

usage="$0 [-h | --help] [ -f | --filelist] [directory]\n  Recursively searches for CVS directories and finds the newest entry"

listfiles=false;
while [ $# -gt 0 ] ; do
	case "$1" in
		-h | --help) printf "$usage"; exit 2;;
		-f | --filelist) listfiles=true; shift;;
		*) break;;
	esac;
done;

dir="$1";
if [ -z "$dir" ] ; then
	dir=".";
fi;

tmpfile=/tmp/dates.cvsCheckoutDate;
rm -f "$tmpfile";
find $dir -name "CVS" -type d -exec cut -f 4 -d / \{\}/Entries \; | grep '^... ... .. ..:..:..' > "$tmpfile";

if $listfiles ; then
	d1=`date -f "$tmpfile" "+%F %T" | sort -r | head -n 1`;
	d2=`date -d "$d1" "+%b %e %T %Y"`;
	find $dir -name "CVS" -type d -exec grep -H "$d2" \{\}/Entries \;
else
	date -f "$tmpfile" "+%F %T" | sort -r | head -n 1;
fi;
