#! /usr/bin/perl
# -*- mode: Perl -*-
##################################################################
# MRTG 2.7.2  --- Index Generator
##################################################################
#
# This reads a mrtg.cfg file form std input or cmdline argument
# and it takes a regexp on the cmdline to specify which 
# targets to look at.
#
# from this info it produces a router index on std out.
#
##################################################################
# Created by Tobias Oetiker <oetiker@ee.ethz.ch>
# Massive patch from From andre@ml.ee Thu Feb 25 21:13:01 1999
# Distributed under the GNU copyleft
#
##################################################################

# Few defaults:
$regexp = ".";
$pagetop = 0;
$hruler = 0;
$columns = 2;
$sorted = 0;
$numbered = 0;
$WIDTH = 500;
$HEIGHT = 135;	# decent options: 135, 185, 235
$title = 'All targets';
$match = '.';
$nomatch = '';
$perfile = 0;
$showargs = 1;
$runArgz = '';
$graphs = 1;
$argz = join($",@ARGV);

sub ParseOptions {

while ($ARGV[0] =~ /^\-/) {
     if ($ARGV[0] eq '-t') {	shift @ARGV ; $title = $ARGV[0]; shift @ARGV}
  elsif ($ARGV[0] eq '-r') {	shift @ARGV ; $regexp = $ARGV[0]; shift @ARGV}
  elsif ($ARGV[0] eq '-b') {	shift @ARGV ; $bgcolor = $ARGV[0]; shift @ARGV}
  elsif ($ARGV[0] eq '-Y') {	shift @ARGV ; $HEIGHT = $ARGV[0]; shift @ARGV}
  elsif ($ARGV[0] eq '-X') {	shift @ARGV ; $WIDTH = $ARGV[0]; shift @ARGV}
  elsif ($ARGV[0] eq '-F') {	FlushPerFile() if (!$perfile); $perfile = 1; shift @ARGV}
  elsif ($ARGV[0] eq '-f') {	FlushPerFile() if ($perfile); $perfile = 0; shift @ARGV}
  elsif ($ARGV[0] eq '-P') {	$pagetop = 1; shift @ARGV}
  elsif ($ARGV[0] eq '-p') {	$pagetop = 0; shift @ARGV}
  elsif ($ARGV[0] eq '-H') {	$hruler = 1; shift @ARGV}
  elsif ($ARGV[0] eq '-h') {	$hruler = 0; shift @ARGV}
  elsif ($ARGV[0] eq '-1') {	$columns = 1; shift @ARGV}
  elsif ($ARGV[0] eq '-2') {	$columns = 2; shift @ARGV}
  elsif ($ARGV[0] eq '-S') {	$sorted = 1; shift @ARGV}
  elsif ($ARGV[0] eq '-s') {	$count = $sorted = 0; shift @ARGV}
  elsif ($ARGV[0] eq '-N') {	$numbered = 1; shift @ARGV}
  elsif ($ARGV[0] eq '-n') {	$number = $numbered = 0; shift @ARGV}
  elsif ($ARGV[0] eq '-G') {  $graphs = 1; shift @ARGV}
  elsif ($ARGV[0] eq '-g') {  $graphs = 0; shift @ARGV}
  elsif ($ARGV[0] eq '-A') {	$showargs = 1; shift @ARGV}
  elsif ($ARGV[0] eq '-a') {	$showargs = 0; shift @ARGV}
  elsif ($ARGV[0] eq '-M') {	shift @ARGV ; $regexp=$match = $ARGV[0]; shift @ARGV}
  elsif ($ARGV[0] eq '-m') {	shift @ARGV ; $nomatch = $ARGV[0]; shift @ARGV}

  else { print STDERR "Unknown option: $ARGV[0]\n" ; shift @ARGV }
}

 if ($title) {
   $Title = "$title Overview";
 } else {
   $Title = "Overview";
 }
 $Columns = $columns-1;
} # ParseOptions

if (! $ARGV[0]) { Usage(); }

sub Usage {
 die <<HELP;

USAGE: indexmaker [options] <mrtg.cfg> [options] <other.cfg> [options] .. ..

 This tool will read the contents of each mrtg.cfg file and will 
 return the HTML code of a webpage contanig the 'daily' graphs 
 of all the routers whose titles match the regular expression.

 Options:
  -r regexp : use regular expression 'regexp' to match targets (default: '$regexp')
  -t Title  : set 'Title' of index page (default: '$title')
  -b color  : set background color bgcolor (eg. -b #ffffff) (default: '$bgcolor')

  -1 : make 1-column graph page (default: $columns)
  -2 : make 2-column table page

  -P : set to parse PageTop tags for graph names in index page (default: $pagetop)
  -p : unset parsing of PageTop tags for graph names in index page

  -H : set use of <HR> tag between graphs. (default: $hruler)
  -h : unset use of <HR> tag between graphs.

  -S : sorted. present graphs in alphabetic order (default: $sorted)
  -s : unsorted. present graphs in order as they appear in config file

  -N : numbered. prepend sequence number for each graph.
  -n : not numbered. (default: $numbered)

  -X : specify WIDTH  for graphs (default: $WIDTH)
  -Y : specify HEIGHT for graphs (default: $HEIGHT)

  -M regexp : include only Targets whose Title match given regexp (same as -r)
  -m regexp : from those that pass -M exclude those that match -m regexp

  -A : add indexmaker arguments to a comment of generated html page. (default: $showargs)
  -a : do not. (use to memorise the command line used to generate this page)

  -G : add graphs to the index pages (default: $graphs)
  -g : don't add graphs to the index pages

  -F : try to apply all of the above in per config file manner.

 Options can appear between config filenames, thus allowing you to construct
 more complex and flexible rules for creating index files.
 You can use wildcards in filenames.
 You may find "-s -S" and "-n -N" useful between filenames.

EXAMPLE:  

indexmaker -t 'All my Routers' -r '.' conf/*cfg >/home/httpd/stats/index.html

You may also want to change defaults to suite your most frequent options.

HELP
}

sub FlushPerFile {
    WriteIndex();
    undef(%titles);
    undef(%dirs);
    CloseTable();
}

ParseOptions();

while ($ARGV[0]) {
  ParseOptions();
  Readcfg();
  Header();
  FlushPerFile() if ($perfile);
  shift @ARGV;
}

 FlushPerFile() if (!$perfile);

Footer();

if (! $success) { Usage(); }

sub Readcfg {
  #slurp the cfg file
  $last="1";
  if (!open(FILE,$ARGV[0]) ) {
     print STDERR "Can't open $ARGV[0]: $!\n" ; return; }
  $TargNR = '';
  while(<FILE>) {
    s/\t/ /g;  #replace tabs by spac
    next if /^\s+$/; #ignore white space
    next if /^\s*\#/; #ignore comments
    if (! /\[\^\]/ && ! /\[\$\]/ && /^title\[([^\]]+)\]:\s*(.*$regexp.*)/i) {
      $router=lc($1);
      $arg=$2;
      if ($arg =~ /$nomatch/i) { next }
      if (!$sorted && $router !~ /^$last$/) {
 	$count++;
	$TargNR = sprintf("%03d. ",$count); # we number the targets here to get unsorted output later
	$last = $router;
      }
      $titles{$router} = "$TargNR$arg";
      $dirs{$router} = $dirs{'^'} if ($dirs{'^'});
      $success=1; # we matched at least 1 target
      next;
    }
    if (/^directory\[([^\]]+)\]:\s*(.*\S)/i) {
      $arg = $2;
      $tmp = lc($1);
      $dirs{$tmp} = "$arg/";
    }
    if (/^icondir:\s*(.*\S)/i) {
      $icondir = "$1/";
      next;
    }
    if ($router && /^pagetop\[([^\]]+)\]:\s*(.*\S)/i) {
      next;
    }
    if ($pagetop && $router && /^\s+(.*?)<\/H1>/) {
      $titles{$router} .= " $1";
    }
    $router = '';
  }
} # Readcfg

sub Header {
 if (!$success) { return; }
 if (!$perfile && $headeronce) { return; }
 if (!defined(%titles)) { return; }
 if ($showargs) { $runArgz = "<!-- commandline was: indexmaker $argz -->\n"; }
 $BODY = "<BODY>";
 if ($bgcolor) { $BODY = "<BODY bgcolor=\"$bgcolor\">" }
print <<ECHO;
<HTML>
<HEAD>
<TITLE>$Title</TITLE>
</HEAD>
$BODY
ECHO

 if (!$headeronce) {
print $runArgz;
print <<'ECHO' if $ENV{USER} eq 'oetiker';
<TABLE BORDER=0 CELLSPACING=0 CELLPADING=0 WIDTH="100%">
  <TR><TD><A HREF="http://www.ethz.ch">
      <img border=0 alt="ETH: " src="/eth.199x32.gif"
        HEIGHT=32 WIDTH=199></A>
  </TD>
      <TD ALIGN=RIGHT>
      <A HREF="http://www.ethz.ch/">
        Swiss Federal Institute of Technology Zurich</A><BR>
      <A HREF="http://www.ee.ethz.ch/">
        Department of Electrical Engineering</A><BR>
  </TD>
  </TR>
</TABLE>
ECHO
}
 $headeronce++;
 $file='';
 $file = " ($ARGV[0])" if ($perfile);
 print "<H1>$Title$file</H1>";
 }
 
 sub WriteIndex {
  if ( ! defined(%titles)) { return; }
  OpenTable() if (!defined($TableSide));
  $TargNR = '';

foreach $router (sort {$titles{$a} cmp $titles{$b}} keys %titles) {
  $dirs{$router} = "" if (!defined($dirs{$router}));
  $rdir = $dirs{$router};
  $Title = $titles{$router};
  if (!$sorted) {
    $Title =~ s/^[0-9][0-9][0-9]\. //; # we must strip off temp numbering
  } 
  if ($numbered) {
    $number++;
    $TargNR = sprintf("%03d. ",$number);
    $Title = "$TargNR$Title"; 
  }
  
  if ( $Columns) {
    if ($TableSide eq "left" ) {
      $TableStart="<tr><td>";
      $TableStop ="$HR</td>";
      $TableSide="right";
    } else {
      $TableStart="<td>";
      $TableStop ="$HR</td></tr>";
      $TableSide="left";
    }
  }
  print <<ECHO;
$TableStart
 <P><B><A HREF="$rdir$router.html">$Title</B><P> 
    <SMALL><!--#flastmod file="$rdir$router.html" --></SMALL></P>
ECHO
 if ($graphs) {
 print <<ECHO;
	<IMG BORDER=0 WIDTH=$WIDTH HEIGHT=$HEIGHT SRC="$rdir$router-day.gif">
ECHO
}
  print "</A>$TableStop";
  print "<HR>" if ($hruler && !$Columns);
} # foreach
  
}  # Writeindex

sub OpenTable {
  $TableSide="left";

  if ($Columns) {
     print "<table border=0 width=\"100%\">\n";
     $HR="<HR>" if ($hruler);
  }
}

sub CloseTable {
  return if (!defined($TableSide));
  if ($Columns) {
    if ( $TableSide eq "right" ){
      print "<td></td></tr>\n";
    }
    print "</table>\n";
  }
  undef($TableSide);
}

$VERSION = "2.7.2";

sub Footer {
if (!$success) { return; }

print <<ECHO;
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
  <TR>
    <TD WIDTH=63><A ALT="MRTG"
    HREF="http://www.ee.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><IMG
    BORDER=0 SRC="${icondir}mrtg-l.gif"></A></TD>
    <TD WIDTH=25><A ALT=""
    HREF="http://www.ee.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><IMG
    BORDER=0 SRC="${icondir}mrtg-m.gif"></A></TD>
    <TD WIDTH=388><A ALT=""
    HREF="http://www.ee.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><IMG
    BORDER=0 SRC="${icondir}mrtg-r.gif"></A></TD>
  </TR>
</TABLE>
<SPACER TYPE=VERTICAL SIZE=4>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
  <TR VALIGN=top>
  <TD WIDTH=88 ALIGN=RIGHT><FONT FACE="Arial,Helvetica" SIZE=2>
  $VERSION</FONT></TD>
  <TD WIDTH=388 ALIGN=RIGHT><FONT FACE="Arial,Helvetica" SIZE=2>
  <A HREF="http://www.ee.ethz.ch/~oetiker">Tobias Oetiker</A>
  <A HREF="mailto:oetiker\@ee.ethz.ch">&lt;oetiker\@ee.ethz.ch&gt;</A> 
  and&nbsp;<A HREF="http://www.bungi.com">Dave&nbsp;Rand</A>&nbsp;<A HREF="mailto:dlr\@bungi.com">&lt;dlr\@bungi.com&gt;</A></FONT>
  </TD>
</TR>
</TABLE>
</BODY></HTML>
ECHO

}
