#!/usr/local/bin/perl
##--------------------------------------------------------------------------##
##  File:
##	$Id: read-mail,v 1.10 2002/09/15 03:33:08 ehood Exp $
##  Description:
##	Read mail spool and archive messages.
##--------------------------------------------------------------------------##
##  Copyright (C) 2001	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::read_mail;

##--------------------------------------------------------------------------##
# <x-boot-strap>
BEGIN { die qq/CGI use FORBIDDEN!\n/ if (defined($ENV{'GATEWAY_INTERFACE'})); }
my $Dir; BEGIN { $Dir = `dirname $0`; chomp $Dir; }
use lib "$Dir/../lib";  # Add relative lib to search path
# </x-boot-strap>
##--------------------------------------------------------------------------##
# <x-config>
use MHArc::Config;
my $config = MHArc::Config->load("$Dir/../lib/config.sh");
# </x-config>
##--------------------------------------------------------------------------##


use Getopt::Long;
use MHArc::Util qw( ch_dir cmd usage );

MAIN: {
  # Grap command-line options
  my %opt = ( );
  my $clstatus = GetOptions(\%opt,
    'force!',
    'home=s',
    'verbose!',

    'help',
    'man',
  );
  usage(0) unless $clstatus;
  usage(1) if $opt{'help'};
  usage(2) if $opt{'man'};

  my $verbose = $opt{'verbose'};
  if ($verbose) {
    $MHArc::Util::ECHO_CMDS = 1;
  }

  my $home = $opt{'home'} ||
		$config->{'SW_ROOT'} ||
		"$Dir/..";
  my $force = $opt{'force'};

  ch_dir($home) ||
      die qq/ERROR: Unable to change directory to "$home": $!\n/;

  if (-e '.noarchive') {
    print "Archiving is currently disabled\n"  if $verbose;
    if (!$force) {
      exit 0;
    }
    print "However, -force specified, so continuing...\n"  if $verbose;
  }

  my @cmd_args = ();
  push(@cmd_args, '-verbose')  if $verbose;
  if (cmd('./bin/filter-spool', @cmd_args) == 0) {
    cmd('./bin/web-archive', @cmd_args);
  }
}

##---------------------------------------------------------------------------##
__END__

=head1 NAME

read-mail - Archive incoming mail

=head1 SYNOPSIS

  read-mail
  read-mail [options]

=head1 DESCRIPTION

This program is part of mharc and is the main program for archiving
mail.  It is generally called via cron.

This program does very little itself, but it invokes other mharc
scripts to do all the work.

If the file C<.noarchive> exists in mharc root directory, then this
program will do nothing and exit (unless the C<-force> option is
specified).  This is to allow one to disable incoming mail processing
while performing administrative tasks.

=head1 OPTIONS

This program is generally called without any command-line options
since it will read C<E<lt>mharc-rootE<gt>/lib/config.sh> for any configurable
options.  Regardless, the following command-line options are
available:

=over

=item C<-help>

Print out usage information.

=item C<-home> I<pathname>

Root pathname of archiving software and data.  If not specified,
C<SW_ROOT> variable in C<config.sh> is used, else the parent directory
that contains this program is used.

=item C<-man>

Print out entire manpage.

=item C<-verbose>

Print out status messages.

=back

=head1 FILES

=over

=item C<I<mharc-root>/lib/config.sh>

Main configuration file for mharc.

=back

=head1 VERSION

$Id: read-mail,v 1.10 2002/09/15 03:33:08 ehood Exp $

=head1 AUTHOR

Earl Hood, earl@earlhood.com

This program is part of the mharc archiving system and comes with
ABSOLUTELY NO WARRANTY and may be copied only under the terms of
the GNU General Public License, which may be found in the mharc
distribution.

=cut

