#!/usr/bin/perl
      
@manifest               =`cat manifest.dat`;
$svg_process_file_list  = "";

$thisTest 		        = "";
$thisProfile 	        = "";
$thisOriginal 	        = "";
$thisSection 	        = "";
$thisLink 		        = "";
$thisComment 	        = "";

$dir_image_patches      = "../imagePatches";
$dir_png                = "../png";
$dir_svg                = "../svg";

#
# copyFile
#
#   sub routine to copy a PNG file from the 'imagePatches' to the 'png' directory.
#
sub copyFile
{
    system "cp \.\.\/imagePatches\/$thisTest.png \.\.\/png";
}


#
# splitManifestLine
#
sub splitManifestLine
{
    my @token = split /\t/, $test;
    $thisTest 		= $token[0];
    $thisProfile 	= $token[1];
    $thisOriginal 	= $token[2];
    $thisSection 	= $token[3];
    $thisLink 		= $token[4];
    $thisComment 	= $token[5];
    chop $thisComment;
}


#
# ----------------- Main processing --------------------------------------------
#

#
# Go through each to determine what to use for its reference image.
#
foreach $test (@manifest)
{
    splitManifestLine;
    print "Processing: $thisTest";
    
    my $image_patch_found = 0;
    my $file_path = "";
    #
    # Check for a PNG image in the imagesPatches directory. If there is
    # a PNG file in the images patches directory use that for the reference
    # image instead of generating the SVG file.
    #
    
    $file_path = $dir_image_patches."/".$thisTest."\.png";
    
    if (-e $file_path)
    {
        print " - Using PNG image patch\n";
        copyFile();
        $image_patch_found = 1;        
    }
    
    #
    # Check for an SVG file in the imagePatches directory. If there is
    # a file in the images patches directory use that for the reference
    # image instead of the SVG file in the 'svg' directory.
    #
    $file_path = $dir_image_patches."/".$thisTest."\.svg";
    
    if (-e $file_path)
    {
        #
        # If an image patch is already found, print an warning and continue.
        #
        if ($image_patch_found == 1)
        {
            print "-- WARNING test has a duplicate image patch --\n";
            next;
        }
        
        $image_patch_found = 1;
        
        #
        # Add the SVG file in the image patch diretory to the process list.
        #
        $svg_process_file_list = $svg_process_file_list." ".$dir_image_patches."/".$thisTest."\.svg";
        print " - Using SVG image patch\n";        
    }
    
    #
    # Add the SVG file in the 'svg' diretory to the process list.
    #
    if ($image_patch_found == 0)
    {
        $svg_process_file_list = $svg_process_file_list." ".$dir_svg."/".$thisTest."\.svg"; 
        print " - Using SVG file\n";
    }
}


#
# If there are any reference images to be generated then call batik.
#
if ($svg_process_file_list ne "")
{
    system "java -Xmx512m -jar ./batik-1.7/batik-rasterizer.jar -d $dir_png -w 480 -h 360 -snapshotTime 200$svg_process_file_list";
}

