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

# Original code contributed by Henry Steinhauer

use warnings;
use strict;

BEGIN { unshift @INC , "."; }

use RRDutils qw( :DEFAULT :Web );

die "\nUsage RRDweb conf[,conf2,...]\n\n" unless @ARGV;

my $ncols = 2;
my $col = 0;

# This function prints the actual graph web page in its deepest form, with
# every period and no external href
sub rrdHtml($$$@) {
   my ( $host , $target , $graph , @periods ) = @_ ;

   my $html = openWeb( "$host/$target/$graph" , "History for $target at $host" );
   for my $period ( @periods ) {
      my $imgname = $getNAME{'src'}( $graph , $period );
      print $html "<tr>\n";
      print $html htmlAnchor $imgname ;
      print $html "</tr>\n";
      }
   closeWeb($html);
   }

sub rrdContainer($$$$) {
   my ( $host , $target , $keys , $hash ) = @_ ;

   $col = 0;

   my $html = openWeb( "$host/$target" , "Summary plots for $target" ) ;
   for my $graph ( @{$keys} ) {
      my $period = (split /,/ , $$hash{$graph}{'periods'})[0] ;
# Maybe needed for real multihosts ...
#      my $target = $$hash{$graph}{'ds'}[0] ;
      my $imgname = $getNAME{'src'}( $target , $graph , $period );
      print $html "<tr>\n" unless $col % $ncols ;
      print $html htmlAnchor $imgname , $target , $graph ;
      print $html "</tr>\n" if $col % $ncols == $ncols - 1 ;
      $col++;
      }
   closeWeb($html);
   }

sub webTarget($$$$@) {
   my ( $target , $multi , $single , $hash ) = splice @_ , 0 , 4;

   my @hosts = getHosts $target ;
   unless ( @hosts || @_ ) {
      warn "No host to process for $target\n";
      return;
      }

   $target =~ s+^.*\.++ ;

##  --
#   Create the top level Index page
#      one entry per Host - Use first graph & first period

   my $html = openWeb( "$target-i" , "Summary for $target" );

   if ( @$multi ) {
      my $graph = $$multi[0] ;
      $graph = $mainGraph if $mainGraph && $$hash{$mainGraph};
      my $ds = $$hash{$graph}{'ds'}[0];
      my $period = (split /,/ , $$hash{$graph}{'periods'})[0];
      my $imgname = $getNAME{'src'}( $target , $ds , $graph , $period );
      print $html "<tr>\n" unless $col % $ncols ;
      if ( $$multi[1] ) { # Using $#$multi seems too risky ...
         print $html htmlAnchor( $imgname , $target , $ds );
      } else {
         print $html htmlAnchor( $imgname , $target , $ds , $graph );
         }
      print $html "</tr>\n" if $col % $ncols == $ncols - 1 ;
      $col++;
      }

   if ( @$single ) {
      my $graph = $$single[0] ;
      $graph = $mainGraph if $mainGraph && $$hash{$mainGraph};
      my $period = (split /,/ , $$hash{$graph}{'periods'})[0];
      for my $host ( @hosts ) {
# snmptables under revision
      print $html "<tr>\n" unless $col % $ncols ;
         if ( ref($host) eq 'ARRAY' ) {
            my ( $node , @indexes ) = @{$host};
            for my $idx ( @indexes ) {
# needed in case index is not concatenated
#               mkdir "$graphdir/$node/$target" ;
               mkdir "$graphdir/$node/$target.$idx";
               my $imgname = $getNAME{'src'}( $node , "$target.$idx" , $graph , $period );
               if ( $$single[1] ) {
                  print $html htmlAnchor( $imgname , $node , "$target.$idx" );
               } else {
                  print $html htmlAnchor( $imgname , $node , "$target.$idx" , $graph );
                  }
               }
         } else {
      my $imgname = $getNAME{'src'}( $host , $target , $graph , $period );
            if ( $$single[1] ) {
               print $html htmlAnchor( $imgname , $host , $target );
            } else {
               print $html htmlAnchor( $imgname , $host , $target , $graph );
               }
            }
      print $html "</tr>\n" if $col % $ncols == $ncols - 1 ;
      $col++;
         }
      }

   closeWeb($html);

##  --
#   Create the multi-host Index pages

   if ( @$multi ) {
      my $graph = $$multi[0];
      my $ds = $$hash{$graph}{'ds'}[0] ;
      rrdContainer $target , $ds , $multi , $hash if $$multi[1] ;
      for my $graph ( @$multi ) {
         my $ds = $$hash{$graph}{'ds'}[0] ;
         my @periods = split /,/ , $$hash{$graph}{'periods'} ;
         rrdHtml $target , $ds , $graph , @periods ;
         }
      }

   @hosts = @_ if @_;

##  --
#   Create the node-target Index page
#      one entry per Graph - refers to graph history

   if ( @$single ) {
      for my $host ( @hosts ) {
# snmptables under revision
         if ( ref($hosts[0]) eq 'ARRAY' ) {
            my ( $host , @indexes ) = @{$host};
            for my $idx ( @indexes ) {
# needed in case index is not concatenated
#               mkdir "$graphdir/$host/$target" ;
               mkdir "$graphdir/$host/$target.$idx";
               rrdContainer $host , "$target.$idx" , $single , $hash if $$single[1] ;
               for my $graph ( @$single ) {
                  my @periods = split /,/ , $$hash{$graph}{'periods'} ;
                  rrdHtml $host , "$target.$idx" , $graph , @periods ;
                  }
               }
         } else {
            rrdContainer $host , $target , $single , $hash if $$single[1] ;
            for my $graph ( @$single ) {
               my @periods = split /,/ , $$hash{$graph}{'periods'} ;
               rrdHtml $host , $target , $graph , @periods ;
               }
            }
         }
      }
   }

for my $target ( split /,/ , shift ) {

   my ( $conf , $tag ) = split /\./ , $target ;

   unless ( -f "$confdir/$conf" ) {
      warn "No '$conf' configuration file found\n";
      next;
      }

   changeDefs $conf ;
   getIntervals $conf ;

   my %graphs = getGraphs $conf ;
   unless ( %graphs ) {
      warn "No graph defined at $conf" unless %graphs;
      next;
      }

   my ( @multi , @single ) ;
   for my $graph ( keys %graphs) {
      if ( $graphs{$graph}{'type'} eq 'PH' ) {
         push @multi , $graph ;
      } else {
         push @single , $graph ;
         }
      }

   if ( $tag && $tag eq '*' ) {
      for ( getTags $conf ) {
         webTarget "$conf.$_" , \@multi , \@single , \%graphs , @ARGV;
         }
   } else {
       webTarget $target , \@multi , \@single , \%graphs , @ARGV ;
      }

   }

