#!/usr/local/gnu/bin/perl5
# -*- mode: Perl -*-
##################################################################
# Multi Router Traffic Grapher: 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>
# Distributed under the GNU copyleft
#
# $Id: indexmaker,v 2.2 1997/06/12 08:18:29 oetiker Exp $
##################################################################


$regexp = pop @ARGV;
$Title = pop @ARGV;
if (defined($Title) && -f $Title) {
 push(@ARGV, $Title);
 $Title = "$Title Overview ($regexp)";
}
if (! -f $ARGV[0]) {
 print "

USAGE: indexmaker <mrtg.cfg> <title> <regular expression>

This tool will read the contents of the 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.

NOTE: you have to adjust this tool to your needs as the HTML 
      contains our logo and our name ...

";

 exit ;

}

#slurp the cfg file
while(<>) {
  s/\t/ /g;  #replace tabs by spac
  next if /^\s+$/; #ignore white space
  next if /^\s*\#/; #ignore comments
  if (/$regexp/i && ! /\[\^\]/ && ! /\[\$\]/ && /^title\[([^\]]+)\]:\s*(.*\S)/i) {
    $router=lc($1);
    $arg=$2;
    $titles{$router} = $arg;   
    next;
  }
  if (/^directory\[([^\]]+)\]:\s*(.*\S)/i) {
    $arg = $2;
    $tmp = lc($1);
    $dirs{$tmp} = "$arg/";
  }
  if ($router && /^pagetop\[([^\]]+)\]:\s*(.*\S)/i) {
    next;
  }
  if ($router && /^\s+(.*?)<\/H1>/) {
    $titles{$router} .= " $1";
  }
  $router = '';
}

print <<ECHO;
<HTML>
<HEAD>
<TITLE>$Title</TITLE>
</HEAD>
<BODY bgcolor=#ffffff>

<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>

<H1>$Title</H1>
ECHO

foreach $router (sort {$titles{$a} cmp $titles{$b}} keys %titles) {
  $dirs{$router} = "" if (!defined($dirs{$router}));
  $rdir = $dirs{$router};
  print <<ECHO;
<P><B><A HREF="$rdir$router.html">$titles{$router}</B><P> 
   <SMALL><!--#flastmod file="$rdir$router.html" --></SMALL></P>
  <IMG BORDER=0 WIDTH=500 HEIGHT=135 SRC="$rdir$router-day.gif"></A>
  <HR>
ECHO
}

'$Revision: 2.2 $ ' =~ /Revision: (\S*)/;
$rev=$1;
'$Date: 1997/06/12 08:18:29 $ ' =~ /Date: (\S*)/;
$date=$1;

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="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="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="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>
  $rev-$date</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




