#!/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 ($LEGEND,@ITEMS,$name,$range,$descr);
my $IOMIN=500;

my $FORCE=@ARGV;

$LEGEND="accesses";

# generate diagrams
#

opendir DIR,"rrd";
@ITEMS=grep /\.rrd/,readdir DIR;
closedir DIR;

if (! (@ITEMS) ) { print "  No database found\n  Please check if the daemon is running.\n";next; }

# if database exists, create images...
foreach my $dev (@ITEMS) {
	($name=$dev) =~ s/\.rrd//g;
	my $description=$name;
	my $var;
	my $mode;
	my $command;

	$command = "/sbin/iwconfig $description";
	open (FILE,"$command|");
	while(<FILE>) {
		$var=$_;
		if ($var =~ /^.*Mode[=|:]+(.*)  Fre.*/ ) {
			$mode=$1;
		}
	}
	close(FILE);

	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) {
			if ($mode eq "Managed" ) {
				my ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b", "1024",
				"-s","-$range", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
				"-l 0", "--title", "Stats for $name in Managed mode - last $descr ($DATESTRING)",
				"DEF:rate=rrd/$dev:rate:AVERAGE",
				"DEF:quality=rrd/$dev:quality:AVERAGE",
				"DEF:signal=rrd/$dev:signal:AVERAGE",
				"DEF:noise=rrd/$dev:noise:AVERAGE",
				"LINE2:rate#000080:bitrate",
				"LINE2:quality#800000:link quality",
				"LINE2:signal#008000:signal level",
				"LINE2:noise#808080:noise level",
				"HRULE:0#000000";
				rename $TEMPNAME,$FILENAME;
				printf "  %-7s %d"."x%d %s\n",$descr,$xs,$ys,$FILENAME;
			} elsif ($mode eq "Ad-Hoc" ) {
				my ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b", "1024",
				"-s","-$range", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
				"-l 0", "--title", "Stats for $name in Ad-Hoc mode - last $descr ($DATESTRING)",
				"DEF:rate=rrd/$dev:rate:AVERAGE",
				"DEF:quality=rrd/$dev:quality:AVERAGE",
				"DEF:signal=rrd/$dev:signal:AVERAGE",
				"DEF:noise=rrd/$dev:noise:AVERAGE",
				"LINE2:rate#000080:bitrate",
				"LINE2:quality#800000:link quality",
				"LINE2:signal#008000:signal level",
				"LINE2:noise#808080:noise level",
				"HRULE:0#000000";
				rename $TEMPNAME,$FILENAME;
				printf "  %-7s %d"."x%d %s\n",$descr,$xs,$ys,$FILENAME;
			} elsif ($mode eq "Master" ) {
				my ($prints,$xs,$ys)=RRDs::graph $TEMPNAME, "-i", "-b", "1024",
				"-s","-$range", "-w",$CONFIG{"WIDTH"}, "-h",$CONFIG{"HEIGHT"}, "-a", uc($IMGFMT),
				"-l 0", "--title", "Stats for $dev in Master mode - last $descr ($DATESTRING)",
				"DEF:rate=rrd/$dev:rate:AVERAGE",
				"DEF:quality=rrd/$dev:quality:AVERAGE",
				"DEF:signal=rrd/$dev:signal:AVERAGE",
				"DEF:noise=rrd/$dev:noise:AVERAGE",
				"LINE2:rate#000080:bitrate",
				"LINE2:quality#800000:number of associated clients",
				"LINE2:signal#008000:SNR link 1",
				"LINE2:noise#808080:SNR link 2",
				"HRULE:0#000000";
				rename $TEMPNAME,$FILENAME;
				printf "  %-7s %d"."x%d %s\n",$descr,$xs,$ys,$FILENAME;
			}
		}
	}
}
print "\n";
exit 0;


