#!/usr/bin/perl
#$Id: refresh_info_file.pl,v 1.2 2026/03/04 07:45:24 sqko Exp $
use strict;
use warnings;
use RFC::Main;
use RFC::Rig;
use Sys::Hostname;
my @list = &RFC::Main::get_rigs(hostname);
@list = RFC::Main::get_rigs() unless (@list);
my @output = ();
foreach my $rig (@list)
{
    my $path = $RFC::Header::publicdir.'rig'.$rig.'/';
    my $tnrf = $path.'test.nr';
    open MYIN,'<',$tnrf or die "Could not open testnr file '$tnrf' $!\n";
    my $maxtest = <MYIN>;
    close MYIN;
    for (my $x = 0; $x <= $maxtest; $x++)
    {
	my $testnr = $rig.'test'.$x;
	my $info = $path.$testnr.'/info.txt';
	next unless (-e $info);
	open MYIN,'<',$info or die "Could not open info file '$info' $!\n";
	my @infolines = <MYIN>;
	my $start = undef;
	my $sample = '';
	my $alt = '';
	foreach my $str (@infolines)
	{
	    next unless $str =~ /Test started\s+([\w\s\.\-]+)/i;
	    $start = $1;
	    last;
	}
	foreach my $str (@infolines)
	{
	    next unless $str =~ /[Cell|Sample] number[\t\s]+\:\s+(.+)/i;
	    $sample = $1;
	    last;
	}
	foreach my $str (@infolines)
	{
	    next unless $str =~ /Alternate test numbers[\t\s]+\:\s+(.+)/i;
	    $alt = $1;
	    last;
	}
	next unless ($start);
	$start =~ s/[\s\n\r]+$//;
	$sample =~ s/[\s\n\r]+$// if ($sample);
	$alt =~ s/[\s\n\r]+$// if ($alt);
	my $str =  $start.'  '.$testnr.'  '.$sample.' '.$alt;
	push @output,$str;
    }
}
my $file = '/home/celltest/info_table.txt';
open MYOUT,'>',$file or die "Could not open file '$file' $!\n";
print MYOUT join("\n",@output),"\n";
close MYOUT;
