remove seemingly old useless ed
[mharc.git] / bin / compress-files
... / ...
CommitLineData
1#!/usr/bin/perl
2##--------------------------------------------------------------------------##
3## File:
4## $Id: compress-files,v 1.8 2002/09/15 03:33:08 ehood Exp $
5## Description:
6## Gzip files matching a specified pattern and older then specified
7## time period.
8##--------------------------------------------------------------------------##
9## Copyright (C) 2001-2002 Earl Hood <earl@earlhood.com>
10##
11## This program is free software; you can redistribute it and/or modify
12## it under the terms of the GNU General Public License as published by
13## the Free Software Foundation; either version 2 of the License, or
14## (at your option) any later version.
15##
16## This program is distributed in the hope that it will be useful,
17## but WITHOUT ANY WARRANTY; without even the implied warranty of
18## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19## GNU General Public License for more details.
20##
21## You should have received a copy of the GNU General Public License
22## along with this program; if not, write to the Free Software
23## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24## 02111-1307, USA
25##--------------------------------------------------------------------------##
26
27package MHArc::compress_files;
28
29##--------------------------------------------------------------------------##
30# <boot-strap>
31BEGIN { die qq/CGI use FORBIDDEN!\n/ if (defined($ENV{'GATEWAY_INTERFACE'})); }
32my $Dir; BEGIN { $Dir = `dirname $0`; chomp $Dir; }
33use lib "$Dir/../lib"; # Add relative lib to search path
34# </boot-strap>
35##--------------------------------------------------------------------------##
36
37
38use Getopt::Long;
39use File::Find;
40use MHArc::Util qw( usage );
41
42# Amount of seconds in a year
43my $year_secs = 31536000;
44
45my %opt = ( );
46my $clstatus = GetOptions(\%opt,
47 "debug!",
48 "pattern=s",
49 "mbox-mode!",
50 "mtime=i",
51 "n!",
52
53 'help',
54 'man'
55);
56usage(0) unless $clstatus;
57usage(1) if $opt{'help'};
58usage(2) if $opt{'man'};
59
60my $debug = $opt{'debug'};
61my $mbox_mode = $opt{'mbox-mode'};
62my $pattern = $opt{'pattern'} || ($mbox_mode ? '^\d+(?:-\d+)?$' : '^[^.]');
63my $compress_time = $opt{'mtime'} || 31;
64my $noact = $opt{'n'};
65
66my $time = time;
67$compress_time *= 24 * 3600; # convert to seconds
68
69sub wanted {
70 if (-d $_ ||
71 /\.gz$/i ||
72 !/$pattern/o) {
73 print qq/Ignoring "$File::Find::name"\n/ if $debug;
74 return;
75 }
76
77 my $mtime = (stat($_))[9];
78 print qq/$File::Find::name (/, scalar(localtime($mtime)), qq/)\n/ if $debug;
79
80 if ($mbox_mode && (/^\d+$/)) {
81 if ($time-$mtime < ($compress_time+$year_secs)) {
82 print qq/Skipping "$File::Find::name"\n/ if $debug;
83 return;
84 }
85 } elsif ($time-$mtime < $compress_time) {
86 print qq/Skipping "$File::Find::name"\n/ if $debug;
87 return;
88 }
89
90 print qq/Compressing "$File::Find::name"\n/ if $debug;
91 if ($noact) {
92 print qq/gzip $File::Find::name\n/;
93 } else {
94 if (system('gzip', $_)) {
95 die qq/gzip $File::Find::name failed: $?\n/;
96 }
97 }
98}
99
100if ($#ARGV < 0) {
101 die qq/No directories, or files, specified\n/;
102}
103find(\&wanted, @ARGV);
104
105##---------------------------------------------------------------------------##
106__END__
107
108=head1 NAME
109
110compress-files - Gzip files not modified over a given period of time
111
112=head1 SYNOPSIS
113
114 compress-files [options] <directory> [<directory> ...]
115
116=head1 DESCRIPTION
117
118This program is part of mharc. This program is used to compress
119files that have not been modified over a given period of time.
120
121=head1 OPTIONS
122
123=over
124
125=item C<-debug>
126
127Print out alot of status information.
128
129=item C<-help>
130
131Print out usage information.
132
133=item C<-n>
134
135Print the commands that would be executed, but do not execute them.
136
137=item C<-mbox-mode>
138
139A hack for compress mailbox files. When this option is specified,
140the default C<-pattern> is C<^\d+(?:-\d+)?$>. This pattern basically
141searches for files with names in YYYY and YYYY-MM format. Files with
142names in YYYY format are handled slightly different. When checking
143if the file can be compressed, a year is added to C<-mtime>.
144
145=item C<-man>
146
147Print out manpage.
148
149=item C<-mtime> I<days>
150
151Modification time in days a file has to be older than to get compressed.
152If this option is not specified, 31 days is used.
153
154=item C<-pattern> I<regex>
155
156Perl regular expression that represents files that should be
157checked. If not specifed, the following regex is used:
158C<^[^.]>.
159
160=back
161
162=head1 SEE ALSO
163
164L<compress-mboxes|compress-mboxes>
165
166=head1 VERSION
167
168$Id: compress-files,v 1.8 2002/09/15 03:33:08 ehood Exp $
169
170=head1 AUTHOR
171
172Earl Hood, earl@earlhood.com
173
174This program is part of the mharc archiving system and comes with
175ABSOLUTELY NO WARRANTY and may be copied only under the terms of
176the GNU General Public License, which may be found in the mharc
177distribution.
178
179=cut
180