#!/usr/bin/perl -T
# $Id: list_tests.cgi,v 1.18 2024/11/18 11:29:30 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2011              ###
###                                                      ###
############################################################
############################################################
use strict;
use warnings;
use RFC::Main;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;
use SocketClient;
use CGI qw(:standard :netscape);
use Sys::Hostname;
use SemaforeFile;
my $ref = &RFC::RFCCGI::get_user_data_ref('nocheck');
my $uid = $$ref{'uid'};
my $ssl = CGI::https();
my $hostname = hostname();
my $default_rows = 10;
if (!defined(param))
{
    print process('to_login.tt');
}
else
{
    if ($uid)
    {
        my $url = get_cv('passwds','passwd_server_url');
        my %data = (
            'https' => $ssl,
            'pwdurl' => $url
            );
        print process('main_nav.tt',&get_template_data_ref(\%data));
        $_ = param('action');
      CASE:
        {
            ### Insert case statements ###
            &main_page;
        }
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}
exit;


sub main_page()
{
    my $allow = &RFC::RFCCGI::rig_data_access_allow();
    my @authids = &get_cv('global','data_access_authorisationid');
    my $doc_root = '/home/http/html/';
    my $rig = RFC::Rig->new(param('rig'),0,'nocheck');
    my $test = $rig->session();
    my @list = ();
    my $offset = param('offset');
    my $init = 0;
    $init = 1 unless (defined($offset));
    $offset = 0 unless ($offset);
    my $entries = param('entries');
    $entries = $default_rows unless ($entries && $entries > $default_rows);
    if (defined(param('target_test')) && param('target_test') ne '')
    {
	$offset = POSIX::floor((param('target_test') - 1)/$entries) * $entries;
	$offset = 0 if ($offset < 0);
	$init = 0;
    }
    my $page = POSIX::ceil($offset / $entries) +1;
    my $pages = POSIX::ceil($test / $entries);
    if ($init)
    {
	$page = $pages;
	$offset = $entries * ($pages - 1);
	$offset = 0 if ($offset < 0);
    }
    my $max = $offset + $entries;
    $max = $test if ($max > $test);
    for (my $x = $offset; $x <= $max; $x++)
    {
	my $is_read = &RFC::RFCCGI::rig_data_test_access_allow(param('rig'),$x);
	my $dir = $rig->webdir($x);
	my $f = $dir.'proglog';
	my $i = $dir.'info.txt';
	my $start_by = undef;
	my $start = undef;
	my $sample = undef;
	my $project = undef;
	if ($i && -e $i)
	{
	    open MYIN,'<',$i or die "Cant open file $i for reading $!";
	    while (my $str = <MYIN>)
	    {
		if ($str && $str =~ /test started at \s+(.+)$/i)
		{
		    $start = $1;
		}
		if ($str && $str =~ /test started by:\s+(.+)$/i)
		{
		    $start_by = $1;
		}		
		if ($str && $str !~ /test started by/i && $str =~ /test started\s+(.+)$/i)
		{
		    $start = $1;
		}
		last if ($start && $start_by);
	    }
	    close MYIN;
	}
	if ($f && -e $f && !($start && $start_by))
	{
	    open MYIN,'<',$f or die "Cant open file $f for reading $!";
	    my $str = <MYIN>;
	    close MYIN;
	    if (!$start && $str && $str =~ /^([\w\s]+)\s\d+\:\d+\:\d+\s(\d+)/)
	    {
		$start = $1.' '.$2 unless ($start);
	    }
	    if (!$start_by && $str && $str =~ /user\s\'(\w+)\'/)
	    {
		$start_by = $1;
	    }
	}
	if ($i && -e $i)
	{
	    open MYIN,'<',$i or die "Cant open file $i for reading $!";
	    while (my $str = <MYIN>)
	    {
		if ($str && $str =~ /^Sample number\s*\:\s*(.+)$/)
		{
		    $sample = $1;
		}
		if ($str && $str =~ /PROJECT\s*\#\s*(.*)$/)
		{
		    $project = $1;
		}
		last if ($sample && $project);
	    }
	    close MYIN;	    
	}
	my %h = (
	    'rig' => param('rig'),
	    'test' => $x,
	    'is_read' => $is_read, 
	    'dir' => $dir,
	    'start' => $start,
	    'start_by' => $start_by,
	    'sample' => $sample,
	    'project' => $project,
	    'hostname' => $hostname,
	    'ssl' => $ssl,
	    );
	push @list,\%h if (-e $dir);
    }
    $$RFC::RFCCGI::template_data{'pages'} = $pages;
    $$RFC::RFCCGI::template_data{'page'} = $page;
    $$RFC::RFCCGI::template_data{'entries'} = $entries;
    print process('list_tests.tt',&get_template_data_ref(\@list));
}
