#!/home/jc/bin/perl -dw
#
# Merge all the *.fmt files into the *.hdr files. This is a kludge to
# convert my dance medley pages to "pure ABC" so they'll be formatted
# correctly even if you don't download the fmt files.

open(D,"default.fmt") || die "$0: Can't read \"default.fmt\" [$!]\n";
@d = grep(/^\s*\w+\s+/,<D>);
foreach $d (@d) {$d =~ s/^\s*/%%/}

for $hdr (<[a-z]*.hdr>) {
	($nam = $hdr) =~ s/\.hdr$//;
	$fmt = $nam . '.fmt';
	if (open(F,"$fmt")) {	# Does dance have format file?
		@f = grep(!/^\s*$/,<F>);
		close F;
		foreach $f (@f) {$f =~ s/^\s*/%%/}
	} else {				# If not, use default.
		@f = @d;
	}
	if (open(H,"$hdr")) {	# Can we read the dance's header file?
		@h = <H>;
		close H;
	} else {
		print STDERR "$0: Can't read \"$hdr\" [$!]\n";
		next;
	}
	system "rm -f $hdr";
	if ($?) {
		print STDERR "$0: Got $? from \"rm $hdr\"\n";
		next
	}
	if (!open(O,">$hdr")) {
		print STDERR "$0: Can't write \"$hdr\" [$!]\n";
		next;
	}
	print O "% abc2ps format for US letter pages:\n";
	print O @f;
	print O @h;
	close O;
}
