#!/usr/bin/perl -T
# $Id: main.cgi,v 1.58 2025/08/21 11:11:06 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();
my $uid = $$ref{'uid'};
my $ssl = CGI::https();
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');
	$_ = '' unless (defined($_));
      CASE:
        {
            ### Insert case statements ###
            &main_page_template;
        }
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}
exit;


sub main_page_template
{
    my $listserver =  get_cv('servers','listserver');
    my $is_cache = 0;
    my $save_cache = 0;
    my %cache = ();
    my @list = ();
    my @listservers = ();
    my $file = '/tmp/RFCcache.txt';
    my $conf = SemaforeFile->new($file);
    $conf->lock_sh();
    if ($conf->exist)
    {
        $is_cache = 1;
        my $mtime = $conf->mtime();
        $is_cache = 0 if ($mtime + 3600 < time);
        $is_cache = 0 if (param('reload'));
    }
    if ($is_cache)
    {
        foreach my $l ($conf->readlines())
        {
            next unless ($l);
            chomp $l;
            my ($k,$d) = split(/\s*\#\s*/,$l);
            $cache{$k} = $d;
        }
    }
    $conf->lock_un();
    if ($listserver)
    {
        chomp $listserver;
        unless ($is_cache && $cache{'servers'})
        {
	    my $str= &RFC::Main::report_client($listserver,'servers');
	    my @l = split(/\t/,$str) if ($str && $str !~ /ERROR/);
	    foreach (@l)
	    {
		if (/([\w\.\-]+)/)
		{
		    push @list,$1;
		}
	    }
	    $cache{'servers'} = join(',',@list);
	    $save_cache = 1;
	}
	@list = split(/\s*\,\s*/,$cache{'servers'});
	unless ($is_cache && $cache{'listservers'})
        {
	    my $str= &RFC::Main::report_client($listserver,'list_servers');
	    my @l = split(/\t/,$str) if ($str && $str !~ /ERROR/);
	    foreach (@l)
	    {
		if (/([\w\.\-]+)/)
		{
		    push @listservers,$1;
		}
	    }
	    $cache{'listservers'} = join(',',@listservers);
	    $save_cache = 1;
	}
	@listservers = split(/\s*\,\s*/,$cache{'listservers'});
    }
    foreach my $s (@list)
    {
	unless ($is_cache && $cache{$s.'_active_rigs'})
	{
	    my $str = &RFC::Main::report_client($s,'active_rigs');
	    $str = '' unless defined($str);
	    $cache{$s.'_active_rigs'} = $str;
	    $save_cache = 1;
	}
	unless ($is_cache && $cache{$s.'_rigs'})
	{
	    my $str = &RFC::Main::report_client($s,'rigs');
	    $str = '' unless defined($str);
	    $cache{$s.'_rigs'} = $str;
	    $save_cache = 1;
	}
	my $str = $cache{$s.'_active_rigs'};
    }
    my $str = get_cv('celltest','rigs');
    $str = '' unless defined($str);
    my @l = split(/\s*\,\s*/,$str);
    my %data = (
	'listserver' => $listserver,
	'cache' => \%cache,
	'listservers' => [@listservers],
	'rig_list' => [@list],
	'local_list' => [@l],
	'https' => $ssl
	);
    eval {
        if (!$is_cache && %cache || $save_cache)
        {
            $conf->lock_ex();
            $conf->writeline('');
            foreach my $key (keys %cache)
            {
                $conf->append($key.'#'.$cache{$key});
            }
            chmod 0666,$file;
            $conf->lock_un();
        }
    };
    if ($@)
    {
        warn $@;
    }
    print process('main.tt',&get_template_data_ref(\%data));
}

