#!/usr/bin/perl
use strict;
use warnings;
use RFC::Rig;
use RFC::Header;
use RFC::Main;
use File::Copy;
use Cwd qw(getcwd);
my $cwd = getcwd();
my $r = shift;
my $rig = RFC::Rig->new($r,0,'nocheck');
$rig->check_run;
my $max = $rig->session();
my $web = $publicdir.'rig'.$rig->name.'/';
if (@ARGV)
{
    my $x = shift;
    my $test = $rig->name.'test'.$x;
    my $tarfile = $web.$test .'/'.$test.'.tgz';
    if (-e $web.'/'.$test)
    {
	&create_tgz($web,$test);
    }
    exit;
}

for (my $x = 0; $x < $max; $x++)
{
    my $test = $rig->name.'test'.$x;
    my $tarfile = $web.$test .'/'.$test.'.tgz';
    if (-e $web.'/'.$test && !-e $tarfile)
    {
	&create_tgz($web,$test);
    }
}
my $currtest = $rig->name.'test'.$max;
&create_tgz($web,$currtest);
chdir $cwd;

sub create_tgz($$)
{
    my $dir = shift;
    my $test = shift;
    chdir $dir;
    my $cmd = "/usr/bin/tar --ignore-failed-read -cvzf $test.tgz $test/jdata $test/iv/ivdatadir-*/ivdata* $test/iv/ivtable $test/iv/ivtable-avg $test/impedance/*.i2b $test/impedance/raw/*.i2b $test/impedance/imp_jdata $test/impedance/imp_data $test/info.txt $test/proglog $test/Fast* $test/fast*";
    system($cmd);
    move($dir.'/'.$test.'.tgz',$dir.'/'.$test.'/.');
}
