source: trunk/gsdl/bin/script/filecopy.pl@ 1485

Last change on this file since 1485 was 1485, checked in by sjboddie, 24 years ago

More improvements to collector

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# filecopy.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# This program will "download" the specified files/directories
30
31BEGIN {
32 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
33 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
34}
35
36use util;
37use parsargv;
38use File::stat;
39use FileHandle;
40
41sub print_usage {
42 print STDERR "\n usage: $0 [options] [directories] collection-name\n\n";
43
44 print STDERR " options:\n";
45 print STDERR " -collectdir directory Collection directory (defaults to " .
46 &util::filename_cat ($ENV{'GSDLHOME'}, "collect") . ")\n";
47 print STDERR " -out Filename or handle to print output status to.\n";
48 print STDERR " The default is STDERR\n\n";
49}
50
51sub download_files
52{
53 my $dirname = pop(@_);
54 my $full_importname
55 = &util::filename_cat($collectdir, $dirname, "import");
56
57 # split argv into 2 lists: files and directories
58 my (@files,@dirs) = ((),());
59 my $a;
60 foreach $a (@_)
61 {
62 $a =~ s/^\"//;
63 $a =~ s/\"$//;
64
65 if (-e $a)
66 {
67 if (-d $a)
68 {
69 push(@dirs,$a);
70 }
71 else
72 {
73 push(@files,$a);
74 }
75 }
76 else
77 {
78 print $out "Error: filename '$a' does not exist\n";
79 }
80 }
81
82 if (scalar(@files)>0)
83 {
84 my $f;
85 foreach $f (@files)
86 {
87 my $src_file = $f;
88 my $dst_file = &get_dst_dir ($full_importname,$f);
89
90 my $do_copy = "no";
91 if (-e $dst_file)
92 {
93 # compare file dates
94 my $src_stat = stat($src_file);
95 my $dst_stat = stat($dst_file);
96
97 if ($src_stat->mtime > $dst_stat->mtime)
98 {
99 $do_copy = "yes";
100 }
101 }
102 else
103 {
104 $do_copy = "yes";
105 }
106
107 if ($do_copy eq "yes")
108 {
109 print $out "Copying $src_file-->$dst_file\n";
110 &util::cp($src_file,$dst_file);
111 }
112 }
113 }
114
115
116 if (scalar(@dirs)>0)
117 {
118 my $d;
119 foreach $d (@dirs)
120 {
121 my $src_dir = $d;
122 my $dst_dir = &get_dst_dir ($full_importname, $d);
123 if (!-e $dst_dir)
124 {
125 # create it if it doesn't exist
126 &util::mk_all_dir($dst_dir);
127 }
128
129 # read in dir
130 if (!opendir (INDIR, $d))
131 {
132 print $out "Error: Could not open directory $d\n";
133 }
134 else
135 {
136 my @sub_files = grep (!/^\.\.?$/, readdir (INDIR));
137 closedir (INDIR);
138
139 map { $_ = &util::filename_cat($d,$_); } @sub_files;
140 download_files(@sub_files,$dirname);
141 }
142 }
143 }
144}
145
146sub main
147{
148 if (!parsargv::parse(\@ARGV,
149 'collectdir/.*/', \$collectdir,
150 'out/.*/STDERR', \$out)) {
151 &print_usage();
152 die "\n";
153 }
154
155 if ($collectdir !~ /\w/) {
156 $collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect");
157 }
158
159 my $close_out = 0;
160 if ($out !~ /^(STDERR|STDOUT)$/i) {
161 open (OUT, ">$out") || die "Couldn't open output file $out\n";
162 $out = OUT;
163 $close_out = 1;
164 }
165 $out->autoflush(1);
166
167 download_files(@ARGV);
168
169 close OUT if $close_out;
170 return 0;
171}
172
173sub get_dst_dir {
174 my ($full_importname, $dir) = @_;
175
176 if ($ENV{'GSDLOS'} eq "windows") {
177 # don't want windows filenames like c:\gsdl\...\import\c:\dir
178 $dir =~ s/^[a-z]:[\\\/]//i;
179 }
180 return &util::filename_cat($full_importname,$dir);
181}
182
183&main();
Note: See TracBrowser for help on using the repository browser.