#!/usr/bin/perl -T
# $Id: crontab.cgi,v 1.1 2023/02/20 10:17:27 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);    

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



sub update_crontab()
{
    my $text = param('crontab');
    $text =~ s/[\r\n]+/\n/g;
    my $filename = $RFC::Header::publicdir.'rig'.$rig->name.'/crontab.txt';
    system("/usr/bin/co -l $filename") if (-e $filename.',v');
    my $file = SemaforeFile->new($filename);
    $file->writeline($text);
    system("echo '.' | /usr/bin/ci -u $filename");
    my $user = get_user();
    $rig->proglog("Crontab updated",$user);
    my $res = RFC::Main::CGI_client($rig->name,'crontab_file',$filename);
    unless ($res && $res =~ /Crontab installed/)
    {
        print center(h1({-class=>'Alert'},"Error: $res"));
    }
}


sub main_page
{
    my %data = (
	'crontab' => my $crontab = RFC::Main::CGI_client($rig->name,'crontab_read'),
	);
    print process('crontab.tt',&get_template_data_ref(\%data));
}

