#! /usr/local/bin/perl
# 
# /*   $Id $
#
# *    Copyright (c) 1998 John Phillips
# *
# *    You may distribute under the terms of the Artistic License
# *
# */
#

push (@INC,"$ENV{'BCNUHOME'}/cgi-bin");
push (@INC,"/usr/local/bcnu/cgi-bin");

require "bcnu.inc";
require "bcnulib.pl";
require "bcnuqrylib.pl";

$WWW="$BCNUHOME/docs/sample";
$DATA="$BCNUHOME/docs/sample/data";

$AGENTDETAIL=1;
$AGENTS=1;

$BCNUSTATIC=1;

#
# main
#

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

# walk thru the hosts directory and get a list of agents for each host
#

&gethosts;

@HOSTS=@hosts;

$numhosts=@HOSTS;

$errlevel=0;
$sound=0;

foreach $host (sort @HOSTS) {
        next if $host =~ /\.$/;
	&mainhost($host);
}

foreach $host (sort @HOSTS) {
        &sumhost($host);
}

# Produce status page
open (WEB,">$WWW/status.htm") || die "can't write web page $WWW/status.htm";

$perline=0;
$BCNUSTATIC=0;
print WEB &cleanup(&header("Status Only",$errlevel,5));
$BCNUSTATIC=1;
if ($errlevel) {
	print WEB &cleanup("<TR><TH COLSPAN=$PERLINE $DEFCELL>$numhosts Systems Monitored</TR>\n");
	foreach $entry (sort keys %cell) {
		if ($perline==0) {
			print WEB &cleanup("<TR $BASEROW>\n");
			$perline=$PERLINE;
		}
#		print "$entry $hoststat{$entry}\n";
		if ($hoststat{$entry} > 0) {
			$perline--;
       			print WEB &cleanup($cell{$entry}) if $hoststat{$entry} > 0;
				}
	        	}
	}

print WEB &cleanup(&footer());
close (WEB);

# Produce summary page
open (WEB,">$WWW/summ.htm") || die "can't write web page $WWW/summ.htm";

$perline=0;
$BCNUSTATIC=0;
print WEB &cleanup(&header("All Systems - Summary",$errlevel,5));
$BCNUSTATIC=1;
print WEB &cleanup("<TR><TH COLSPAN=$PERLINE $DEFCELL>$numhosts Systems Monitored</TR>\n");
foreach $entry (sort keys %cell) {
	if ($perline==0) {
		print WEB &cleanup("<TR $BASEROW>\n");
		$perline=$PERLINE;
	}
	$perline--;
	print WEB &cleanup($cell{$entry});
	}

print WEB &cleanup(&footer());
close (WEB);

# produce platform page

# get an array with each host in it's correct platform
foreach $h (sort keys %platform)	{
	$plat{$platform{$h}}.="$h:";
	}

#
open (WEB,">$WWW/platforms.htm") || die "can't write web page $WWW/platforms.htm";

$perline=0;

$BCNUSTATIC=0;
print WEB &cleanup(&header("All Systems - Summary by type",$errlevel,5));
$BCNUSTATIC=1;
print WEB &cleanup("<TR><TH colspan=$PERLINE ALIGN=CENTER $BGCELL>$numhosts Systems Monitored</TH></TR>\n");
foreach $p (sort keys %plat)    {
	(@plattype) = split(":",$plat{$p});
	$numthishost=@plattype;
	print WEB &cleanup("<TR><TH colspan=$PERLINE ALIGN=CENTER $ECELL>$bcnu_host_type{$p} Systems</TH></TR>\n");
	$perline=0;
	foreach $entry (sort @plattype) {
		if ($perline==0) {
			print WEB &cleanup("<TR $BASEROW>\n");
			$perline=$PERLINE;
		}
#		$perline--;
        	print WEB &cleanup($cell{$entry});
	        }
}
print WEB &cleanup(&footer());

close WEB;

# generate query page
opendir(HDIR,"$DATA") || die "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;


open (WEB,">$WWW/bcnuqry.htm") || die "can't write sample query page";
print WEB &cleanup(&header("Query",0,200));
print WEB &cleanup(&selectform);
print WEB &cleanup(&footer());

# agent stuff below here
$sound=0;

if ($AGENTS) {

#Generate agent pages
foreach $host (@HOSTS) {

	$errlevel=0;
	undef %mon;
	&getagents ($host);
	undef %mon;
        &agenthost ($host);

	open (WEB,">$WWW/$host.htm") || die "can't write web page $WWW/hosts/$host.htm";

        print WEB &cleanup(&header("$host - Agent Summary",$errlevel,2));
        print WEB &cleanup($agdisp);
        print WEB &cleanup(&footer());
	close (WEB);

	if ($AGENTDETAIL) {	
	foreach $agent (@agents) {
	        &agentdetail ($host,$agent);
		open (WEB,">$WWW/$host.$ag.htm") || die "can't write web page $WWW/hosts/$host_$ag.htm";

	        print WEB &cleanup(&header("$host - Agent ($ag)",$errlevel,2));
	        print WEB &cleanup($agdisp);
	        print WEB &cleanup(&footer());
		close (WEB);
		}
	}
	}
}

#####################################
sub cleanup {

local ($cleanpage)=@_;

$cleanpage =~ s/href=$BCNUURL\/cgi-bin//g;
$cleanpage =~ s/ACTION=.*\/bcnu.cgi"//g;
$cleanpage =~ s/$BCNUURL\/images/..\/sample/g;
#$cleanpage =~ s/$BCNUURL\/docs\//..\/docs\/htframe.htm/g;
$cleanpage =~ s/$BCNUURL/..\/sample/g;
$cleanpage =~ s/\/sample\/docs\/help/\/help/g;
$cleanpage =~ s/\hosts\///g;
$cleanpage =~ s/POST//g;

return ($cleanpage);

}
