#!/usr/local/bin/perl
##---------------------------------------------------------------------------##
##  File:
##	$Id: gc-search-indexes,v 1.4 2002/07/09 01:30:49 ehood Exp $
##  Description:
##	Script to do garbage collection on Namazu search indexes.
##---------------------------------------------------------------------------##
##  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::compress_mboxes;

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::Util qw( cmd usage );

MAIN: {
  my %opt = ( );
  my $clstatus = GetOptions(\%opt,
    "debug!",
    "n!",

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

  my $debug = $opt{'debug'};
  my $noact = $opt{'n'};

  $ENV{'PATH'} = join(':', join('/', $config->{'SW_ROOT'}, 'bin'),
			   '/usr/local/bin',
			   '/bin',
			   '/usr/bin');

  $MHArc::Util::ECHO_CMDS = $noact || $debug;
  $MHArc::Util::ECHO_ONLY = $noact;

  print "PATH=$ENV{'PATH'}\n"  if $debug;

  # Sanity check on $HTML_DIR
  my $html_dir = $config->{'HTML_DIR'};
  if (! -d $html_dir) {
    die qq/ERROR: "/, $html_dir, qq/" is not a directory!/;
  }
  print qq/HTML_DIR=$html_dir\n/  if $debug;

  # Get list of possible directories
  opendir(HTML_DIR, $html_dir) ||
    die qq/ERROR: Unable to open "$html_dir" for reading: $!\n/;
  my @dirs = map { "$html_dir/$_" }
		 grep { ($_ ne '.') &&
			($_ ne '..') &&
			(-d "$html_dir/$_") &&
			(-e "$html_dir/$_/NMZ.t") } readdir(HTML_DIR);

  # Check that we have something to do
  if (!scalar(@dirs)) {
    print "No search indexes found\n"  if $debug;
    exit 0;
  }
  print "Searchable directories:\n\t",
	join("\n\t", @dirs), "\n"  if $debug;

  # Run garbage collection
  my @cmd_args = ( '-b' );
  if ($debug) {
    push(@cmd_args, '-v');
  } else {
    push(@cmd_args, '-q');
  }
  foreach (@dirs) {
    if (cmd('gcnmz', @cmd_args, $_) != 0) {
      warn qq/Warning: Command "gcnmz @cmd_args $_" failed: $?\n/;
    }
  }

} # End: MAIN

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

=head1 NAME

gc-search-indexes - Garbage collect archive search indexes.

=head1 SYNOPSIS

  gc-search-indexes
  gc-search-indexes [options]

=head1 DESCRIPTION

This program is part of MHArc, the auto-archiving system that works in
conjuction with Procmail, Namazu, and a collection of shell and
Perl programs.  This program has the responsibility of performing
garbage collection on archive search indexes.

Generally, this program will be invoked automatically from C<cron>
on a periodic basis.

This program will use the C<HTML_DIR> variable from
C<I<mharc-root>/lib/config.sh> as the root directory to search for
searchable archives.

=head1 OPTIONS

=over

=item C<-debug>

Print out alot of information on what is going on.

=item C<-help>

Print out usage information.

=item C<-n>

Print the commands that would be executed, but do not execute them.

=item C<-man>

Print out manpage.

=back

=head1 FILES

=over

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

Main configuration file for MHArc.

=back

=head1 NOTES

=over

=item *

This program should be invoked occasionally, like once a week.  The
template crontab provided with MHArc provides a useful crontab entry
for invoking this program.

=back

=head1 VERSION

$Id: gc-search-indexes,v 1.4 2002/07/09 01:30:49 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

