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