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