#!/usr/bin/perl
#
# Kludge to add "Z:Allan Garvin" line to all tunes. If a tune already
# has  a  Z line, we don't add anything.  This is a one-off script to
# handle Allan's modest omission of credit for his work.  If we  find
# another case like this, we might parameterize the name.

require "Backup.pm";

for $f (@ARGV) {
	print "$f\n";
	if (open(F,$f)) {
		&Backup($f);
		if (open(G,">$f")) {
			$gotZ = 0;
			for $l (<F>) {
				if ($l =~ /^X:/) {
					$gotZ = 0;	# Reset at new tune
				} elsif ($l =~ /^Z:/) {
					++$gotZ;	# Don't add to existing Z: line
				} elsif ($l =~ /^K:/) {
					print G "Z:Allan Garvin\n" unless $gotZ;
				}
				print G $l;
			}
			close G;
		} else {
			print STDERR "Can't write \"$f\" [$!]\n";
		}
		close F;
	} else {
		print STDERR "Can't read \"$f\" [$!]\n";
	}
}
