[1173] | 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;
|
---|
[1424] | 37 | use parsargv;
|
---|
[1173] | 38 | use File::stat;
|
---|
[1424] | 39 | use FileHandle;
|
---|
[1173] | 40 |
|
---|
[1424] | 41 | sub print_usage {
|
---|
| 42 | print STDERR "\n usage: $0 [options] [directories] collection-name\n\n";
|
---|
| 43 |
|
---|
| 44 | print STDERR " options:\n";
|
---|
[1454] | 45 | print STDERR " -collectdir directory Collection directory (defaults to " .
|
---|
| 46 | &util::filename_cat ($ENV{'GSDLHOME'}, "collect") . ")\n";
|
---|
[1424] | 47 | print STDERR " -out Filename or handle to print output status to.\n";
|
---|
| 48 | print STDERR " The default is STDERR\n\n";
|
---|
[1173] | 49 | }
|
---|
| 50 |
|
---|
| 51 | sub download_files
|
---|
| 52 | {
|
---|
| 53 | my $dirname = pop(@_);
|
---|
| 54 | my $full_importname
|
---|
[1454] | 55 | = &util::filename_cat($collectdir, $dirname, "import");
|
---|
[1173] | 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 | {
|
---|
[1424] | 78 | print $out "Error: filename '$a' does not exist\n";
|
---|
[1173] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (scalar(@files)>0)
|
---|
| 83 | {
|
---|
| 84 | my $f;
|
---|
| 85 | foreach $f (@files)
|
---|
| 86 | {
|
---|
| 87 | my $src_file = $f;
|
---|
[1179] | 88 | my $dst_file = &get_dst_dir ($full_importname,$f);
|
---|
[1173] | 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 | {
|
---|
[1424] | 109 | print $out "Copying $src_file-->$dst_file\n";
|
---|
[1173] | 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;
|
---|
[1179] | 122 | my $dst_dir = &get_dst_dir ($full_importname, $d);
|
---|
[1173] | 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 | {
|
---|
[1424] | 132 | print $out "Error: Could not open directory $d\n";
|
---|
[1173] | 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 |
|
---|
| 146 | sub main
|
---|
| 147 | {
|
---|
[1454] | 148 | if (!parsargv::parse(\@ARGV,
|
---|
| 149 | 'collectdir/.*/', \$collectdir,
|
---|
| 150 | 'out/.*/STDERR', \$out)) {
|
---|
[1424] | 151 | &print_usage();
|
---|
| 152 | die "\n";
|
---|
[1173] | 153 | }
|
---|
| 154 |
|
---|
[1454] | 155 | if ($collectdir !~ /\w/) {
|
---|
| 156 | $collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect");
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[1424] | 159 | my $close_out = 0;
|
---|
| 160 | if ($out !~ /^(STDERR|STDOUT)$/i) {
|
---|
[1431] | 161 | open (OUT, ">$out") || die "Couldn't open output file $out\n";
|
---|
| 162 | $out = OUT;
|
---|
[1424] | 163 | $close_out = 1;
|
---|
| 164 | }
|
---|
| 165 | $out->autoflush(1);
|
---|
| 166 |
|
---|
[1173] | 167 | download_files(@ARGV);
|
---|
[1424] | 168 |
|
---|
| 169 | close OUT if $close_out;
|
---|
[1173] | 170 | return 0;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[1179] | 173 | sub get_dst_dir {
|
---|
| 174 | my ($full_importname, $dir) = @_;
|
---|
[1173] | 175 |
|
---|
[1179] | 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 | }
|
---|
[1173] | 182 |
|
---|
[1179] | 183 | &main();
|
---|