#! /usr/bin/perl -w

$file_state = "";
$rep_revision = "";
$tag_revision = "";
$debug=0;
$diff=0;
$index = 0;

LOOP: while (1) {
    if (!defined ($ARGV[$index])) {
	last LOOP;
    }
    $_ = $ARGV[$index];
    if ($index == 0) {
	$path=$ARGV[$index];
    }
    elsif($index == 1) {
    	$tag=$ARGV[$index];
    }
    elsif (/^-debug$/) {
	$debug=1;
    }
    elsif (/^-diff$/) {
	$diff=1;
    }
    else {
	die "Unknown option '$_'\n" . 
	    "usage: check_changes.pl [ path ] [ tag ] [ -diff ] [ -debug ]";
    }
    $index++;
}

$path || die "You must specify the path\n";
$tag || die "You must specify the tag\n";

print "Checking for changes since tag $tag in $path\n";

open (LOG, "cvs -q status -v $path|") or die "Can't execute cvs status\n";
while (<LOG>) {
    print STDERR if $debug;
    if (/^File.*Status: ([a-zA-Z-]+).*/) {
    	if (defined $file_name) {
	    $file_name =~ s#(/home)?/cvs/eis/##g;
	    print "$file_name (new)\n";
	}
	$file_state = $1;
	print STDERR "\n\$file_state = $1\n" if $debug;
    }
    
    if (/Repository revision:\s+((\d|\.)+)\s+(\S+),v.*/) {
	$rep_revision = $1;
	$file_name = $3;
	print STDERR "\n\$rep_revision = $rep_revision\n" . 
	    "\$file_name = $file_name\n" if $debug;
    }
    
    if (/$tag.*revision:\s+((\d|\.)+)/) {
	$tag_revision = $1;
	print STDERR "file: $file_name, rep: " . 
	    "$rep_revision, tag: $tag_revision\n" if $debug;
	    
	if ($tag_revision ne $rep_revision) {
	    $file_name =~ s#(/home)?/cvs/eis/##g;
	    
	    if ($file_state ne "Up-to-date") {
		print "$file_name (warning: state $file_state)\n";
	    }
	    else {
		if ($diff) {
		    open (DIFF, "cvs diff -r $tag $file_name|");
		    while (<DIFF>) {
			print;
		    }
		    close DIFF;
		}
		else {
		    print "$file_name\n";
		}
	    }
	}
	undef $file_name;
    }
}
