#!/usr/ug/bin/perl5 -w
#           vim:fo=croql:cin:com=\:#
# 
# Drops local logs in ~/gale of the running user; each puff is copied
# to multiple places:
#   ~/gale/by-date/YYYY-MM-DD -> based on the timestamp
#   ~/gale/by-sender/USER@DOMAIN -> based on the sender
#   ~/gale/by-prefix/TARGET@DOMAIN -> for each component of each
#        destination address; for example, a message cross posted to
#        pub.this@ofb.net and pub.that@ofb.net would go in this
#        directory in the following files:
#          pub@ofb.net, pub.this@ofb.net, pub.that@ofb.net
# example use:
#       GALE_NAME="jtr's logger" gsub -akef ~/.gale/gsubrcs/logall \
#         pub@ofb.net local@ofb.net ugcs@ugcs.caltech.edu cvs@gale.org \
#         new@gale.org test@ofb.net >>& ~/gale/:errs&
#
# jtr@ofb.net

use lib '/usr/lib/perl';  # no idea what this is for -- jtr 7/6/01
use strict;
use POSIX qw(strftime);



#
# global variables out of spite
# 

use vars qw(@CATS @KEYWORDS $SENDER $FTIME @FORMPUFF);
use vars qw($LOGDIR %LOGGEDTO);


#
# initin de globals
# 

$LOGDIR= $ENV{'HOME'} . "/gale/";
@CATS= &getFragments('GALE_TO');
$SENDER= $ENV{'GALE_FROM'};
$FTIME= $ENV{'GALE_TIME_ID_TIME'};
@FORMPUFF= &getFormattedPuff();
@KEYWORDS= &getFragments('GALE_TEXT_MESSAGE_KEYWORD');



#
# dealin wit de nolog and de so-forth
# 

my $i;
foreach $i (@KEYWORDS) {
  exit if $i =~ m-nolog-;
}
foreach $i (@FORMPUFF) {
  exit if ($i =~ /jj.?232/i);
}
foreach $i (@CATS) {
  exit if ($i =~ /^_gale.(key|query)/);
}







#
# main handler
#


my $oobflag= 1;
&logit;

foreach $i (@CATS) {
    $oobflag= 0
      if ($i !~ m-@(printf.net|dfmm.org)$-);
#if ($i !~ m-^(gate.web|bot|(@[^/]*/web|@[^/]*/bot|cmd|test|spam|@[^/]*/server|/))-);
    $i =~ s-/-:-g;
    my $pref= "";
    my $part;
    my ($id, $domain)= split('@', $i);
    foreach $part (split(m-[.]-, $id)) {
        $pref .= $part;
        &logto("by-prefix/$pref\@$domain") if $pref;
        $pref .= '.';
    }
    &logto("by-category/$i");
}

foreach $i (@KEYWORDS) {
    $i =~ s-/-:-g;
    my $pref= "";
    my $part;
    foreach $part (split(m-[.]-, $i)) {
      $pref .= $part;
      &logto("by-keyword/$pref") if $pref;
      $pref .= '.';
    }
}

&logto("by-sender/$SENDER")
    if (defined($SENDER) && length($SENDER) > 0);

&logto("by-date/".substr($FTIME, 0, 10))
    if (defined($FTIME) && length($FTIME) > 10 && !$oobflag);






# 
# hook to log information about recent messages
#

sub logit {
  print STDERR ": puff to ", join(", ", @CATS), "\n";
}




#
# nyurgle vah
#

sub logto {
    my($logfn)= @_;
    return if defined($LOGGEDTO{$logfn});
    print STDERR $logfn, "\n";
    $LOGGEDTO{$logfn}= 1;
    open(LOGF, ">>$LOGDIR$logfn") ||
        die "couldn't open $LOGDIR$logfn";
    my $line;
    foreach $line (@FORMPUFF) {
      print LOGF $line;
    }
    close(LOGF);
}


#
# Consumes standard input; must be called before anything else tries
# to.
#

sub getFormattedPuff {
  open(GSUBR, "gsub -r |") || die;
  my @pufflines= <GSUBR>;
  if ($#pufflines >= 0) {
    if (defined($FTIME)) {
      my $x= $pufflines[$#pufflines] =~
        s/at \d\d-\d\d \d\d:\d\d:\d\d --/$FTIME --/;
      if ($x > 0) {
        $pufflines[$#pufflines] =~ s/^\s\s//;
      }
    }
  }
  return @pufflines;
}



#
# If multiple values for a variable GALE_xx exist, gsub puts the first
# in GALE_xx, the second in GALE_xx_2, the third in GALE_xx_3, and so
# on.  This function, given the string GALE_xx, would collect the
# values of all of the appropriate variables and return them as a
# list.
#

sub getFragments {
  my($fragname)= @_;
  my($curfrag, $counter, @frags)= ($fragname, 1, ());
  while (defined($ENV{$curfrag})) {
    push(@frags, $ENV{$curfrag});
    $curfrag= $fragname . "_" . ++$counter;
  }
  return @frags;
}

