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

$TURBOAGENT="1";
$inlimit=$QRYLIMIT;

&methodparse;

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

$inhost=$in{"host"};
$inagent=$in{"agent"};
#@inagent=split('!',$in{"agent"});
$innormal=$in{"normal"};
$inerrors=$in{"errors"};
$inwarnings=$in{"warnings"};
$insubmit=$in{"submit"};
$inlimit=$in{"limit"} if $in{"limit"};
$indtime=$in{"dtime"} if $in{"dtime"};
@logfiles=split('!',$in{"logfile"});

#
# main
#


$errlevel=0;
$readrecs=0;

if ($indtime) {
	
	$inlogfile=$in{"logfile"};

	$rec=&gethistrec();
	
	&agentdetail ($host,$ag,$rec);

	print &printhdr;
	print &header("$host - Agent ($inagent)",$errlevel,5*$REFRESH);
	print $agdisp;
	print &footer();
}

elsif (@logfiles) {

	$agdisp = "<TR><TH $DEFCELL>System<TH $DEFCELL>Status<TH $DEFCELL>Agent<TH $DEFCELL>Date/Time<TH $DEFCELL>Message</TR>\n";
	foreach $inlogfile (sort @logfiles ) {
		undef %record;
		&gethist() if $readrecs <= $inlimit;
		foreach $hist (sort keys %record) {
			$agdisp .= &agentdisp($inhost,$inagent,$record{$hist});
		}
	}
	print &printhdr;
	print &header("Query",$errlevel,5*$REFRESH);
	print $agdisp;
	print "<TR><TH COLSPAN=100% $BGCELL>$readrecs records returned for this query\n";
	print &footer();
}
else	{

	&gethosts();
	undef %mon;
	opendir(HDIR,"$DATA") || &bcnuerror("History", "cannot open directory $BCNUDATA");
	@files=grep (/\.log$/,readdir(HDIR));
	foreach $file (sort @files) {
	        next if $file =~ /\.$/;
	        $mon{$file}++;
	}
	@logs = (keys (%mon));

	@displog=sort grep(s/\.log$//,@logs);
	@agentlog=sort @agents;
	print &printhdr;
	print &header("Query",0,5*$REFRESH);
	print &selectform;
	print &footer();
}
