#! /usr/bin/perl

# This small script adds a selection of topics
#  in the stats generated by IRCstats
# WARNING : This is very sensitive to ircstats version.

# (c) 2001 - Rémi Peyronnet, remi@via.ecp.fr, http://www.via.ecp.fr/~remi

# La version shell est beaucoup plus... rapide :-)

$optIrclog = ".irclog";
$optIrcstatsFile = "index.html";
$optOut = "topics.html";
$optNameChannel = "TOPIC #c00lz";
$optPutAfter = "<!-- User defined footer begins -->";
$optTitle = "Topics selection";

# Retrieves the Topics
open (IRCLOG,$optIrclog) or die "Unable to open $optIrclog";
while (<IRCLOG>)
{
  chomp;
  if (m/$optNameChannel :(.*)/) 
   {
    $li = $1;
    $li =~ s/</&lt;/g;
    $li =~ s/>/&gt;/g;
    push @topics, $li; 
   }
}
close (IRCLOG);

# Choose 10 randoms topics
for (1 .. 10) 
{
  $nbtopics=@topics;
  push @sel, splice (@topics, rand $nbtopics, 1);
}

# Write the output file
open (OUT, ">".$optOut) or die "Unable to open $optOut";
open (IRCFILE, $optIrcstatsFile) or die "Unable to open $optIrcstatsFile";

while (<IRCFILE>)
{
 # Translate http://.. in <a href=""></a>
 # May cause little problems if http:// is required in the html file...
 # Just leaves the first line to remove this silly conversion
 $li = $_;
 #$li =~ s/(http:\/\/[^"<, ]*)/<a href="$1">$1<\/a>$2/ig;
 # This is removed : it is not the right place to do that :
 #  do it before calling IRCstats.
 print OUT $li;
 if (m/$optPutAfter/i)
 {
   # These ugly tags are taken from the original IRCstats...
   print OUT '<hr><font color="#F8FCD0" size="4" face="Verdana"><i>'.$optTitle.'</i></font><br>';
   print OUT '<table nowrap border="0" cellpadding="2" cellspacing="3" width="90%"><tr><td bgcolor="#A0AAAA"><font color="#102018" size="2" face="Verdana">\n';
   print OUT join '</font></td></tr><tr><td bgcolor="#A0AAAA"><font color="#102018" size="2" face="Verdana">', @sel;
   print OUT "</font></td></tr></table><br><br>\n";
 }
}

close (IRCFILE);
close (OUT);
