#!/bin/sh
eval 'exec perl -x -S $0 ${1+"$@"}'
#! perl

use warnings;
use strict;

BEGIN { unshift @INC , "."; }

# The -g flag to recreaate all files is temporarily removed
# graph=""
# [ "$1" = "-g" ] && graph="YES"

use RRDutils qw( :DEFAULT );

# [ -n "$graph" ] && find $GRAPHDIR -type f -exec rm {} \;

# This initial piece is close to openWeb(), except for styles
open _HTML, '>' , "$graphdir/index.html" ;
print _HTML "<HTML>\n<HEAD>\n";
print _HTML "<TITLE>RRDutils main index</TITLE>\n";
print _HTML "</HEAD>\n<BODY>\n";
print _HTML "<H1>Por m&eacute;trica</H1>\n";

# First standard confs, then tags within confs
# we don't use getHosts to avoid problems with snmp tables

my %all_hosts;

print _HTML "<UL>";
opendir _CONFDIR , $confdir;
for my $conf ( readdir _CONFDIR ) {
   next if $conf =~ /^\./;
   my %names = map { (ref($_) eq 'ARRAY') ? (@$_)[0] : $_ => $_ } getHosts $conf ;
   my @hosts = keys %names;
   if ( ref($hosts[0]) ne "ARRAY" ) {
      @all_hosts{@hosts} = () if @hosts;
#      [ -n "$graph" ] && RRDgrapher $conf
      print _HTML "<LI><A HREF=$conf-i.html>$conf</A>\n" if @hosts;
      system( "RRDweb $conf" );
      }
   my @tags = getTags $conf ;
   if ( @tags ) {
      print _HTML "<LI>$conf (tagged)\n";
      print _HTML "<UL>\n";
      for my $tag ( @tags ) {
         my $hosts = getHosts "$confdir.$tag";
         if ( ref($hosts[0]) ne "ARRAY" ) {
            @all_hosts{@hosts} = () if @hosts;
#            [ -n "$graph" ] && RRDgrapher $conf.$tag
            print _HTML "<LI><A HREF=$tag-i.html>$tag</A>\n";
            system( "RRDweb $conf.$tag" );
            }
         }
      print _HTML "</UL>\n";
      }
   }
closedir _CONFDIR;
print _HTML "</UL>\n";


# No Graphs with multiple hosts


print _HTML "<H1>Por nodo</H1>\n";

for my $host ( sort keys %all_hosts ) {
   print _HTML "&nbsp;&nbsp;<A HREF=$host/index.html>$host</A>&nbsp;&nbsp;\n";
   system( "RRDnode $host" );
   }

print _HTML "</BODY>\n</HTML>\n";

