#
#  Strip out error code, given message.
#
#

$sum = 0;

# hardwire it in
LOOP: while(<>) {
	$text = $_;

        /ERROR/ || goto LOOP;
        $text = $_;
        (@list) = split(/:/);

        $file = shift(@list) || die "no file!\n";
        $line = shift(@list) || die "no line!\n";
        $fn = shift(@list) || die "no function!\n";
        $err = shift(@list) || die "no error!\n";
        $err = shift(@list) || die "no error!\n";
        $start = shift(@list) || die "no start lineno!\n";
        $end = shift(@list) || die "no end lineno\n";
	
	# print "file = <$file>, start = $start, end = $end\n";
	open(FILE, $file) || die "cannot open $_";

	# should maybe emit the variable name as well, and some statistics
	# about whether they are in a global, structure, etc.
	if(!$start || !$end) {
	} elsif($start > $end) {
		$t = $end;
		$end = $start;
		$start = $t;
		$start_str = "Error --->";
		$end_str = "Start --->";
	} else {
		$start_str = "Start --->";
		$end_str = "Error --->";
	}
	# print "file = <$file>, start = $start, end = $end\n";
	

	# skip forward: has to be a better way.
	for($i = 1; $i < ($start-2); $i++) {
		$l = <FILE>;
	}
	
	print "---------------------------------------------------------\n";
	print "X [BUG]\n";
	print "X [FALSE]\n";
	print "X [BROKE]\n";
	print "$text\n";

	if(!$start || !$end) {
		print "SKIPPING\n";
		goto END ;
	} 

	$start_emit = (($end - $start) > 10) ? ($i + 8) : ($end+1);

	if($start == $end) {
		$start_emit -= 10;
	}

	#  now print out the lines.
	for(; $i <= $start_emit; $i++) {
		print "$start_str\n" if($i == $start);
		$l = <FILE>;
		print "$end_str\n" if($i == $end);
		print $l;
	}

	if($i <= $end) {
		for(; $i < ($end - 8); $i++) {
			$l = <FILE>;
		}

		$deleted = $i - $start_emit;
		print "\n\t... DELETED $deleted lines ...\n\n";

		#  now print out the lines.
		for(; $i <= ($end+4); $i++) {
			$l = <FILE>;
			print "$end_str\n" if($i == $end);
			print $l;
		}
	}
	
END:
	$n++;
	$sum += ($end - $start);
	close(FILE);
}

$mean = $n ? ($sum / $n) : 0;
print "$n errors in all, average distance = $mean\n";
