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

use warnings;
use strict;

BEGIN { unshift @INC , "."; }

use RRDutils qw( :DEFAULT :Graph );

die "\nUsage RRDgrapher conf[,conf2,...] [host1 host2 ...]\n\n" unless @ARGV;

sub graphTarget($$$$$$$@) {
   my ( $target , $type , $vars ) = splice @_ , 0 , 3 ;
   my $legend = shift ;
   my ( $graph , $period ) = splice @_ , 0 , 2 ;
   my $cmdargs = shift ;

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

# The unless is the trick for PH plots with SNMP tables
#   $target =~ s+^.*\.++ unless ref($hosts[0]) eq "ARRAY" ;
   $target =~ s+^.*\.++ ;

# multihost plots under revision (not using rrdGraph)
#   if ( $type eq "PH" ) {
#      my $ds;
#      for $ds ( @$vars ) {
#         system "mkdir -p $graphdir/$target";
#         my $imgname = $getNAME{'img'}( $host , $target , $graph , $period ) ;
#         RRDs::graph ( "$imgname" , @$cmdargs , "--title=$ds @ $target" ,
#           $getDEFs{$type}($ds,$target,\%{$legend},@hosts) ) ;
#         my $error = RRDs::error;
#         print STDERR "ERROR : $error\n" if $error ;
#         }
#      return;
#      }

# This is a newer version for multihost, which uses a different
#     format on the configuration file, where hosts are explicited
#   if ( $type eq "PH" ) {
#      my ( $ds , @hosts ) = @$vars ;
#      my @cmd = $getDEFs{$type}($ds,$target,\%{$legend},@hosts) ;
#      rrdGraph($target,$ds,@$cmdargs,@cmd);
#      return;
#      }

   for my $host ( @hosts ) {
# snmptables under revision
      if ( ref($host) eq "ARRAY" ) {
         my ( $host , @indexes ) = @{$host};
         for my $idx ( @indexes ) {
# needed in case index is not concatenated
#            mkdir "$graphdir/$host/$target" ;
            my @cmd = $getDEFs{$type}($host,"$target.$idx",\%{$legend},@$vars);
            my $res = rrdGraph($host,"$target.$idx",$graph,$period,@$cmdargs,@cmd);
            print STDERR "ERROR : $res\n" if $res ;
            }
      } else {
         my @cmd = $getDEFs{$type}($host,$target,\%{$legend},@$vars);
         my $res = rrdGraph($host,$target,$graph,$period,@$cmdargs,@cmd);
         print STDERR "ERROR : $res\n" if $res ;
         }
      }
   }

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 %legend = getLabels $conf ;
 
   my %graphs = getGraphs $conf ;
   unless ( %graphs ) {
      warn "No graph defined at $conf";
      next;
      }

   for my $graph ( keys %graphs ) {

# What about checking here for multihostgraphs ??

      my @vars = @{$graphs{$graph}{'ds'}} ;
      my $type = $graphs{$graph}{'type'} ;
      my @graphSize = @{$graphs{$graph}{'size'}};
      @graphSize = @figSize unless @graphSize;

      for my $period ( split /,/ , $graphs{$graph}{'periods'} ) {

         my @cmdargs = ( @graphSize , "--lazy" , "--start" , "$graphInterval{$period}" ) ;
         push @cmdargs , "$graphs{$graph}{'opts'}" if $graphs{$graph}{'opts'} ;

         if ( $tag && $tag eq '*' ) {
            for ( getTags $conf ) {
               graphTarget( "$conf.$_" , $type , \@vars , \%legend , $graph , $period , \@cmdargs , @ARGV );
               }
         } else {
             graphTarget( $target , $type , \@vars , \%legend , $graph , $period , \@cmdargs , @ARGV);
             }

         }
      }
   }
