#!/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
#

# evaluate number of CPUs
#
opendir DIR,"rrd";
@DBs = grep /^[^.].*/, readdir DIR;
closedir DIR;
foreach (@DBs) { chomp; s/\.rrd//g;
  push @db,$_;
  if (/mem/) { s/mem/swap/g; push @db,$_; }
  }

  @db=sort(@db);

# build time-based .html files
#
foreach $index ("hour","week") {
  open (FILE,">index/$index.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";
 
  for (@db) {
    ($link_name = $_) =~ s/_/:/;
    print FILE "<tr><td><a href=\"$_.html\"><img src=\"$_-",$index,".",$IMAGEFORMAT,"\"></a></td>\n
    <td valign=top><a href=\"proc.html\"><h2>$link_name</h2></a></td></tr>\n";
  }

  print FILE "</table>\n</body>\n</html>\n";
  close FILE;
  }

$count = 0;
for (@db) { $count += 1 if /cpu/; }

open (IDXFILE,">idxdata");
if ($count>1) { print IDXFILE "$count\n"; }

print IDXFILE "0##    <td colspan=".2*$count." align=center> <font size=\"+3\">",$MODNAME,"</font><br>
0##      <a href=\"",$MODNAME,"/hour.html\">hour</a>
0##      <a href=\"",$MODNAME,"/week.html\">week</a><br>
0##      <hr width=90%>
0##      </td>";

@res = grep /proc/, @db;
for (@res) {
       ($link_name = $_) =~ s/_/:/;
print IDXFILE "
1##    <td><a href=\"",$MODNAME,"/$_.html\"><img src=\"system/thumb-$_.",$IMAGEFORMAT,"\"></a></td>
1##    <td valign=top><a href=\"",$MODNAME,"/$_.html\">$link_name</a></td>";
}
$num=@res;
for ($nn=$num;$nn<$count;$nn++) { print IDXFILE "\n1##    <td></td><td></td>";}

@res = grep /cpu/, @db;
for (@res) {
       ($link_name = $_) =~ s/_/:/;
print IDXFILE "
2##    <td><a href=\"",$MODNAME,"/$_.html\"><img src=\"system/thumb-$_.",$IMAGEFORMAT,"\"></a></td>
2##    <td valign=top><a href=\"",$MODNAME,"/$_.html\">$link_name</a></td>";
}
$num=@res;
for ($nn=$num;$nn<$count;$nn++) { print IDXFILE "\n2##    <td></td><td></td>";}

@res = grep /load/, @db;
for (@res) {
       ($link_name = $_) =~ s/_/:/;
print IDXFILE "
3##    <td><a href=\"",$MODNAME,"/$_.html\"><img src=\"system/thumb-$_.",$IMAGEFORMAT,"\"></a></td>
3##    <td valign=top><a href=\"",$MODNAME,"/$_.html\">$link_name</a></td>";
}
$num=@res;
for ($nn=$num;$nn<$count;$nn++) { print IDXFILE "\n3##    <td></td><td></td>";}

@res = grep /mem/, @db;
for (@res) {
       ($link_name = $_) =~ s/_/:/;
print IDXFILE "
4##    <td><a href=\"",$MODNAME,"/$_.html\"><img src=\"system/thumb-$_.",$IMAGEFORMAT,"\"></a></td>
4##    <td valign=top><a href=\"",$MODNAME,"/$_.html\">$link_name</a></td>";
}
$num=@res;
for ($nn=$num;$nn<$count;$nn++) { print IDXFILE "\n4##    <td></td><td></td>";}

@res = grep /swap/, @db;
for (@res) {
       ($link_name = $_) =~ s/_/:/;
print IDXFILE "
5##    <td><a href=\"",$MODNAME,"/$_.html\"><img src=\"system/thumb-$_.",$IMAGEFORMAT,"\"></a></td>
5##    <td valign=top><a href=\"",$MODNAME,"/$_.html\">$link_name</a></td>";
}
$num=@res;
for ($nn=$num;$nn<$count;$nn++) { print IDXFILE "\n5##    <td></td><td></td>";}

@res = grep /users/, @db;
for (@res) {
       ($link_name = $_) =~ s/_/:/;
print IDXFILE "
6##    <td><a href=\"",$MODNAME,"/$_.html\"><img src=\"system/thumb-$_.",$IMAGEFORMAT,"\"></a></td>
6##    <td valign=top><a href=\"",$MODNAME,"/$_.html\">$link_name</a></td>";
}
$num=@res;
for ($nn=$num;$nn<$count;$nn++) { print IDXFILE "\n6##    <td></td><td></td>";}

@res = grep /irq/, @db;
for (@res) {
print IDXFILE "
7##    <td><a href=\"",$MODNAME,"/$_.html\"><img src=\"system/thumb-$_.",$IMAGEFORMAT,"\"></a></td>
7##    <td valign=top><a href=\"",$MODNAME,"/$_.html\">interrupts</a></td>";
}
$num=@res;
for ($nn=$num;$nn<$count;$nn++) { print IDXFILE "\n7##    <td></td><td></td>";}

@res = grep /uptime/, @db;
for (@res) {
print IDXFILE "
8##    <td><a href=\"",$MODNAME,"/$_.html\"><img src=\"system/thumb-$_.",$IMAGEFORMAT,"\"></a></td>
8##    <td valign=top><a href=\"",$MODNAME,"/$_.html\">uptime</a></td>";
}
$num=@res;
for ($nn=$num;$nn<$count;$nn++) { print IDXFILE "\n8##    <td></td><td></td>";}

close IDXFILE;

foreach $index (@db) {
  print "writing $index\n";
  open (FILE,">index/$index.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";
  if ( index ($index,"uptime") <0 ) { print FILE "<img src=\"",$index,"-hour.",$IMAGEFORMAT,"\">\n"; }
  print FILE "<img src=\"",$index,"-day.",$IMAGEFORMAT,"\">\n";
  print FILE "<img src=\"",$index,"-week.",$IMAGEFORMAT,"\">\n";
  print FILE "<img src=\"",$index,"-month.",$IMAGEFORMAT,"\">\n";
  print FILE "<img src=\"",$index,"-year.",$IMAGEFORMAT,"\">\n</body>\n</html>\n";
  close FILE;
  }

