#!/usr/bin/perl

my $mainDir = "VTK";
my $dir = $mainDir . "/lagrangian/defaultCloud";
my $prefix = "defaultCloud_";
my $postfix = ".vtk";
@mfiles = `ls "$mainDir"`;
@files = `ls "$dir"`;
my $maxIndex = -1;
my $nFiles = 0;

foreach my $file (@mfiles)
{
    if ($file =~ s/$postfix/$postfix/)
    {
        $nFiles++;
    }
}

foreach my $file (@files)
{

    my $length = length($file)-18;
    my $index = substr(substr($file, 13, -1), 0, $length);

    if ($index > $maxIndex)
    {
        $maxIndex = $index;
    }
}

print "max index = " . $maxIndex . "\n";
my $vtkInFile = $dir . "/" . $prefix . $maxIndex . $postfix;
my $line;
open(DATA, "<" . $vtkInFile);
$line = <DATA>;
$line = <DATA>;
close(DATA);
print $line . "\n";
for (my $i=1; $i<=$nFiles; $i++)
{
    my $vtkFile = $dir . "/" . $prefix . $i . $postfix;
    if (-e $vtkFile)
    {
#        print $vtkFile . " exists.\n";
    }
    else
    {
        open(FILE, ">" . $vtkFile);
        print FILE "# vtk DataFile Version 2.0\n";
        print FILE $line;
        print FILE "BINARY\n";
        print FILE "DATASET POLYDATA\n";
        print FILE "POINTS 0 float\n";
        close (FILE);
    }

}
