#!/usr/bin/perl
#
# Programmer:    Craig Stuart Sapp <craig.stanford.edu>
# Creation Date: Tue Feb  3 12:58:36 PST 2009
# Last Modified: Tue Feb  3 12:58:39 PST 2009
# Filename:      ...auto/match/bin/copyinputdata
# Syntax:        perl 5
#
# Description:   Copy all manually corrected tapping data from
#		 any mazurka recordings which have it.
#

use strict;

my %mazurkas = getMazurkaList("/project/mazurka/website/info/discography/mazurka-discography.txt");

my @pids = keys %mazurkas;
my $pid;
foreach $pid (@pids) {
   print "checking $pid...\n";
   copyInputFiles($pid, $mazurkas{$pid});
}


exit(0);

###########################################################################


##############################
##
## copyInputFiles --
##

sub copyInputFiles {
   my ($pid, $mazurka) = @_;
   my $conddir = "/project/mazurka/website/info/revcond";

   if (!-r "$conddir/$pid/$pid-clean.txt") {
      # print "$pid does not have a companion -clean.txt file\n";
      # There is no manually corrected data for it, so skip it
      next;
   }

   if (!-r "$conddir/$pid/$pid.score") {
      print "WARNING: $pid ($mazurkas{$pid}) does not have a score\n";
      # Can't continue because of some accounting problem that needs
      # to be fixed.
      next;
   }

   if (!-r "$mazurka") {
       mkdir("$mazurka", 0755);
   }
   if (!-r "$mazurka/$pid") {
       mkdir("$mazurka/$pid", 0755);
   }

   `cp $conddir/$pid/$pid-clean.txt $mazurka/$pid/`;
   `cp $conddir/$pid/$pid.score $mazurka/$pid/`;
   `hum2mid -CT0 $conddir/$pid/$pid.score -o $mazurka/$pid/$pid.mid`;

   # copy over the score align results and difference files for 
   # comparative analysis:
   if (-r "../scorealign/$mazurka/$pid-sa.txt") {
      `cp ../scorealign/$mazurka/$pid-sa.txt $mazurka/$pid/`;
   }

}



##############################
##
## getMazurkaList --
##

sub getMazurkaList {
   my ($file) = @_;
   my $line = "XXXX";
print line;

   open (FILE, $file) or die;

   my %output;
   my $maz;
   my $pid;
   my @fields;
   while ($line = <FILE>) {
print line;
      chomp $line;
      @fields = split(/\t/, $line);
      next if @fields < 8;
      $pid = $fields[7];
      $maz = $fields[0];
      next if $maz =~ /opus/;
      $maz =~ s/\./-/;
      if ($maz =~ /^.-/) {
         $maz = "0$maz";
      }
      $maz = "mazurka$maz";
      $pid = "pid$pid";
      $output{$pid} = $maz;
   }
   close FILE;

   return %output;
}
