#!/usr/bin/perl

############################################################################
# Revision history
# v1.0 (September 2, 2015)
#    - Greg Sanders
# v2.0 (December 1, 2015)  Additional command-line information
#    - Greg Sanders
#
#    Developed using Perl v5.16.3
#
############################################################################
# This software was developed at the National Institute of Standards and Technology by
# employees and/or contractors of the Federal Government in the course of their official duties.
# Pursuant to Title 17 Section 105 of the United States Code this software is not subject to 
# copyright protection within the United States and is in the public domain.
#
# NIST assumes no responsibility whatsoever for its use by any party, and makes no guarantees,
# expressed or implied, about its quality, reliability, or any other characteristic.
# We would appreciate acknowledgement if the software is used.  This software can be
# redistributed and/or modified freely provided that any derivative works bear some notice
# that they are derived from it, and any modified versions bear some notice that they
# have been modified.
#
# THIS SOFTWARE IS PROVIDED "AS IS."  With regard to this software, NIST MAKES NO EXPRESS
# OR IMPLIED WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING MERCHANTABILITY,
# OR FITNESS FOR A PARTICULAR PURPOSE.
############################################################################

use strict;
use Getopt::Std;
use XML::LibXML;

## opt_c is the cluster number as a two-digit number (e.g., 03)
## opt_d is the test definition file path
##       (This path get expanded to:  opt_d . "/cluster" . opt_c . "_test_definition_file.xml" )
## opt_h is the hyp file path (for the directory with output files from ALL systems)
##   (the system and submission part of the path gets appended from opt_t and opt_u)
## opt_l is the channel letter(s)  (e.g., A, XA, or XMT)
## opt_r is the ref file path (for the answer key files)
## opt_s is the set of data (e.g., dev2 or progress -- match capitalization of the corresponding directory)
##       (the specific ref dir is:  opt_r . "/" .  opt_s . "_" . opt_l . "/cluster" . opt_c )
## opt_t is the team ID         (Note: Must match the submission directory name, before the underscore in it)
## opt_u is the submission ID   (  and must match the submission directory name, after the underscore in it)
 use vars qw($opt_c $opt_d $opt_h $opt_l $opt_r $opt_s $opt_t $opt_u $opt_v);
 getopts ('c:d:h:l:r:s:t:u:v');

 my $TeamSubmissionClusternum = $opt_t . "_" . $opt_u . "_" . $opt_c;     # This goes in the output .csv file

 my $outputPath;
 if ($opt_s eq "lsdd") {
     $outputPath = $opt_h . "/" . $opt_t . "_" . $opt_u . "/" . $opt_t . "_" . $opt_u . "_LSDDscores.csv";
 } else {
     $outputPath = $opt_h . "/" . $opt_t . "_" . $opt_u . "/" . $opt_t . "_" . $opt_u . "_scores.csv";
 }

 die "Error: Test-definition dir not specified\n" if not defined($opt_d);
 my $testDefPath = $opt_d . "/cluster" . $opt_c . "_test_definition_file.xml";
 open TEST_DEF, "<$testDefPath" or die "Error: Cannot open test definition file $testDefPath\n";
 close TEST_DEF;

 my $parser = XML::LibXML->new();
 my $testDef = $parser->parse_file($testDefPath);


   ## As a matter of Perl pain, the following print statement won't work (needs to be a printf)
   # print "Sample == [id: $sample->getAttribute('id')   file: $sample->getAttribute('file')\n";


## The following code assumes we are running this Perl program with
## the current working directory being the location of this program
## and of scoreFile_SAD.pl -- and we further assume that any relative path(s)
## that appear as the value of a file attribute of a SAMPLE element
## are relative to the current working directory.  The audio filename
## extension is hard-coded as .flac (hard assumption). 

for my $testSet ($testDef->findnodes('TestSet')) {
    my $testSetName = $testSet->getAttribute('id');

    for my $test ($testDef->findnodes('TestSet/TEST')) {
        my $testName = $test->getAttribute('id');

        for my $sample ($testDef->findnodes('TestSet/TEST/SAMPLE')) {
            my $audioFilenameWithPath = $sample->getAttribute('file');

            $audioFilenameWithPath =~ '^(.*)/([^/]+).flac$';
            my $path = $1;
            my $baseFilenameWithoutPath = $2;

            my $refFile = $opt_r . "/" . $opt_s . "_" . $opt_l . "/cluster" . $opt_c . "/" . $baseFilenameWithoutPath . "_0_annot.txt";
            my $hypFile = $opt_h . "/" . $opt_t . "_" . $opt_u . "/cluster" . $opt_c . "/" . $baseFilenameWithoutPath . ".txt";

                   print("scoreFile_SAD.pl -r $refFile -h $hypFile -n $baseFilenameWithoutPath -s 2 -e 3 -g 4 -t 5 -f 6 -u 7 -o $outputPath -p $TeamSubmissionClusternum -a $testSetName -b $testName\n");
            system ("perl scoreFile_SAD.pl -r $refFile -h $hypFile -n $baseFilenameWithoutPath -s 2 -e 3 -g 4 -t 5 -f 6 -u 7 -o $outputPath -p $TeamSubmissionClusternum -a $testSetName -b $testName");
        }
    }
}

