#!/usr/bin/perl
# -*- 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>
# Modifications by Laurie Gellatly <gellatly@one.net.au>
# Distributed under the GNU copyleft
#
# $Id: index,v 1.1.1.1 2002/02/26 10:16:36 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: index <mrtg.cfg>  <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 = '';
}

$Today=datestr(time);
print <<ECHO;

$Title

$Title

These statistics were last updated $Today 

ECHO

foreach $router (sort {$titles{$a} cmp $titles{$b}} keys %titles) {
  $dirs{$router} = "" if (!defined($dirs{$router}));
  $rdir = $dirs{$router};
  print <<ECHO;

$titles{$router}

ECHO
}

'$Revision: 1.1.1.1 $ ' =~ /Revision: (\S*)/;
$rev=$1;
'$Date: 2002/02/26 10:16:36 $ ' =~ /Date: (\S*)/;
$date=$1;

print <<ECHO;

  $rev-$date
   
  Tobias Oetiker (http://www.ee.ethz.ch/~oetiker)
  <oetiker\@ee.ethz.ch> (mailto:oetiker\@ee.ethz.ch) 
  and Dave Rand (http://www.bungi.com) <dlr\@bungi.com> (mailto:dlr\@bungi.com)
  Back to Main Router List

ECHO

sub datestr {
  my ($time) = shift(@_) || return 0;
  my ($wday) = ('Sunday','Monday','Tuesday','Wednesday',
                'Thursday','Friday','Saturday')[(localtime($time))[6]];
  my ($month) = ('January','February' ,'March' ,'April' ,
                 'May' , 'June' , 'July' , 'August' , 'September' , 
                 'October' ,
                 'November' , 'December' )[(localtime($time))[4]];
  my ($mday,$year,$hour,$min) = (localtime($time))[3,5,2,1];
  if ($min<10) {$min = "0$min";}
  return "$wday, $mday $month ".($year+1900)." at $hour:$min";
}

