#!/usr/bin/perl -T
# $Id: manual_edit_prog.cgi,v 1.3 2023/02/22 12:47:06 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2011              ###
###                                                      ###
############################################################
############################################################

use strict;
use warnings;
use CGI qw(:standard :netscape);
use RFC::Main;
use RFC::Rig;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;
use SemaforeFile;
my $ref = &RFC::RFCCGI::get_user_data_ref();
my $uid = $$ref{'uid'};

my $r = param('rig');
unless ($r)
{
    print header();
    print "No rig specified!\n";
    exit;
}
my $rig = RFC::Rig->new($r);
my $fn = 'custom.conf';
$fn = 'flowvar.conf' if (param('file') && param('file') eq 'flowvar');
$fn = 'impedance.conf' if (param('file') && param('file') eq 'impedance');
my $file = SemaforeFile->new($RFC::Header::publicdir.'rig'.$rig->name.'/'.$fn);
my $acmode = &get_cv('global','access_mode');

my $hstring = 'Manual program edit for rig '.$rig->name;
$hstring = 'Edit flow variation macro for rig '.$rig->name if (param('file') && param('file') eq 'flowvar');
$hstring = 'Edit impedance macro for rig '.$rig->name if (param('file') && param('file') eq 'impedance');

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(); &show_complete_file(); last CASE;};
            ### Default response ###
            &show_complete_file();
        }
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}

sub save
{
    my $string = &get_CGI_value('conf');
    $file->writeline($string);
}

sub show_complete_file
{
    my %data = (
        'allow' => $$ref{'cert'}, 
        'mode' => param('file'),
        'conf' =>  [$file->readlines()],
	'file' => param('file'),
        );
    print process('edit_conf.tt',&get_template_data_ref(\%data));
}

