#!/usr/bin/perl -T
# $Id: rig_conf.cgi,v 1.6 2017/08/24 09:10:24 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2011              ###
###                                                      ###
############################################################
############################################################
#$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';

use strict;
use warnings;
use CGI qw(:standard :netscape);
use RFC::Main;
use RFC::Rig;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;

my $acmode = get_cv('global','access_mode');
$acmode = 'celltest' unless ($acmode);
my $r = param('rig');
unless ($r)
{
    print header();
    print "No rig specified!\n";
    exit;
}
my $rig = RFC::Rig->new($r);

my $modifymode = user_auth($acmode,'m');
$modifymode = rig_cert() if (RFC::RFCCGI::rig_mode());
unless ($modifymode)
{
    $modifymode = RFC::Main::passwd_client('isadmin',get_id());
}

if (!defined(param))
{
    &print_header("Welcome to the the $name");
    print '<center>';
    &RFCCGI::login;
    print '</center>';
    &print_end;
}
else
{
    if (login_ok() && $modifymode)
    { 
        &print_header("Manual configuration for $rig");
        top_nav_bar_start;
        top_no_button;
        top_no_button;
        top_no_button;
        tab_newrow;
        if (get_CGI_value('action'))
        {
            top_no_button;
        }
        else
        {
            &js_back();
            top_js_return('#006600','white');
        }
	top_no_button;
        top_no_button;
        top_nav_bar_button('rig_main.cgi','','','To rig'.$rig->name.' main page','s3','rig,'.$rig->name.',1');
        top_nav_bar_button('main.cgi','','','To menu','s4');
        end_top_bar;
        param(-name=>'action',-value=>'')  unless (defined(param('action')));
	$_ = param('action');
      CASE:
	{
	    /^save$/ and do {&save(); &show_complete_file(); last CASE;};
	    ### Default response ###
	    &show_complete_file();
	}
	&print_end;
    }
    else
    {
	&not_auth;
    }
}

sub show_complete_file
{
    print center(h2("Manual configuration for rig ".$rig->name));
    my $string = join("\n",$rig->config->readlines());
    print start_form(-class=>'form');
    &print_hidden_rig();
    &action('save');
    print center(textarea(-name=>'conf',-value=>$string,-cols=>'120',-rows=>'24',-wrap=>'off'));
    print br;
    print center(submit(-name=>'save',-value=>'Save',-class=>'s0',-style=>"width:200px;"));
    print end_form;
}

sub save
{
    my $string = &get_CGI_value('conf');
    $string =~ s/\f//g;
    $string =~ s/\r//g;
    $string =~ s/\b//g;
    return undef unless ($string);
    $rig->config->writeline($string);
    my $user = &get_user();
    $string = "New configuration file written";
    $rig->proglog($string,$user);
}
