#!/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 $IMGFMT=lc $CONFIG{"IMAGEFORMAT"};

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

my $FORCE=@ARGV;

# generate diagrams
#

my $name="usv";

# if database exists, create images...
  if ( -e "./rrd/$name.rrd") {

  foreach my $DEV ("loadpct","battpct","outv","battv","timeleft","temp") {

    my $title;
    my $LEGEND;

    if ( $DEV eq "loadpct" )  { $title="USV load";        $LEGEND="%"; }
    if ( $DEV eq "battpct" )  { $title="battery charge";  $LEGEND="%"; }
    if ( $DEV eq "outv" )     { $title="output voltage";  $LEGEND="Volts"; }
    if ( $DEV eq "battv" )    { $title="battery voltage"; $LEGEND="Volts"; }
    if ( $DEV eq "timeleft" ) { $title="time left"; $LEGEND="minutes"; }
    if ( $DEV eq "temp" )     { $title="temperature"; $LEGEND="°C"; }

    my $unit=$LEGEND;
    if ($unit eq "%") { $unit="%%"; }

    print "creating images for $name ($title) ...\n";

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

      my $FILENAME="$OUTDIR/$DEV-$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);

        ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b","1024",
        "-v", $LEGEND, "-s","-$fullrange", "-w",$CONFIG{WIDTH},
        "-h", $CONFIG{HEIGHT}, "-a", uc($IMGFMT), "-l 0", 
        "--alt-autoscale-max", "--title", "$title - last $descr ($DATESTRING)",
        "DEF:onbatt=rrd/$name.rrd:onbatt:AVERAGE",
        "DEF:replacebatt=rrd/$name.rrd:replacebatt:AVERAGE",
        "DEF:valmin=rrd/$name.rrd:$DEV:MIN",
        "DEF:valavg=rrd/$name.rrd:$DEV:AVERAGE",
        "DEF:valmax=rrd/$name.rrd:$DEV:MAX",
        "CDEF:obinf=onbatt,0,GT,inf,0,IF",
        "CDEF:obmin=onbatt,0,GT,valmin,0,IF",
        "CDEF:rbinf=replacebatt,0,GT,inf,0,IF",
        "CDEF:rbmin=replacebatt,0,GT,valmin,0,IF",
        "CDEF:or=onbatt,replacebatt,+,10,*",
        "CDEF:orinf=or,1,GT,inf,0,IF",
        "CDEF:ormin=or,1,GT,valmin,0,IF",
        "AREA:obinf#c0c0ff:on battery\\n",
        "AREA:rbinf#ff8080:battery needs replacement\\n",
        "AREA:orinf#ff80e0:both (overlapping)\\n",
        "AREA:valmax#d0ffd0:min/max",
        "LINE1:valmax#c0c0c0:",
        "AREA:valmin#ffffff:",
        "AREA:obmin#c0c0ff:",
        "AREA:rbmin#ff8080:",
        "AREA:ormin#ff80e0:",
        "LINE1:valmin#c0c0c0:",
        "LINE2:valavg#00a000:avg",
        "GPRINT:valmin:MIN:  min\\:%7.2lf %s",
        "GPRINT:valavg:AVERAGE: avg\\:%7.2lf %s",
        "GPRINT:valmax:MAX: max\\:%7.2lf %s ($unit)\\n",
        "HRULE:0#000000";
            
        rename $TEMPNAME,$FILENAME;
        printf "  %-7s %d"."x%d %s\n",$descr,$xs,$ys,$FILENAME;

        }
      }
    }
  print "\n";
  }
  else { print "  No database found\n  Please check if the daemon is running.\n"; }

exit 0;

