#! /usr/bin/perl -w

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

LOOP: while (1) {
    if (!defined ($ARGV[$index])) {
	last LOOP;
    }
    $_ = $ARGV[$index];
    if (/(2\.1\.[0-9])/) {
	$revision=$1;
    }
    elsif (/^-debug$/) {
	$debug=1;
    }
    elsif (/^-diff$/) {
	$diff=1;
    }
    else {
	die "Unknown option '$_'\n" . 
	    "usage: check_changes.pl [ version ] [ -diff ] [ -debug ]";
    }
    $index++;
}
$r = $revision;
$r =~ s/\./_/g;
$fli4l_tag="fli4l-" . $r;

print "Checking for changes since release $revision (tag $fli4l_tag)\n";

open (LOG, "cvs -q status -v|") 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/fli4l/##g;
	    print "$file_name (new)\n";
	}
	$file_state = $1;
	print STDERR "\$file_state = $1\n" if $debug;
    }
    if (/Repository revision:\s+((\d|\.)+)\s+(\S+),v.*/) {
	$rep_revision = $1;
	$file_name = $3;
	print STDERR "\$rep_revision = $rep_revision" . 
	    " \$file_name = $file_name\n" if $debug;
    }
    if (/$fli4l_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/fli4l/##g;
	    if ($file_state ne "Up-to-date") {
		print "$file_name (warning: state $file_state)\n";
	    }
	    else {
		if ($diff) {
		    open (DIFF, "cvs diff -r $fli4l_tag $file_name|");
		    while (<DIFF>) {
			print;
		    }
		    close DIFF;
		}
		else {
		    print "$file_name\n";
		}
	    }
	}
	undef $file_name;
    }
}
