#!/usr/bin/perl
#
#   dbglvls file...
#
# This program wanders thru the .b source files looking for  lines  of
# the forms:
#    :x		Dy(...);
#    :x		Py(...);
# where  x and y differ.  If it finds any, it replaces y with x.  This
# requires a total rewrite of  the  file,  of  course.   We  take  the
# space-hog  attitude  of  slurping  up each file in its entirety, and
# modifying it in memory.  If any of the modifications work, we  write
# the file back to disk. Note that we only look for x and y that are a
# single digit; things like :f or :i aren't affected by this program.

$pat = '^:(\d)(\s+)([DP])(\d)';

for $f (@ARGV) {
	if (open(F,"<$f")) {
		@f = <F>;	# Read the entire file.
		close(F);
		$changes = 0;
		foreach $line (@f) {
			if ($line =~ /$pat/) {
				if ($1 ne $4) {
					$line =~ s/$pat/:$1$2$3$1/;
					print "$f: $line";
					$changes ++;
				}
			}
		}
		if ($changes) {
			if (open(F,">$f")) {
				print F @f;
				close(F);
			} else {
				print STDERR "### Can't write \"$f\" [$!]\n";
			}
		}
	} else {
		print STDERR "### Can't read \"$f\" [$!]\n";
	}

}
