#!/usr/bin/perl -w
#
#NAME
#  abcunbang - Strip out trailing '!' chars from ABC files
#
#SYNOPSIS
#  abcunbang [file]..
#
#DESCRIPTION
#  The use of these trailing '!' chars comes from the abcwin program.
#  It's  not  standard, and hardly any other ABC software understands
#  it.  And some other programs use '!' for a different  purpose,  to
#  set off annotations.
#
#  If you simply remove the '!' chars, it usually fixes problems.
#
#OPTIONS
#
#EXAMPLES
#
#FILES
#
#BUGS
#   We read the entire file in and, if it is changed, rewrite  it  in
#   place. This makes it work for multiply-linked files, but produces
#   a  window  during  which  a  crash  may  leave  the  file  in  an
#   indeterminate state.
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>  Jan 2005

$| = 1;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || 1;

file:
	for $file (@ARGV) {
		++$files;
		unless (open(F,$file)) {
			print STDERR "$0: Can't read \"$file\" ($!)\n";
			next file;
		}
		@data = <F>;	# Slurp up the file's contents.
		$changes = 0;	# Count the changed lines.
		for $line (@data) {
			if ($line =~ s"\|!\s$"|\n") {
				++$changes;
			}
		}
		if ($changes) {
			if (open(F,">$file")) {
				print F @data;
				print STDERR "Changed $file\n" if $V>1;
			} else {
				print STDERR "$0: Can't write \"$file\" ($!)\n";
			}
		}
		close F;
	}

unless ($files) {
	print STDERR "$0: No files on command line.\n" if $V>1;
}
