#!/usr/bin/perl
#
# abc+3 [file]..
#
# Read an abc file (or passage from stdin), and write output
# that has the same melody plus a harmony a third up.
#
# Options:

$| = 1;
@S = (
"C,","D,","E,","F,","G,","A,","B,",
"C", "D", "E", "F", "G", "A", "B", 
"c", "d", "e", "f", "g", "a", "b", 
"c'","d'","e'","f'","g'","a'","b'",
);
for ($i=0; $i<@S; $i++) {
	$I{$S[$i]} = $i;
}

$N = '[\^=_]*[A-Ga-g][\',]*';	# Note.
$L = '[/\d.\<>]*';				# Length.

line:
for $line (<>) {
	while ($line) {
		if ($line =~ s/^(\s+)//) {print $1}
		if (($hdr,$sp) = ($line =~ /^([A-Z]:)(\s*)/)) {
			print $line;
			next line;
		} elsif ($line =~ s'^("[^"]*"\s*)'') {print $1; next;
		} elsif ($line =~ s'^(\(\d*\s*)'')   {print $1; next;
		} elsif ($line =~ s'^(\)\s*)'')      {print $1; next;
		} elsif ($line =~ s'^(\[\|\s*)'')    {print $1; next;
		} elsif ($line =~ s'^(\|\]*\s*)'')   {print $1; next;
		} elsif ($line =~ s'^(\[.*\]\s*)'')  {print $1; next;
		} elsif ($line =~ s/^($N)($L)(\s*)//) {
			 $n = $1;  $l = $2; $s = $3;
			 $i = $I{$n};
			 $j = $i + 2;
			 if ($h = $S[$j]) {
				 if ($l =~ '^\d') {
				 	print "[$n$l$h]$s";
				 } else {
				 	print "[$n$h]$l$s";
				 }
				 next;
			 }
		}
		if ($line =~ s'^(.)'')          {print $1; next;
		}
		print "Left: \"$line\"\n" if $D>1;
	}
}
