#!/usr/bin/perl
# $Id: jdata.pl,v 1.7 2023/10/06 07:20:52 sqko Exp $
use strict;
use warnings;
use RFC::Header;
use RFC::Rig;
use SemaforeFile;
my $rig = RFC::Rig->new(shift);
$rig->check_run;

my $testnr = $rig->session;
my $redo = 1;
my $julian = 0;
for (my $x = 0; $x < scalar(@ARGV);$x++)
{
    my $string = $ARGV[$x];
    $julian = 1 if ($string =~ /julian/);
    $redo = 0 if ($string =~ /raadata/);
}

if (@ARGV)
{
    $testnr = shift;
}


my @list = ();
my $homedir = $RFC::Header::publicdir.'rig'.$rig->name.'/'.$rig->name.'test'.$testnr.'/';
if ($redo)
{
    opendir(IND,$homedir) || die "Could not open directory $homedir !$\n";
    while (my $f = readdir(IND))
    {
	next unless ($f =~ /raadata[\.\_]\d/);
	push @list,$f;
    }
    closedir(IND);
    my $raadata = SemaforeFile->new($homedir.'/raadata',$rig->riglock);
    $raadata->open_write;
    my $fh = $raadata->fh;
    foreach my $file (sort @list)
    {
	print $file,"\n";
	my $f = SemaforeFile->new($homedir.'/'.$file);
	$f->open_read;
	while (my $str = $f->readline)
	{
	    chomp $str;
	    print $fh $str,"\n";
	}
	$f->close;
    }
    $raadata->close;
}


my $dat  = `date +"%Y:%m:%d"`;
chomp($dat);


my $cstr='/usr/bin/awk -f '.$libdir.'awk/utime.awk '.$homedir.'raadata   | /usr/bin/awk -f '.$libdir.'awk/coulomb.awk  > '.$homedir.'jdata ';
$cstr='/usr/bin/awk -f '.$libdir.'awk/julian.awk '.$homedir.'raadata   | /usr/bin/awk -f '.$libdir.'awk/coulomb.awk  > '.$homedir.'jdata ' if ($julian);

system($cstr);

my $rignr = $rig->name;
$cstr = "/usr/local/bin/celltest/create_data_export.pl $rignr > /dev/null 2> /dev/null";
system($cstr);
