#!/usr/bin/perl -T
# $Id: logbook.cgi,v 1.10 2023/02/21 15:13:03 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::Rig;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;
use CGI qw(:standard :netscape);
use SemaforeFile;
use LWP::UserAgent;

my $ref = &RFC::RFCCGI::get_user_data_ref();
my $uid = $$ref{'uid'};
my $r = param('rig');
my $rig = RFC::Rig->new($r);    

if (!defined(param))
{
    print process('to_login.tt');
}
else
{
    if ($uid)
    {
        print process('main_nav.tt');
        $_ = param('action');
      CASE:
        {
            ### Insert case statements ###
            ### Default response ####
	    /^Save/ and do {&save; &insert_data_form(); last CASE;};
	    ### Default response ####
    	    &insert_data_form;
        }
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}
    
exit;

sub save
{
    my $logall = &get_CGI_value('log_all');
    my $string = &get_CGI_value('text');
    my $time = localtime;
    my $user = get_user();
    my $str = $time.": User $user added:\n".$string."\n\n";
    eval
    {
	foreach my $rigname (&RFC::Main::get_rigs())
	{
	    my $r = RFC::Rig->new($rigname);
	    next unless ($logall || $r->name == $rig->name);
	    my $f = $RFC::Header::publicdir.'rig'.$r->name.'/logbook.txt';
	    my $l = $r->riglock; 
	    my $file = SemaforeFile->new($f,$l);
	    my $test = $r->session;
	    $f = $RFC::Header::publicdir.'rig'.$r->name.'/'.$r->name.'test'.$test.'/info.txt';
	    my $info = SemaforeFile->new($f,$l);
	    $file->append($str);
	    $info->append($str);
	    my $equipid = $r->get_cv('main','equipmentid');
	    &remote_log($equipid,$string) if ($equipid);
	}
    };
    if ($@)
    {
	&errorlog($@);
	&print_error();
    }
}



sub remote_log($$)
{
    my $eqid = shift;
    my $msg = shift;
    my $eqadr = get_cv('global','remote_equipment_database');
    return unless ($eqadr);
    my $ua = LWP::UserAgent->new();
    $ua->timeout(60);
    my $url = $eqadr;
    my $query = '?name='.&get_user().'&pass='.&get_pass().'&action=new_maint_log&type=Riglog&equipid='.$eqid.'&description='.$msg;
    my $request = new HTTP::Request('GET',$url.$query);
    my $response = $ua->request($request);
    return if ($response->is_success);
    &errorlog("Could not append mesage to remote message log for rig $rig!");
}

sub insert_data_form
{
    my %data = ();
    my $f = $RFC::Header::publicdir.'rig'.$rig->name.'/logbook.txt';
    my $l = $rig->riglock; 
    my $file = SemaforeFile->new($f,$l);
    my $test = $rig->session;
    $data{'content'} = join("\n",$file->readlines);
    print process('logbook.tt',&get_template_data_ref(\%data));
}

