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