#!/usr/local/bin/perl -w
##---------------------------------------------------------------------------##
##  File:
##	$Id: mk-procmailrc,v 1.9 2002/03/11 20:28:11 ehood Exp $
##  Description:
##	Program to create a procmail recipe file from lists.txt.
##---------------------------------------------------------------------------##
##  Copyright (C) 2001-2002	Earl Hood <earl@earlhood.com>
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##  
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##  
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
##  02111-1307, USA
##---------------------------------------------------------------------------##

package MHArc::mk_procmailrc;

my $Dir;
BEGIN {
  $Dir = `dirname $0`; chomp $Dir;
}
use lib "$Dir/../lib";  # Add relative lib to search path

use MHArc::Config;
my $config = MHArc::Config->load("$Dir/../lib/config.sh");

use Getopt::Long;
use MHArc::ListDef;

MAIN: {
  my @htaccess = ();
  my $basedir = $config->{'SW_ROOT'} || "$Dir/..";
  my $procmail_path = $config->{'PROCMAIL_PATH'};

  GetOptions(

    'basedir', \$basedir,
    'procmail-path=s', \$procmail_path

  ) || die;

  ## Read lists definition file
  my $file = shift(@ARGV) ||
	     $config->{'LISTS_DEF_FILE'} ||
	     "$Dir/../lib/lists.def";
  my $listdef = MHArc::ListDef->new($file);

  ## Print procmailrc header
  print <<EOT;
##======================================================================
##	!!AUTO-CREATED, DO NOT EDIT!!
##	Procmail resource file for MHonArc archives
##======================================================================
##	This recipe is only responsible for storing messages within
##	mail folders.  A separate process will be used to generate
##	HTML archives.
##======================================================================

SHELL=/bin/sh
UMASK=133
PATH=$procmail_path
BASEDIR=$basedir

LOGFILE=\$BASEDIR/log/procmail.log

## Do alot of logging?
#VERBOSE=yes

## Should deliveries be logged?
LOGABSTRACT=yes

## Root path to mail folders
MBOXROOT=\$BASEDIR/mbox

## Current month: used as filename to store messages
MONTHFOLDER=`date +"%Y-%m"`

## Current year: used as filename to store messages
YEARFOLDER=`date +"%Y"`

## Flag if a list was matched
HAVEMATCH=no

## Avoid duplication messages
:0 Wh: \$BASEDIR/msgid.lock
| formail -D 8192 \$BASEDIR/msgid.cache

##======================================================================

EOT

  ## Print recipies
  my ($name,
      $cvs_prefix,
      $check_cvs,
      $addr,
      $period,
      $pm_conditions,
      $pm_copy,
      $folder_name);
  my (@addr);
  my ($str);

  foreach $name (sort keys %$listdef) {
    @addr = @{$listdef->{$name}{'address'}};
    if (!scalar(@addr)) {
      # no addresses defined
      warn qq/Warning: No addresses defined for '$name'\n/;
      next;
    }

    # create procmail regex for list
    $pm_conditions  = '* (^TO_';
    $pm_conditions .= '('  if (scalar(@addr) > 1);
    $pm_conditions .= join('|', @addr);
    $pm_conditions .= ')'  if (scalar(@addr) > 1);
    $pm_conditions .= '|(^(Delivered-To:|List-Post:).*';
    $pm_conditions .= '('  if (scalar(@addr) > 1);
    $pm_conditions .= join('|', @addr);
    $pm_conditions .= ')'  if (scalar(@addr) > 1);
    $pm_conditions .= '))';

    if (defined($listdef->{$name}{'procmail-condition'})) {
      foreach $str (@{$listdef->{$name}{'procmail-condition'}}) {
	$pm_conditions .= "\n$str";
      }
    }

    # check if doing monthly or yearly archives
    $period = lc($listdef->{$name}{'period'}) || 'month';
    $period = 'month'  if ($name eq '.catch');
    if ($period eq 'year') {
      $folder_name = '$YEARFOLDER';
    } else {
      $folder_name = '$MONTHFOLDER';
    }

    # check if rule should not be final if matched
    if ($listdef->{$name}{'final'}) {
      $pm_copy = '';
    } else {
      $pm_copy = ' c';
    }

    # check if separating out cvs commits
    if ($check_cvs = $listdef->{$name}{'cvs-commits'}[0]) {
      $cvs_prefix = $listdef->{$name}{'cvs-subject-prefix'}[0] || 'CVS commit';
      foreach $addr (@addr) {
	print <<EOT;
## $name (CVS)
:0
$pm_conditions
* ^Subject: $cvs_prefix
{
  :0 Wic
  * ? test ! -d \$MBOXROOT/$name.CVS
  | mkdir -m 755 -p \$MBOXROOT/$name.CVS

  :0:
  \$MBOXROOT/$name.CVS/$folder_name
}

EOT
      }
    } # End: $check_cvs

    # address receipe
    print <<EOT;
## $name
:0
$pm_conditions
{
  :0 Wic
  HAVEMATCH=|echo yes

  :0 Wic
  * ? test ! -d \$MBOXROOT/$name
  | mkdir -m 755 -p \$MBOXROOT/$name

  :0$pm_copy:
  \$MBOXROOT/$name/$folder_name
}

EOT
  }

  print <<'EOT';
##======================================================================
##	No Matches
##======================================================================
:0
* HAVEMATCH ?? no
{
  :0 Wic
  * ? test ! -d $MBOXROOT/.catch
  | mkdir -m 755 -p $MBOXROOT/.catch

  :0
  $MBOXROOT/.catch/$MONTHFOLDER
}

##======================================================================
##	Fallback (should not get here)
##======================================================================
:0
/dev/null
EOT

} # End: MAIN
