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

#
# $Id: makeindex.pl,v 1.10 2003/07/12 20:57:27 bernisys Exp $
#
# use Getopt::Long;
use FindBin;
use lib "./lib";
use lib $FindBin::RealBin;
use HotSaNICparser;

# let all outputs be flushed directly
$|=1;

(my $CALLDIR=$FindBin::RealBin) =~ s/\/rrdtimer//g;
(my $VERSION = '$Revision: 1.10 $') =~ s/.*(\d+\.\d+).*/$1/;

my %CONFIG=HotSaNICparser::get_config($CALLDIR);


my $DAEMONDIR=$CONFIG{"DAEMONDIR"};
my $WEBDIR=$CONFIG{"WEBDIR"};
my $ORDER=$CONFIG{"ORDER"};
my $SHOW=$CONFIG{"SHOW"};
my $CONVERTMETHOD=$CONFIG{"CONVERTMETHOD"};
my $THUMBSIZE=$CONFIG{"THUMBSIZE"};
my $WIDTH=$CONFIG{"WIDTH"};
$THUMBSIZE=~s/%//g;
$THUMBSIZE=~ s/,/\./g;
$THUMBSIZE=int($THUMBSIZE*$WIDTH/100);

my @modules=HotSaNICparser::scan_for_modules("$DAEMONDIR/modules",-1,$SHOW);

my (@order,$subdirs,@ITEMS,$item,%count,%lines,$dir,$MAXLINES,$num,$value,$modname,$i,$out);

# create webdir if necessary
#
if ( ! -e $WEBDIR ) {
  print "creating HTTP output directory:\n   $WEBDIR\n";
  umask 000;
  mkdir $WEBDIR,0755 or die "ERROR: $!\n\n";
  }

@order=split(/ /,$ORDER);

# first add all existing modules in the order they are listed
# in the "ORDER" config-string
#
$subdirs=join(" ",@modules);
foreach $item (@order) {
  if (index($subdirs,$item) >=0) { push @ITEMS,$item; }
  }

# then add all not ordered but wanted modules in alphabetical order
#
$subdirs=join(" ",@ITEMS);
foreach $item (@modules) {
  if (index($subdirs,$item) <0) { push @ITEMS,$item; }
  }

$MAXLINES=0;
print "generating index-files for each plugin...\n";

foreach $modname (sort @ITEMS) {
  my $subdir="$DAEMONDIR/modules/$modname";
  print $modname,"\n";
  $count{$modname}=1;
  if ( ! -e "$subdir/index" ) { mkdir "$subdir/index",0755; }
  if ( -e "$subdir/makeindex.pl") { system("cd \"$subdir\"; ./makeindex.pl >/dev/null"); }
  elsif ( -e "$subdir/makeindex") { system("cd \"$subdir\"; ./makeindex >/dev/null"); }
  open FILE,"$subdir/idxdata";
  while (<FILE>) {
    ($num,$value)=split(/##/,$_);
    if ($MAXLINES < $num) { $MAXLINES=$num; }
    if ( (defined $value) && ($value ne "") ) { push @{ $lines{$modname} },"$_"; } elsif ($num ne "") { $count{$modname} = $num; }
    }
  close FILE;
  unlink "$subdir/idxdata";

  # create module-subdir if necessary and copy the module's
  # html files there.
  # (this may vanish in future because it could also be
  # handled by each module itself!)
  #
  if ( ! -e "$WEBDIR/$modname" ) {
    print "  creating directory $WEBDIR/$modname\n";
    mkdir "$WEBDIR/$modname",0755 or die "ERROR: $!\n";
    }
  print "  moving index-files to webdir\n";
  system ("mv -f \"$subdir/index/\"* \"$WEBDIR/$modname/\"");
  print "\n";
  }
  
print "\nbuilding $WEBDIR/index.html ...\n";

if ( ! -e "$WEBDIR/rrdtool.gif" ) { system("cp -f images/rrdtool.gif $WEBDIR"); }
if ( ! -e "$WEBDIR/HotSaNIC.gif" ) { system("cp -f images/HotSaNIC.gif $WEBDIR"); }

open INDEXFILE,">$WEBDIR/index.html";
print INDEXFILE "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">
<html>
<HEAD>
  <META NAME=\"Author\" CONTENT=\"Bernd Pissny\">
  <META NAME=\"GENERATOR\" CONTENT=\"perl-script\">
  <TITLE>System statistics</TITLE>
  </HEAD>
<body>
<center>
<table border=0>\n";

print "table-size: ",$MAXLINES+1," lines.\n\n";
print "Modules are added in this order:\n";

# scan array from 0 to $MAXLINES to create the table lines.
# if no line was found corrosponding to actual line-number
# then output a table row with no entries
for ($i=0; $i<=$MAXLINES; $i++) {
  print INDEXFILE "  <tr valign=top>\n";
  foreach $modname (@ITEMS) {
    if ($i == 0) { print $modname,"\n"; }
    $out=0;
    foreach (@{ $lines{$modname} }) {
      ($num,$value)=split(/##/);
      if ($num == $i) {
        if ($CONVERTMETHOD eq "HTML") {
          $value=~ s/thumb-//g;
          $value=~ s/.gif"/-week.gif" width="$THUMBSIZE"/g;
          $value=~ s/.png"/-week.png" width="$THUMBSIZE"/g;
          }
        print INDEXFILE $value; $out=1;
        }
      }
    if ( $out == 0 ) { for (my $nn=1; $nn<=$count{$modname}; $nn++) { print INDEXFILE "    <td></td><td></td>\n"; } }
    }
  print INDEXFILE "  </tr>\n";
  }
  print INDEXFILE "</table><br><hr><br>
<a href=\"http://hotsanic.sourceforge.net\"><img src=\"HotSaNIC.gif\" width=97 height=34 alt=\"HotSaNIC\" border=0></a>&nbsp;<a href=\"http://ee-staff.ethz.ch/~oetiker/webtools/rrdtool/\"><img src=\"rrdtool.gif\" width=120 height=34 alt=\"rrdtool\" border=0></a><br>
HTML overview to System and Network Information Center
</center>
</body>
</html>\n";

close INDEXFILE;

print "\ndone.\n\n";

