#! /usr/bin/perl

# This software was produced by NIST, an agency of the U.S. government, 
# and by statute is not subject to copyright in the United States. Recipients 
# of this software assume all responsibilities associated with its operation, 
# modification and maintenance.

# Usage : vtt.matching.ranking.run_checker.pl testing_subset[2:5] run_files*

$num_args = $#ARGV+1;

if ($num_args < 2) {
    print "\nUsage: vtt.matching.ranking.run_checker.pl testing_subset[2:5] run_files*\n";
    exit;
}

$testing_subset = $ARGV[0];
open URL_LIST, "< tv17.vtt.url.list.set$testing_subset" or die "Can't open URL list file $!";

shift @ARGV;

if ($testing_subset == 2)
{
    $minRank = 1;
    $maxRank = 1613;
    $minVideoID = 1;
    $maxVideoID = 1880;
    $minTxtID = 1;
    $maxTxtID = 1613;
}
elsif ($testing_subset == 3)
{
    $minRank = 1;
    $maxRank = 795;
    $minVideoID = 1;
    $maxVideoID = 1878;
    $minTxtID = 1;
    $maxTxtID = 795;
}
elsif ($testing_subset == 4)
{
    $minRank = 1;
    $maxRank = 388;
    $minVideoID = 4;
    $maxVideoID = 1877;
    $minTxtID = 1;
    $maxTxtID = 388;
}
elsif ($testing_subset == 5)
{
    $minRank = 1;
    $maxRank = 159;
    $minVideoID = 21;
    $maxVideoID = 1877;
    $minTxtID = 1;
    $maxTxtID = 159;
}

#############################################################
# load URL_ID list
while ($line = <URL_LIST>)
{
    ($URL_ID, undef) = split /\s+/,$line;
    $video_list{$URL_ID} = 1;
}
#############################################################

while ($file = <@ARGV>)
{
    open RUN, "< $file" or die "Can't open file $!";
    open ERR, "> $file.err.log" or die "Can't open file $!";

    $errors = 0;
    %results = ();
    @topics = ();
    while ($line = <RUN>)
    {
	chomp $line;
	(undef, $rank, $subVideo, $subTxt) = split /\s+/, " $line";
	if ($rank eq "" && $subVideo eq "" && $subTxt eq "")
	{#empty line
	    next;
	}
	$lineCount++;

	if ($rank < $minRank || $rank > $maxRank)
	{#invalid rank
	    $str = "Error: invalid rank $rank at line $lineCount of run $file";
	    printf ERR "%s\n", $str;
	    $errors++;
	}
	elsif ($subVideo < $minVideoID || $subVideo > $maxVideoID)
	{#invalid video ID
	    $str = "Error: out of range video ID $subVideo at line $lineCount of run $file";
	    printf ERR "%s\n", $str;
	    $errors++;
	}
	elsif ($subTxt < $minTxtID || $subTxt > $maxTxtID)
	{#invalid textual description ID
	    $str = "Error: invalid txt description ID $subTxt at line $lineCount of run $file";
	    printf ERR "%s\n", $str;
	    $errors++;
	}
	elsif (not exists ($video_list{$subVideo}))
	{
	    $str = "Error: video ID $subVideo is incorrect in run $file";
	    printf ERR "%s\n", $str;
	    $errors++;
	}
	else
	{
	    $results{$subVideo}{$rank}++;
	    $topics{$subVideo} = 1;
	    if ($results{$subVideo}{$rank} > 1)
	    {#duplicate rank detected for the same video                                                                     
		$str = "Error: video $subVideo has duplicate ranking at rank $rank (line $lineCount) of run $file";
		printf ERR "%s\n", $str;
		$errors++;
            }
	    
	    

	}
	
    }
    close RUN;

    foreach $vid_id (sort keys %video_list)
    {
	if (not exists ($topics{$subVideo}))
	{
	    $str = "Error: video ID $x is missing from run $file";
	    printf ERR "%s\n",$str;
	    $errors++;
	}
    }
   
    if ($errors > 0)
    {
	$str = "Run $file had $errors errors!. Please correct those errors and recheck your run.";
	printf ERR "%s\n", $str;
    }
    else
    {
	$str = "Run $file has successfully passed the VTT run check.";
	printf ERR "%s\n", $str;
    }
    close ERR;
}
