#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;

use lib "../../lib";
use HotSaNICparser;
use HotSaNICdiagram;
use RRDs;

# read global settings
#
my $MODNAME=HotSaNICparser::get_module_name();
my %CONFIG=HotSaNICparser::get_config("../..");

my $OUTDIR=$CONFIG{"WEBDIR"}."/".lc $MODNAME;
if ( ! -e $OUTDIR ) { mkdir $OUTDIR,0755; }

my $IMGFMT=lc $CONFIG{"IMAGEFORMAT"};
my $TEMPNAME="$OUTDIR/temp.$IMGFMT";

my (@ITEMS,$name,$range,$descr);

my $FORCE=@ARGV;

# read module-specific settings
#
foreach (HotSaNICparser::read_settings(".")) {
  my ($var,$value)=HotSaNICparser::parse_line($_);
  if ($var eq "SENSOR") { push @ITEMS,$value; }
  }

# generate diagrams
#
foreach (@ITEMS) {
  my (undef,$dev,$description,undef,undef,undef,$LEGEND)=split /,/;
  my $name=$dev;

# if database exists, create images...
  if ( -e "./rrd/$dev.rrd") {
    print "creating images for $description...\n";

    foreach $range ("1h","6h","1day","1week","1month","1year") {
      my ($descr,$file,$build,$fullrange,$DATESTRING)=HotSaNICdiagram::get_diagram_properties($range);

      my $FILENAME="$OUTDIR/$name-$file.$IMGFMT";

      my $make=$FORCE;
      if ($range eq "1h") { $make++; }
      elsif (! -e "$FILENAME") { $make++; }
      else  {
        my (undef,undef,undef,undef,undef,undef,undef,undef,undef,$mtime,undef,undef,undef) = stat( "$FILENAME" );
        if (time > ($mtime+$build)) { $make++; }
        }

      if ($make>0) {
	
        my ($prints,$xs,$ys);

	if ($range eq "1h") {
          ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1024", "-v",$LEGEND,
            "-s","-$range", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
            "-l 0", "--alt-autoscale-max", "--title", "$description - last $descr ($DATESTRING)",
            "DEF:avg=rrd/$dev.rrd:data:AVERAGE", 
            "AREA:avg#d0d0d0:",
            "LINE1:avg#00a000: ",
            "GPRINT:avg:AVERAGE:avg\\:%1.2lf %s $LEGEND",
            "GPRINT:avg:MAX:max\\:%1.2lf %s $LEGEND\\n",
            "HRULE:0#000000";
	  }
	  else {
          ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1024", "-v",$LEGEND,
            "-s","-$range", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
            "--alt-autoscale", "--alt-y-grid", "--title", "$description - last $descr ($DATESTRING)",
            "DEF:avg=rrd/$dev.rrd:data:AVERAGE",
            "DEF:max=rrd/$dev.rrd:data:MAX",
            "DEF:min=rrd/$dev.rrd:data:MIN",
            "AREA:max#c0ffc0:min/max",
            "LINE1:max#a0a0a0:",
            "AREA:min#ffffff:",
            "LINE1:min#a0a0a0:",
            "GPRINT:min:MIN:(min\\:%1.2lf %s",
            "GPRINT:max:MAX:max\\:%1.2lf %s $LEGEND)",
            "LINE2:avg#00a000:avg",
            "GPRINT:avg:MIN:(min\\:%1.2lf %s",
            "GPRINT:avg:AVERAGE:avg\\:%1.2lf %s",
            "GPRINT:avg:MAX:max\\:%1.2lf %s $LEGEND)\\n",
            "HRULE:0#000000";
	  }

        rename $TEMPNAME,$FILENAME;
        printf "  %-7s %d"."x%d %s\n",$descr,$xs,$ys,$FILENAME;
        }
      }
    }
  else { print "  No database found\n  Please check if the daemon is running.\n";next; }
  print "\n";
  }
exit 0;


