#! /usr/local/bin/perl
# 
# /*   $Id: bcnuview.cgi,v 1.1 1998/10/27 15:17:51 jap Exp $
#
# *    Copyright (c) 1998 John Phillips
# *
# *    You may distribute under the terms of the Artistic License
# *
# */
#
require "bcnu.inc";
require "bcnulib.pl";

&methodparse;

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

$host=$in{"host"};
$inagent=$in{"agent"};
$inview=$in{"view"};
$inviewspec=$in{"viewspec"};
#
# main
#

$errlevel=0;
$sound=1;

if ($inviewspec) {

	$sound=0;
	&readviewindex;
	undef %subview;
	&genview($inview,$inviewspec);
	print &printhdr;
	print &header("$nview{$inview} - $inviewspec",$errlevel,2);
        print &agent_head();
        foreach $entry (sort (split(" ",$hostlist{$inviewspec}))) {
                print $line{$entry};
                }
	print &footer();
}
elsif ($inview) {

	&readviewindex;
	undef %subview;
	&genview($inview);
	print &printhdr;
	print &header("$nview{$inview}",$errlevel,2);
        foreach $entry (keys %subview) {
                print $vline{$entry};
                }

	print &footer();
}
else	{
	&readviewindex;
	print &printhdr;
	print &header("Index",$errlevel,2);
	foreach $entry (sort keys %view) {
        print "<TR><TD $ECELL><A HREF=\"$BCNUURL/cgi-bin/bcnuview.cgi?view=$view{$entry}\">$entry</A></TD>";
        }
	print &footer();
}


#
#
# process views
#
sub readviewindex {
open (VIEW,"$VIEWHOME/view.cnf") || &bcnuerror("views", "can't open $VIEWHOME/view.conf");
while (<VIEW>)
        {
	chop;
	next if /^[#\s]/; 
        ($vname,$vfile) = split(":");
        $vname =~ s/ /-/g;
        $vfile =~ s/ //g;
        $view{$vname}=$vfile;
	$nview{$vfile}=$vname;
	&genview($vfile);
        }
close (VIEW);

return (%view);
}

#
# Generate pages for each view
#
sub genview {

local ($viewname,$viewspec) =@_;

        open (VNAME,"$VIEWHOME/$viewname") || &bcnuerror("views", "can't open $VIEWHOME/$viewname");

        while (<VNAME>)
                {
		next if /^[#\s]/; 
		chop;
                $errlevel=0;
                ($subname,$hostlist) = split(":");
		$subname =~ s/ /-/g;
		if ($viewspec) {
			next unless $subname =~ /$viewspec/;
		}
                (@hosts) = split(" ",$hostlist);
		$hostlist{$subname}="@hosts";
                foreach $host (@hosts) {
			&getagents($host);
			&mainhost($host);
                        $errlevel = 5 if ($errlevel{$host} == 5 && $errlevel == 0); 
                        $errlevel = 9 if $errlevel{$host} == 9;
                        }
		$subview{$subname}=$errlevel;
		}
	close (VNAME);
	foreach $subv (keys %subview) {

        	$vline{$subv} = "<TR><TD $ECELL><A HREF=\"$BCNUURL/cgi-bin/bcnuview.cgi?view=$viewname\&viewspec=$subv\">$subv";
       	        if ( $subview{$subv} == 9 ) {
               	           $vline{$subv} .= "<TD $CELL> $SAD ALT=\"error\" ></TD>\n";
                       	   $errlevel=9;
                        }
                elsif ( $subview{$subv} == 5 ) {
                           $vline{$subv} .= "<TD $CELL> $WORRY ALT=\"warning\" ></TD>\n";
                           $errlevel=5 if $errlevel==0;
                        }
                else {
                           $vline{$subv} .= "<TD $CELL> $NORMAL ALT=\"OK\" ></TD>\n";
                        }
                $vline{$subv} .= "</A></TR>";

	}
}
