#!/usr/bin/perl -T
# $Id: edit_conf.cgi,v 1.2 2023/02/22 12:35:22 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2005              ###
###                                                      ###
############################################################
############################################################
use strict;
use warnings;
use CGI qw(:standard :netscape);
use Fcntl qw(:DEFAULT :flock);
use RFC::Main;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;
use Sys::Hostname;

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

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 show_complete_file
{
    my %data = (
	'allow' => $$ref{'cert'}, 
	'mode' => 'global',
	'conf' =>  [$config->readlines()],
	);
    print process('edit_conf.tt',&get_template_data_ref(\%data));
}

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