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

Last change on this file since 1970 was 1970, checked in by sjboddie, 23 years ago

Added more usage information to all perl programs and removed a few
programs that are no longer useful.

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