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

use lib "../../lib";
use HotSaNICparser;

# read global settings
#
$MODNAME=lc HotSaNICparser::get_module_name();

%CONFIG=HotSaNICparser::get_config("../..");
$IMAGEFORMAT=$CONFIG{"IMAGEFORMAT"};
$REFRESH=$CONFIG{"REFRESH"};
$REFRESH=0 if !defined $REFRESH;

# read module-specific settings
#
undef %DESCR;
foreach (HotSaNICparser::read_settings(".")) {
  ($var,$value)=HotSaNICparser::parse_line($_);
  if ($var eq "DEVEXT") {
    ($dev,$descr)=split(/,/,$value);
    $devname=$dev;
    $dev =~ s/\//_/g;
    $dev =~ s/! /!/g;
    $DESCR{"ext$dev"}=$descr;
    $DEVNAME{"ext$dev"}=$devname;
    next;
    }
  if ($var eq "DEVINT") {
    ($dev,$descr)=split(/,/,$value);
    $devname=$dev;
    $dev =~ s/\//_/g;
    $dev =~ s/! /!/g;
    $DESCR{"int$dev"}=$descr;
    $DEVNAME{"int$dev"}=$devname;
    next;
    }
  }
@DIAGRAMS=("hour","6h","day","week","month","year");
@TIMES=("6h","week");

# build time-based .html files
#
foreach $time (@TIMES) {
  open (FILE,">index/$time.html");
  print FILE "<html>\n";
  if ($REFRESH > 0) {
    print FILE "<META HTTP-EQUIV=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<META HTTP-EQUIV=\"Refresh\" CONTENT=\"$REFRESH\">
<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n";
    }
  print FILE "<body>\n<table>\n";
  foreach $dev (sort keys %DESCR) {
    $descr=$DESCR{$dev};
    $devname=$DEVNAME{$dev};
    print FILE "  <tr><td><a href=\"",$dev,".html\"><img src=\"",$dev,"-",$time,".",$IMAGEFORMAT,"\"></a></td>
      <td valign=top><a href=\"",$dev,".html\"><h2>",$devname,"</h2></a><br>",$descr,"</td></tr>\n";
    }
  print FILE "</table>\n</body>\n</html>\n";
  close FILE;
  }

# build device-based .html files and complete index
#
open (IDXFILE,">idxdata");
print IDXFILE "0##    <td colspan=2 align=center valign=top> <font size=\"+3\">",$MODNAME,"</font><br>
0##      <a href=\"",$MODNAME,"/6h.html\">6 hours</a>
0##      <a href=\"",$MODNAME,"/week.html\">week</a><br>
0##      <hr width=90%>
0##      </td>\n";
$nn=0;
foreach $dev (sort keys %DESCR) {
  $nn++;
  $descr=$DESCR{$dev};
  $devname=$DEVNAME{$dev};
  print IDXFILE $nn,"##    <td><a href=\"",$MODNAME,"/",$dev,".html\"><img src=\"",$MODNAME,"/thumb-",$dev,".",$IMAGEFORMAT,"\"></a></td>
",$nn,"##    <td valign=top><a href=\"",$MODNAME,"/",$dev,".html\">",$devname,"</a><br><font size=\"-2\">",$descr,"</font></td>\n";

# build device-based .html
#
  open (FILE,">index/$dev.html");
  print FILE "<html>\n";
  if ($REFRESH > 0) {
    print FILE "<META HTTP-EQUIV=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<META HTTP-EQUIV=\"Refresh\" CONTENT=\"$REFRESH\">
<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n";
    }
  print FILE "<body>\n";
  foreach $diagram (@DIAGRAMS) {
    print FILE "<img src=\"",$dev,"-",$diagram,".",$IMAGEFORMAT,"\">\n";
    }
  print FILE "</body>\n</html>\n";
  close FILE;
  }

close IDXFILE;

