#!/usr/bin/perl -T
use strict;
use warnings;
use RFC::Rig;
my $rig = RFC::Rig->new(shift,0,'nocheck');
$rig->check_run();
my $test = shift;
if ($test && $test =~ /(\d+)/)
{
    $test = $1;
}
else
{
    $test = 0;
}
$test = 0 if ($test < 0);
$test = $rig->session() if ($test > $rig->session());
my $dir = $rig->webdir($test);
my $file = $dir.'raadata';
die "No raadata file found for test $test! - Aborting\n" unless (-e $file);
my @dates = ();
my $cdate = '';
my $raadatafile;
open MYIN,'<',$file or die "Couyd not open file $file $!\n";
while (my $str = <MYIN>)
{
    next unless ($str);
    $str =~ /(\d+\:\d+\:\d+)/;
    my $date = $1;
    if ($cdate ne $date)
    {
	close MYOUT if ($raadatafile);
	$raadatafile = $dir.'raadata.'.$date;
	open MYOUT,'>',$raadatafile or die "Could not open file $raadatafile for writing $!\n";
	$cdate = $date;
	push @dates,$date;
    }
    print MYOUT $str;
}
close MYOUT;
close MYIN;
foreach my $d (@dates)
{
    my $cmd = '/usr/local/bin/celltest/cnv.pl '.$rig->name().' 0 recreate '.$test.' '.$d;
    print "Processing $d\n";
    system($cmd);
}
