#!/usr/bin/perl -w
#
#    Counts open and close braces in all the RTF files that reside
#    in the directory that the script is run in.  
#    Sept 2001, Scott Prahl

use strict;

my $level;
my $bad=0;
my $good = 0;
my @files = glob("*.rtf");

for (@files) {
	my $file = $_;
	
	open IN, $_ or die "could not open $_";
	$level = 0;
	while (<IN>){
		my $before = $level;
		s/(\\\\)+//g;
		s/\\}//g;
		s/\\{//g;
		$level += tr/{/{/;
		$level -= tr/}/}/;
#		print "braces becomes $level after line $.\n" if ($level<=0 && $before!=$level);
	}
	close IN;
	if ($level != -3) {$bad++} else {$good++}
	print "brace level = $level for $file\n" if $level != -3;
}

print "Braces are ok in $good/", $good+$bad, " files\n";
