#! /usr/local/bin/perl
# 
# /*   $Id: bcnuconf.cgi,v 1.1 2000/05/05 13:06:09 jap Exp $
#
# *    Copyright (c) 1998 John Phillips
# *
# *    You may distribute under the terms of the Artistic License
# *
# */
#
require "bcnu.inc";
require "bcnulib.pl";

$CONFIGS="agents hostinfo bcnunet logpatterns services bcnuenv bcnulocal bcnud_server";

&methodparse;

$navigate=&navigate();
$showkey=&showkey();

$inlog=$in{"logfile"};
$insubmit=$in{"submit"};
$inlimit=$in{"limit"};

#
# main
#

&cklogin;

&bcnuerror("log", "You must login to use this function") unless $loginid;

(@CFG) = split(" ",$CONFIGS);

$errlevel=0;
$readrecs=0;

print &printhdr;

if ( $inlog ) {
	
	if ($in{"head"}) {
		$readcmd="head";
		$limct=0;
		}
	elsif ($in{"tail"}) {
		$readcmd="tail";
		$limct=0;
		}

	$LOGFILE="$BCNUHOME/etc/$inlog";
	open(LOG,"$readcmd -$inlimit $BCNUHOME/etc/$inlog|") || &bcnuerror("History", "cannot open $LOGFILE");
	while (<LOG>)
	{
		$limct++;
		last if $limct > $inlimit;
		push(@allfile,"<TR><TD>$limct<TD BGCOLOR=#ffff80>$_");
	}
	close (LOG);

	print &header("View logfile $inlog",0,60);
	print &logform(@CFG);
	print "<TABLE WIDTH=80% BORDER=1><TR><TH $DEFCELL>$inlog - showing $limct records</TR>\n";
	if ($in{"head"}) {
		print "<TR><TD><TABLE WIDTH=100%>@allfile</TABLE></TABLE>\n";
		}
	elsif ($in{"tail"}) {
		print "<TR><TD><TABLE WIDTH=100%>\n";
		print reverse(@allfile);
		print "</TABLE></TABLE>\n";
		}
}
else
	{
	print &header("Select logfile",0,60);
	print &logform(@CFG);
}

print &footer();

sub logform {

local (@displog) = @_;

$logselect="<SELECT NAME=\"logfile\">";
foreach $logfile (sort (@displog)) {
	$first="SELECTED" if $logfile eq $inlog;
	$logselect .= "<OPTION $first VALUE=$logfile>$logfile" ;
	$first="";
	}
$logselect .="</SELECT>";

$form = <<"Endofform";

</TABLE>
<TABLE BORDER=0>
<FORM METHOD="POST"> 
<TR>
<TH ALIGN=CENTER>Logfile<TH ALIGN=CENTER>Record Limit</TH>
<TR>
<TD ALIGN=CENTER>$logselect</TD>
<TD ALIGN=CENTER><SELECT NAME=\"limit\"> 
<OPTION  VALUE=$QRYLIMIT>$QRYLIMIT
<OPTION  VALUE=100>100
<OPTION  VALUE=250>250
<OPTION  VALUE=500>500
</SELECT>
<TR>
<TD ALIGN=CENTER $CELL><INPUT TYPE="submit" NAME="head" VALUE="View Logfile forwards"></TD>
<TD ALIGN=CENTER $CELL><INPUT TYPE="submit" NAME="tail" VALUE="View Logfile backwards"></TD>
</TR>
</FORM>
</TABLE>
<P>
Endofform

return ($form);
}

