#!/usr/local/bin/perl
##---------------------------------------------------------------------------##
##  File:
##	$Id: compress-files,v 1.4 2002/03/07 02:03:31 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 MHArc::Config;
my $config = MHArc::Config->load("$Dir/../lib/config.sh");


use Getopt::Long;
use File::Find;

my $debug = 0;
my $pattern = '^[^.]';
my $compress_time = 31;
my $noact = 0;
GetOptions(
    "debug!"	=> \$debug,
    "pattern=s" => \$pattern,
    "mtime=i"	=> \$compress_time,
    "n!"	=> \$noact
);

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 ($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);
