#!/usr/bin/perl -T
# $Id: prelogin.cgi,v 1.34 2023/02/22 06:10:37 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2005 - 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 qw(hostname);
use MIME::Base64;
use SemaforeFile;
my $ssl = 0;
$ssl = 1 if (CGI::https());


print process('main_nav_nologin.tt');
$_ = param('action');
 CASE:
{
    ## Insert case statements ###
    &main_page_template;
}
print process('end_html.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' => CGI::https(),
	'name' => $name,
        );
    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));
}

