#!/local/bin/perl
#          
# Written by: Ove Ruben R Olsen (ruben@uib.no) 
#
################################################################################
#
# Copyright (c) Ove Ruben R Olsen (Gnarfer from hell)
#
# Permission to use, copy, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation.
# This also apply your own incarnations of any or all of this code.
# If you use portions of this code, you MUST include a apropriate notice
# where you "stole" it from.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Email:  Ruben@uib.no 
#
#
# This work is free software, but if you find it usefull I would appreciate
# if you donated aprox 10 USD to your country's cancer research.
#
#
# This work is entierly dedicated to Johanne B Revheim
#
################################################################################
# Alter this lines to suit your site.
#

$IRCDIR  = "/local/irc" ;		# Your IRC directory
$logfile = "$IRCDIR/userlog" ;		# Your userlogfil.
$ddfil   = "$IRCDIR/ircstat.idf" ;	# Your domain description file.


#
# There is no need going beyond this line unless you are very eager to
# debug and destroy this work :-)
################################################################################
################################################################################

$Date    = " Sat Jan 21 19:35:11 GMT 1995.";
$Version = "1.00-000" ;
$ZCAT    = "zcat" ;
$GZIP    = "gzip -cd";

while ($i = shift) {
  $ddfil = shift, next if ("-d" eq $i) ;
  push (@logfil,$i), next if ($i !~ /^\-/) ;
  push (@logfil,shift(ARGV)), next if ("-l" eq $i)  ;
  $STDIN  = 1  if ($i eq "-i");

  if ("-h" eq $i) {
    print (STDERR <<SLUTT);

Usage: newdoms [options] [logfile] ... [logfile]
 Options:
   -d <IDFile>    Uses IDFile as an alternative Ircstat Description file.
   -l <logfile>   Uses logfile instead of default logfile.
   -i             Reads from STDIN.

 Logfiles:
   Logfiles is for the lazy ones who do not want to use the -l option.

SLUTT
  exit ;
  }
  
}

push (@logfil,$logfile) unless @logfil;

print STDERR "\nThis is NEWDOMAINS version $Version - $Date.\n",
        "Written by Ove Ruben R Olsen - Copyright (C) 1995.\n\n" ; 

open (FIL, $ddfil) || warn "\nCannot open IDF file $ddfil.\n";
while (<FIL>) { &IDF() ; } close (FIL);

if ($STDIN) {
  print STDERR "Using STDIN as logfile(s).\n" ;
  &LOG ();
} else {
  print STDERR "Using @logfile as logfile(s).\n" ;
  foreach (@logfil) { &LOG ($_) ; }
}


print "# New domains found:\n\n";
foreach (keys %funnet) { print "dt $_\$\t\t$_\t\tfar\n"; }
print "\n\n";
foreach (keys %funnet) { print "di $_\n"; }

################################################################################


sub LOG {
  $ofil = @_[0];
  if ($STDIN == 0) {
    if (! (-e $ofil && -R $ofil ))  {
       print STDERR "Cannot open userlog file: $ofil\n";
       return  ;
    }
    $ofil = "$ZCAT $ofil |" if ($ofil =~ /.Z$/);
    $ofil = "$GZIP $ofil |" if ($ofil =~ /.gz$|.z$/);
    open(FILEN, $ofil) ; 
  } else {
    open (FILEN, "-");
  }

  print STDERR "Processing @_[0]: " unless $stille ;

  while (<FILEN>) {
    print STDERR "." if ( $. % 250  ==  0 )  ;
    $_ =~ /(\w+.\w+)\s+\[\w+\]$/;
    next if ($domains{$1});
    next if ($funnet{$1});
    $funnet{$1} = 1 ;
  }
  print STDERR "\n";

}


################################################################################

sub IDF {
  if ($_ =~  /^dt/) {
    ($ju,$ju,$dom) = split (/\s+/);
    next if ($dom eq "");
    $domains{$dom} = 1 ;
  }
}
################################################################################
