#! /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.description.generator.run_checker.pl run_files*

$num_args = $#ARGV+1;

if ($num_args < 1) {
    print "\nUsage: vtt.description.generator.run_checker.pl run_files*\n";
    exit;
}

$minVideoID = 1;
$maxVideoID = 1880;


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 = ();
    while ($line = <RUN>)
    {
	chomp $line;
	(undef, $subVideo, $subTxt) = split /\s+/, " $line";
	
	if ($subVideo eq "" && $subTxt eq "")
	{#empty line
	    next;
	}
	$lineCount++;

	if ($subVideo < $minVideoID || $subVideo > $maxVideoID)
	{#invalid video ID
	    $str = "Error: invalid video $subVideo at line $lineCount of run $file";
	    printf ERR "%s\n", $str;
	    $errors++;
	}
	elsif ($subTxt eq "")
	{
	    $str = "Error: video $subVideo at line $lineCount of run $file has empty textual description!";
	    printf ERR "%s\n", $str;
	    $errors++;
	}

	else
	{
	    $results[$subVideo]++;
	    if ($results[$subVideo] > 1)
	    {#multiple submissions detected for the same video URL                                                                    
		$str = "Error: video $subVideo has multiple submissions (line $lineCount) of run $file";
		printf ERR "%s\n", $str;
		$errors++;
            }
	    

	}
	
    }
    close RUN;

    for ($x = $minVideoID; $x <= $maxVideoID; $x++)
    {
	if ($results[$x] < 1)
	{#missing video URL
	    $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;
}
