#!/usr/local/bin/perl
##---------------------------------------------------------------------------##
##  File:
##	$Id: compress-files,v 1.6 2002/08/26 05:33:23 ehood Exp $
##  Description:
##	Gzip files matching a specified pattern and older then specified
##	time period.
##---------------------------------------------------------------------------##
##  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_files;

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

use Getopt::Long;
use File::Find;
use MHArc::Util qw( usage );

# Amount of seconds in a year
my $year_secs = 31536000;

my %opt = ( );
my $clstatus = GetOptions(\%opt,
    "debug!",
    "pattern=s",
    "mbox-mode!",
    "mtime=i",
    "n!",

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

my $debug = $opt{'debug'};
my $mbox_mode = $opt{'mbox-mode'};
my $pattern = $opt{'pattern'} || ($mbox_mode ? '^\d+(?:-\d+)?$' : '^[^.]');
my $compress_time = $opt{'mtime'} || 31;
my $noact = $opt{'n'};

my $time = time;
$compress_time *= 24 * 3600; # convert to seconds

sub wanted {
  if (-d $_ ||
      /\.gz$/i ||
      !/$pattern/o) {
    print qq/Ignoring "$File::Find::name"\n/  if $debug;
    return;
  }

  my $mtime = (stat($_))[9];
  print qq/$File::Find::name (/, scalar(localtime($mtime)), qq/)\n/  if $debug;

  if ($mbox_mode && (/^\d+$/)) {
    if ($time-$mtime < ($compress_time+$year_secs)) {
      print qq/Skipping "$File::Find::name"\n/  if $debug;
      return;
    }
  } elsif ($time-$mtime < $compress_time) {
    print qq/Skipping "$File::Find::name"\n/  if $debug;
    return;
  }

  print qq/Compressing "$File::Find::name"\n/  if $debug;
  if ($noact) {
    print qq/gzip $File::Find::name\n/;
  } else {
    if (system('gzip', $_)) {
      die qq/gzip $File::Find::name failed: $?\n/;
    }
  }
}

if ($#ARGV < 0) {
  die qq/No directories, or files, specified\n/;
}
find(\&wanted, @ARGV);

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

=head1 NAME

compress-files - Gzip files not modified over a given period of time

=head1 SYNOPSIS

  compress-files [options] <directory> [<directory> ...]

=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 is used to compress files that have not
been modified over a given period of time.

=head1 OPTIONS

=over

=item C<-debug>

Print out alot of status information.

=item C<-help>

Print out usage information.

=item C<-n>

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

=item C<-mbox-mode>

A hack for compress mailbox files.  When this option is specified,
the default C<-pattern> is C<^\d+(?:-\d+)?$>.  This pattern basically
searches for files with names in YYYY and YYYY-MM format.  Files with
names in YYYY format are handled slightly different.  When checking
if the file can be compressed, a year is added to C<-mtime>.

=item C<-man>

Print out manpage.

=item C<-mtime> I<days>

Modification time in days a file has to be older than to get compressed.
If this option is not specified, 31 days is used.

=item C<-pattern> I<regex>

Perl regular expression that represents files that should be
checked.  If not specifed, the following regex is used:
C<^[^.]>.

=back

=head1 SEE ALSO

L<compress-mboxes>

=head1 VERSION

$Id: compress-files,v 1.6 2002/08/26 05:33:23 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

