#!/usr/bin/perl -dw
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  renametunekey -
#
#SYNOPSIS
#  renametunekey [file]..
#
#REQUIRES
	push @INC, $ENV{HOME}, "$ENV{HOME}/sh", "$ENV{$HOME}/pm";
	require "Backup.pm";
#
#DESCRIPTION
#  This silly script was written to fix the problems on Macs caused by  using
#  European-style  lower-case  letters  for  minor keys.  We look through the
#  files for an '_' followed by a letter a-g, and rewrite the  file  name  to
#  have Am-Gm instead.
#
#  If there's already a file with the new name, we back it up, in case it's a
#  different file.
#
#OPTIONS
#
#EXAMPLES
#
#FILES
#
#BUGS
#  We don't deal with flats or sharps.  This probably won't be fixed, as this
#  was  a  one-time task that seems to be (nearly) done now.  But this script
#  may still be around, due to the difficulty  of  purging  things  that  are
#  mirrored on several web servers (not to mention online archives).
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu> 2004
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 1;	# Verbose level.
%sub = (
	'a' => 'Am',
	'b' => 'Bm',
	'c' => 'Cm',
	'd' => 'Dm',
	'e' => 'Em',
	'f' => 'Fm',
	'g' => 'Gm',
),

$ARGV = '.' unless $ARGV;

for $name (@ARGV) {
	$name =~ s/^\.\/+//;
	if (($base,$key) = ($name =~ /^(.*)_([a-g])\.abc/)) {
		print "$P: '$key' \"$base\"\n" if $V>2;
		&onename($base,$key)
	} else {
		print "$P: OK: \"$name\"\n" if $V>3;
	}
}

print "$P: Exit with status $exitstat.\n" if $V>3;
exit $exitstat;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub onename {
	local($path,$k) = @_;
	local($old,$new);
	$old = $path . '_' .      $k  . '.abc';
	$new = $path . '_' . $sub{$k} . '.abc';
	if (-f $old) {
		print "$P: old \"$old\"\n" if $V>1;
		if (-f $new) {
			print "$P: new \"$new\" exists.\n" if $V>1;
			&Backup($new);
		} else {
			print "$P: new \"$new\" doesn't exist.\n" if $V>3;
		}
		rename($old,$new);
	} else {
		print "$P: ### \"$old\" doesn't exist!\n" if $V>0;
	}
	system("ls -il $path*") if $V>1;
}
