source: main/trunk/greenstone2/bin/script/exportcol.pl@ 30499

Last change on this file since 30499 was 29246, checked in by ak19, 10 years ago

Fixed an error regarding when the fallback collectdir needs to be set.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# exportcol.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
28BEGIN {
29 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
30 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
31}
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and vice versa
35no strict 'subs'; # allow barewords (eg STDERR) as function arguments
36
37use util;
38use FileUtils;
39use parse2;
40use printusage;
41
42my $arguments =
43 [
44 { 'name' => "cdname",
45 'desc' => "{exportcol.cdname}",
46 'type' => "string",
47 'deft' => "Greenstone Collections",
48 'reqd' => "no" },
49 { 'name' => "cddir",
50 'desc' => "{exportcol.cddir}",
51 'type' => "string",
52 'deft' => "exported_collections",
53 'reqd' => "no" },
54 { 'name' => "collectdir",
55 'desc' => "{exportcol.collectdir}",
56 'type' => "string",
57 # parsearg left "" as default
58 #'deft' => &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "collect"),
59 'deft' => "",
60 'reqd' => "no",
61 'hiddengli' => "yes" },
62 { 'name' => "noinstall",
63 'desc' => "{exportcol.noinstall}",
64 'type' => "flag",
65 'reqd' => "no" },
66 { 'name' => "language",
67 'desc' => "{scripts.language}",
68 'type' => "string",
69 'reqd' => "no" },
70 { 'name' => "out",
71 'desc' => "{exportcol.out}",
72 'type' => "string",
73 'deft' => "STDERR",
74 'reqd' => "no" },
75 { 'name' => "xml",
76 'desc' => "{scripts.xml}",
77 'type' => "flag",
78 'reqd' => "no",
79 'hiddengli' => "yes" },
80 { 'name' => "gli",
81 'desc' => "",
82 'type' => "flag",
83 'reqd' => "no",
84 'hiddengli' => "yes" },
85
86 ];
87
88my $options = { 'name' => "exportcol.pl",
89 'desc' => "{exportcol.desc}",
90 'args' => $arguments };
91
92sub gsprintf
93{
94 return &gsprintf::gsprintf(@_);
95}
96
97
98&main();
99
100sub main {
101 my ($language, $out, $cdname, $cddir);
102
103 my $noinstall = 0;
104 my $xml = 0;
105 my $gli = 0;
106 my $collectdir;
107
108 my $hashParsingResult = {};
109
110 # parse options
111 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
112
113 # If parse returns -1 then something has gone wrong
114 if ($intArgLeftinAfterParsing == -1)
115 {
116 &PrintUsage::print_txt_usage($options, "{exportcol.params}");
117 die "\n";
118 }
119
120 foreach my $strVariable (keys %$hashParsingResult)
121 {
122 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
123 }
124
125 # the default/fallback for collect directory if none is provided
126 # (no -collectdir option given) is the standard Greenstone collect directory
127 if(!$collectdir) {
128 $collectdir = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "collect");
129 }
130
131 # If $language has been specified, load the appropriate resource bundle
132 # (Otherwise, the default resource bundle will be loaded automatically)
133 if ($language && $language =~ /\S/) {
134 &gsprintf::load_language_specific_resource_bundle($language);
135 }
136
137 if ($xml) {
138 &PrintUsage::print_xml_usage($options);
139 print "\n";
140 return;
141 }
142
143 if ($gli) { # the gli wants strings to be in UTF-8
144 &gsprintf::output_strings_in_UTF8;
145 }
146
147 # var collectdir (dir from where selected collections are exported to the CD)
148 # may have been set at this point if it was specified with -collectdir
149
150 # can have more than one collection name,
151 # if the first extra option is -h, then output the help
152 if (scalar(@ARGV) == 0 || (@ARGV && $ARGV[0] =~ /^\-+h/)) {
153 &PrintUsage::print_txt_usage($options, "{exportcol.params}");
154 die "\n";
155 }
156
157 my @coll_list = @ARGV;
158
159 my $close_out = 0;
160 if ($out !~ /^(STDERR|STDOUT)$/i) {
161 open (OUT, ">$out") ||
162 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
163 $out = 'main::OUT';
164 $close_out = 1;
165 }
166
167 # first we do a quick check to see if the export coll function has been
168 # installed
169 my $gssetupexe = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "bin", "windows", "gssetup.exe");
170 if (!-e $gssetupexe) {
171 &gsprintf($out, "{exportcol.fail} {exportcol.export_coll_not_installed}\n");
172 die "\n";
173 }
174
175 # check each collection
176 my @valid_coll_list = ();
177 foreach my $c (@coll_list) {
178 my $colldir = &FileUtils::filenameConcatenate ($collectdir, $c);
179 if (! -d $colldir) {
180 &gsprintf($out, "{exportcol.coll_not_found}\n", $c, $colldir);
181 next;
182 }
183 my $colindexdir = &FileUtils::filenameConcatenate ($colldir, "index");
184 my $coletcdir = &FileUtils::filenameConcatenate ($colldir, "etc");
185 if ((!-d $colindexdir) || (!-d $coletcdir)) {
186 &gsprintf($out, "{exportcol.coll_dirs_not_found}\n", $c);
187 &gsprintf($out, " $colindexdir\n");
188 &gsprintf($out, " $coletcdir\n");
189 next;
190 }
191 # the collection seems ok, we add it to the valid coll list
192 push @valid_coll_list, $c;
193 }
194
195 if (not @valid_coll_list) {
196 # no valid colls left
197 &gsprintf($out, "{exportcol.fail} {exportcol.no_valid_colls}\n");
198 die "\n";
199 }
200
201 # create exported directory
202 my $topdir = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "tmp", $cddir);
203 &FileUtils::makeAllDirectories ($topdir);
204 if (!-d $topdir) {
205 &gsprintf($out, "{exportcol.fail} {exportcol.couldnt_create_dir}\n", $topdir);
206 die "\n";
207 }
208
209 # we create either a self installing cd, or one that runs off the cd (and
210 # doesn't install anything
211
212 # create all the directories - we assume that if we created the top dir ok,
213 # then all the other mkdirs will go ok
214 my $gsdldir;
215 if ($noinstall) {
216 $gsdldir = $topdir;
217 }
218 else {
219 $gsdldir = &FileUtils::filenameConcatenate ($topdir, "gsdl");
220 &FileUtils::makeAllDirectories ($gsdldir);
221 }
222
223 my $newcollectdir = &FileUtils::filenameConcatenate ($gsdldir, "collect");
224 &FileUtils::makeAllDirectories ($newcollectdir);
225 my $etcdir = &FileUtils::filenameConcatenate ($gsdldir, "etc");
226 &FileUtils::makeAllDirectories ($etcdir);
227
228 #create the config files
229 if (!$noinstall) {
230 # create the install.cfg file
231 my $installcfg = &FileUtils::filenameConcatenate ($topdir, "install.cfg");
232 if (!open (INSTALLCFG, ">$installcfg")) {
233 &gsprintf($out, "{exportcol.fail} {exportcol.couldnt_create_file}\n", $installcfg);
234 die "\n";
235 }
236 print INSTALLCFG "CompanyName:New Zealand Digital Library\n";
237 print INSTALLCFG "CollectionName:$cdname\n";
238 print INSTALLCFG "CollectionDirName:$cdname\n";
239 print INSTALLCFG "CollectionVersion:1.0\n";
240 print INSTALLCFG "CollectionVolume:1\n";
241 print INSTALLCFG "ProgramGroupName:Greenstone\n";
242 close INSTALLCFG;
243
244 # create the manifest.cfg file
245 my $manifestcfg = &FileUtils::filenameConcatenate ($topdir, "manifest.cfg");
246 if (!open (MANIFESTCFG, ">$manifestcfg")) {
247 &gsprintf($out, "{exportcol.fail} {exportcol.couldnt_create_file}\n", $manifestcfg);
248 die "\n";
249 }
250 print MANIFESTCFG "all:\n";
251 print MANIFESTCFG " {library} {collection}\n\n";
252 print MANIFESTCFG "library:\n";
253 print MANIFESTCFG " server.exe\n\n";
254 print MANIFESTCFG "database:\n";
255 print MANIFESTCFG ' etc ';
256 foreach my $c (@valid_coll_list) {
257 print MANIFESTCFG "collect\\$c\\index\\text\\$c.gdb ";
258 }
259 print MANIFESTCFG "\n\n";
260 print MANIFESTCFG "collection:\n";
261 print MANIFESTCFG " collect etc macros mappings web\n";
262 close MANIFESTCFG;
263
264 }
265
266 #create the autorun.inf file
267 my $autoruninf = &FileUtils::filenameConcatenate ($topdir, "Autorun.inf");
268 if (!open (AUTORUNINF, ">$autoruninf")) {
269 &gsprintf($out, "{exportcol.fail} {exportcol.couldnt_create_file}\n", $autoruninf);
270 die "\n";
271 }
272
273 print AUTORUNINF "[autorun]\n";
274 if ($noinstall) {
275 print AUTORUNINF "OPEN=server.exe\n";
276 } else {
277 print AUTORUNINF "OPEN=gssetup.exe\n"; # no longer autorun Setup.exe, since it fails on Win 7 64 bit
278 }
279 close AUTORUNINF;
280
281 # copy the necessary stuff from GSDLHOME
282 my $webdir = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "web");
283 my $macrosdir = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "macros");
284 my $mappingsdir = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "mappings");
285 my $maincfg = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "etc", "main.cfg");
286 my $serverexe = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "bin", "windows", "server.exe");
287 my $setupexe = &FileUtils::filenameConcatenate ($ENV{'GSDLHOME'}, "bin", "windows", "Setup.exe");
288
289 if ((!-d $webdir) || (!-d $macrosdir) || (!-d $mappingsdir) || (!-e $maincfg) ||
290 (!-e $serverexe) || (!-e $gssetupexe) || (!-e $setupexe)) {
291 &gsprintf($out, "{exportcol.fail} {exportcol.non_exist_files}\n");
292 &gsprintf($out, " $webdir\n");
293 &gsprintf($out, " $macrosdir\n");
294 &gsprintf($out, " $mappingsdir\n");
295 &gsprintf($out, " $maincfg\n");
296 &gsprintf($out, " $serverexe\n");
297 &gsprintf($out, " $gssetupexe\n");
298 &gsprintf($out, " $setupexe\n");
299 die "\n";
300 }
301
302 &FileUtils::copyFilesRecursiveNoSVN ($webdir, $gsdldir);
303 &FileUtils::copyFilesRecursiveNoSVN ($macrosdir, $gsdldir);
304 &FileUtils::copyFilesRecursiveNoSVN ($mappingsdir, $gsdldir);
305 &FileUtils::copyFiles ($maincfg, $etcdir);
306 &FileUtils::copyFiles ($serverexe, $gsdldir);
307
308 if (!$noinstall) {
309 &FileUtils::copyFiles ($gssetupexe, $topdir);
310 &FileUtils::copyFiles ($setupexe, $topdir); # unused, since Setup.exe does not work on Win 7 64-bit
311 }
312
313 # now change the home.dm macro file to a simple version
314 my $newmacrodir = &FileUtils::filenameConcatenate ($gsdldir, "macros");
315 my $exporthome = &FileUtils::filenameConcatenate ($newmacrodir, "exported_home.dm");
316 my $oldhome = &FileUtils::filenameConcatenate ($newmacrodir, "home.dm");
317 if (-e $exporthome) {
318 &FileUtils::removeFiles($oldhome);
319 &FileUtils::moveFiles($exporthome, $oldhome);
320 }
321
322 # copy the collections over
323 foreach my $c (@valid_coll_list) {
324 #old directories
325 my $colldir = &FileUtils::filenameConcatenate ($collectdir, $c);
326 my $colindexdir = &FileUtils::filenameConcatenate ($colldir, "index");
327 my $coletcdir = &FileUtils::filenameConcatenate ($colldir, "etc");
328 my $colmacrosdir = &FileUtils::filenameConcatenate ($colldir, "macros");
329 my $colimagesdir = &FileUtils::filenameConcatenate ($colldir, "images");
330 my $colscriptdir = &FileUtils::filenameConcatenate ($colldir, "script");
331 my $coljavadir = &FileUtils::filenameConcatenate ($colldir, "java");
332 my $colstyledir = &FileUtils::filenameConcatenate ($colldir, "style");
333 my $colflashdir = &FileUtils::filenameConcatenate ($colldir, "flash");
334
335 # new collection directory
336 # $c might be in a group, for now, copy to the top level.
337 my $new_c = $c;
338 $new_c =~ s/^.*[\/\\]//; # remove any folder info
339 my $newcoldir = &FileUtils::filenameConcatenate ($newcollectdir, $new_c);
340
341 &FileUtils::makeAllDirectories ($newcoldir);
342 &FileUtils::copyFilesRecursiveNoSVN ($colindexdir, $newcoldir);
343 &util::rename_ldb_or_bdb_file(&FileUtils::filenameConcatenate ($newcoldir, "index", "text", $c));
344 &FileUtils::copyFilesRecursiveNoSVN ($coletcdir, $newcoldir);
345 &FileUtils::copyFilesRecursiveNoSVN ($colmacrosdir, $newcoldir) if (-e $colmacrosdir);
346 &FileUtils::copyFilesRecursiveNoSVN ($colimagesdir, $newcoldir) if (-e $colimagesdir);
347 &FileUtils::copyFilesRecursiveNoSVN ($colscriptdir, $newcoldir) if (-e $colscriptdir);
348 &FileUtils::copyFilesRecursiveNoSVN ($coljavadir, $newcoldir) if (-e $coljavadir);
349 &FileUtils::copyFilesRecursiveNoSVN ($colstyledir, $newcoldir) if (-e $colstyledir);
350 &FileUtils::copyFilesRecursiveNoSVN ($colflashdir, $newcoldir) if (-e $colflashdir);
351
352 # now we need to check the collect.cfg file to make sure it's public
353 my $collectcfg = &FileUtils::filenameConcatenate ($newcoldir, "etc", "collect.cfg");
354 open INFILE, "<$collectcfg";
355 open OUTFILE, ">$collectcfg.tmp";
356 my $line;
357 while ($line = <INFILE>) {
358 if ($line =~ /^\s*public\s+false/) {
359 print OUTFILE "public\ttrue\n";
360 last; # stop matching once we have found the line
361 } else {
362 print OUTFILE "$line";
363 }
364 }
365 # continue with no checking
366 while ($line = <INFILE>) {
367 print OUTFILE "$line";
368 }
369 close INFILE;
370 close OUTFILE;
371 &FileUtils::moveFiles("$collectcfg.tmp", $collectcfg);
372 }
373 &gsprintf($out, "{exportcol.success}");
374
375 my $successcolls = "";
376 my $first = 1;
377 foreach my $c (@valid_coll_list) {
378 if ($first) {
379 $first=0;
380 } else {
381 $successcolls .=", ";
382 }
383 $successcolls .= "$c";
384 }
385
386 my $gsdl_home = $ENV{'GSDLHOME'};
387 my $portable_topdir = $topdir;
388 # Disabled this because it isn't currently useful (the GLI applet doesn't do exporting)
389 # It doesn't work on Windows, either
390 # $portable_topdir =~ s/$gsdl_home/\$GSDLHOME/g;
391
392 &gsprintf($out, "{exportcol.output_dir}\n", $successcolls, $portable_topdir);
393 &gsprintf($out, "exportcol.pl succeeded:{exportcol.instructions}\n");
394 close OUT if $close_out;
395}
396
Note: See TracBrowser for help on using the repository browser.