#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  tunecheck - Check transcribedtunes for some common problems
#
#SYNOPSIS
#  tunecheck [file]..
#REQUIRES
#
#DESCRIPTION
#
#OPTIONS
# None yet, but there may be some soon.
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

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

$MM_Bcheck = 0;	# Special check for Kerr's Merry Melodies

arg:
for $arg (@ARGV) {
	print "- - - - - - - - - - $arg:\n" if $V>0;
	$fnum = $fnam = $fver = '';
	unless (open(F,$arg)) {
		print "### Can't read \"$arg\" [$?]\n";
		next arg;
	}
	if (($fnum, $fnam) = ($arg =~ /^(\d+)[-=_](.*)\.(\w*)$/)) {
		print "\tfnum='$fnum' fnam='$fnam'\n" if $V>1;
		if ($fnam =~ /^(.*)-(V\d)$/) {
			$fnam = $1;
			$fver = $2;
			print "\tfnum='$fnum' fnam='$fnam' fver='$fver'\n" if $V>1;
		}
	} else {
		print "+++ Can't parse file name '$arg'\n" if $V>0;
	}	
	print "Read the file ...\n" if $V>3;
line:
	while ($line = <F>) {
		$line =~ s/[\r\s]*$//;
		print "Line: $line\n" if $V>3;
		next line if ($line =~ /^\s*%/);	# Ignore comments
		if (($hdr,$val) = ($line =~ /^([A-Za-z]):\s*(.*)/)) {
			if ($hdr eq 'X') {
				print "X: '$val'\n" if $V>2;
				if (($v,$pp,$s,$nn) = ($val =~ /^(\d)(\d\d)(\d)(\d\d)/)) {
					print "\tv=$v pp=$pp s=$s nn=$nn\n" if $V>1;
					if ($fnum ne "$v$pp$s$nn") {
						print "### File number '$fnum' and X: '$val' don't agree.\n" if $V>0
					} else {
						print "\tIndex checked.\n" if $V>2;
					}
				} else {
					print "+++ Can't parse index '$val'\n" if $V>1;
				}
			} elsif ($hdr eq 'T') {
				print "T: '$val'\n" if $V>2;
				($fttl = $fnam) =~ s/_/ /g;		# fttl is the file's title without '_'
				($tttl = $val) =~ s/['.,()]//g;	# tttl is the tune's title without punctuation
				$fttl =~ s/\s+/ /g;				# Strip out redundant spaces
				$tttl =~ s/\s+/ /g;
				if (lc($tttl) ne lc($fttl)) {	
					print "### File title \"$tttl\" and T \"$fttl\" disagree.\n" if $V>0;
				} else {
					print "\tTitle checked.\n" if $V>2;
				}
			} elsif ($hdr eq 'B' && $MM_Bcheck) {	# Special check for Kerr's Merry Melodies B: info
				print "B: '$val'\n" if $V>2;
				if (($Bv,$Bp,$Bs,$Bn) = ($val =~ / v\.(\d+) p\.(\d+) s\.(\d+) #(\d+)/)) {
					print "### B: v=$Bv and $v differ in $arg\n"  if $V>0 && (int($Bv) ne int($v));
					print "### B: p=$Bp and $pp differ in $arg\n" if $V>0 && (int($Bp) ne int($pp));
					print "### B: s=$Bs and $s differ in $arg\n"  if $V>0 && (int($Bs) ne int($s));
					print "### B: n=$Bn and $nn differ in $arg\n" if $V>0 && (int($Bn) ne int($nn));
				} else {
					print "+++ Can't parse B: \"$val\"\n" if $V>0;
				}
			} elsif ($hdr eq 'K') {
				print "K: '$val'\n" if $V>2;
				next arg;				# No checking inside tune yet
			} else {
				print "--- Ignore '$line'\n" if $V>3;
			}
		} else {
			print "Line '$line' not matched.\n" if $V>2;
		}
	}
}

exit $exitstat;
