source: trunk/gsdl/bin/script/clean_archives.pl@ 946

Last change on this file since 946 was 538, checked in by sjboddie, 25 years ago

added GPL header

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.7 KB
Line 
1#!/usr/local/bin/perl5
2
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
29# clean_archives.pl removes any doc.gml, doc.gml.gz, and empty
30# directories from the directory passed on the command line.
31
32BEGIN {
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
38use parsargv;
39use util;
40
41sub print_usage {
42 print STDERR "\n usage: $0 [options] archive-dir\n\n";
43 print STDERR " oprions:\n";
44 print STDERR " -verbose\n\n";
45}
46
47
48&main ();
49
50sub main {
51
52 if (!parsargv::parse(\@ARGV,
53 'verbose', \$verbose)) {
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
73sub 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);
89 print STDERR "removing $filename\n" if $verbose;
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}
Note: See TracBrowser for help on using the repository browser.