[475] | 1 | #!/usr/local/bin/perl5
|
---|
| 2 |
|
---|
[538] | 3 | ###########################################################################
|
---|
| 4 | #
|
---|
| 5 | # clean_archives.pl
|
---|
| 6 | # A component of the Greenstone digital library software
|
---|
| 7 | # from the New Zealand Digital Library Project at the
|
---|
| 8 | # University of Waikato, New Zealand.
|
---|
| 9 | #
|
---|
| 10 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 11 | #
|
---|
| 12 | # This program is free software; you can redistribute it and/or modify
|
---|
| 13 | # it under the terms of the GNU General Public License as published by
|
---|
| 14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | # (at your option) any later version.
|
---|
| 16 | #
|
---|
| 17 | # This program is distributed in the hope that it will be useful,
|
---|
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | # GNU General Public License for more details.
|
---|
| 21 | #
|
---|
| 22 | # You should have received a copy of the GNU General Public License
|
---|
| 23 | # along with this program; if not, write to the Free Software
|
---|
| 24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | #
|
---|
| 26 | ###########################################################################
|
---|
| 27 |
|
---|
| 28 |
|
---|
[475] | 29 | # clean_archives.pl removes any doc.gml, doc.gml.gz, and empty
|
---|
| 30 | # directories from the directory passed on the command line.
|
---|
| 31 |
|
---|
| 32 | BEGIN {
|
---|
| 33 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
| 34 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
| 35 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | use parsargv;
|
---|
| 39 | use util;
|
---|
| 40 |
|
---|
| 41 | sub print_usage {
|
---|
| 42 | print STDERR "\n usage: $0 [options] archive-dir\n\n";
|
---|
| 43 | print STDERR " oprions:\n";
|
---|
[476] | 44 | print STDERR " -verbose\n\n";
|
---|
[475] | 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | &main ();
|
---|
| 49 |
|
---|
| 50 | sub main {
|
---|
| 51 |
|
---|
| 52 | if (!parsargv::parse(\@ARGV,
|
---|
[476] | 53 | 'verbose', \$verbose)) {
|
---|
[475] | 54 | &print_usage();
|
---|
| 55 | die "\n";
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (scalar (@ARGV) != 1) {
|
---|
| 59 | &print_usage();
|
---|
| 60 | die "\n";
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | my $dir = $ARGV[0];
|
---|
| 64 |
|
---|
| 65 | if (!-d $dir) {
|
---|
| 66 | &print_usage();
|
---|
| 67 | die "\n";
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | &recurse_dir ($dir);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | sub recurse_dir {
|
---|
| 74 | my ($dir) = @_;
|
---|
| 75 |
|
---|
| 76 | opendir (DIR, $dir) || die;
|
---|
| 77 | my @files = readdir DIR;
|
---|
| 78 | closedir DIR;
|
---|
| 79 |
|
---|
| 80 | foreach $file (@files) {
|
---|
| 81 | next if $file =~ /^\.+$/;
|
---|
| 82 | my $filename = &util::filename_cat($dir, $file);
|
---|
| 83 |
|
---|
| 84 | if (-d $filename) {
|
---|
| 85 | &recurse_dir ($filename);
|
---|
| 86 | } elsif ($file =~ /^doc\.gml(\.gz)?$/) {
|
---|
| 87 |
|
---|
| 88 | unlink ($filename);
|
---|
[476] | 89 | print STDERR "removing $filename\n" if $verbose;
|
---|
[475] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | # remove the directory if it's now empty
|
---|
| 94 | opendir (DIR, $dir) || die;
|
---|
| 95 | @files = readdir DIR;
|
---|
| 96 | closedir DIR;
|
---|
| 97 |
|
---|
| 98 | if (scalar(@files) == 0) {
|
---|
| 99 | rmdir ($dir);
|
---|
| 100 | print STDERR "removing empty directory $dir\n" if $verbosity;
|
---|
| 101 | } elsif (scalar(@files) == 2) {
|
---|
| 102 | my $empty = 1;
|
---|
| 103 | map { if ($_ !~ /^\.+$/) {$empty = 0;} } @files;
|
---|
| 104 | if ($empty) {
|
---|
| 105 | rmdir ($dir);
|
---|
| 106 | print STDERR "removing empty directory $dir\n" if $verbosity;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|