source: main/trunk/greenstone2/bin/script/mkcol.pl@ 21301

Last change on this file since 21301 was 21301, checked in by davidb, 14 years ago

Updated mkcol.pl so it creates a collection in the Greenstone3 area (not the Greenstone2's collect folder) when the -site argument is provided

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# mkcol.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 setup a new collection from a model one. It does this by
30# copying the model, moving files to have the correct names, and replacing
31# text within the files to match the parameters.
32
33package mkcol;
34
35BEGIN {
36 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
37 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
38 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
39}
40
41use parse2;
42use util;
43use cfgread;
44use gsprintf 'gsprintf';
45use printusage;
46
47use strict;
48no strict 'subs'; # allow barewords (eg STDERR) as function arguments
49
50my $public_list =
51 [ { 'name' => "true",
52 'desc' => "{mkcol.public.true}"},
53 { 'name' => "false",
54 'desc' => "{mkcol.public.false}"}
55 ];
56
57my $win31compat_list =
58 [ { 'name' => "true",
59 'desc' => "{mkcol.win31compat.true}"},
60 { 'name' => "false",
61 'desc' => "{mkcol.win31compat.false}"}
62 ];
63
64my $arguments =
65 [ { 'name' => "creator",
66 'desc' => "{mkcol.creator}",
67 'type' => "string",
68 'reqd' => "no" },
69 { 'name' => "optionfile",
70 'desc' => "{mkcol.optionfile}",
71 'type' => "string",
72 'reqd' => "no" },
73 { 'name' => "maintainer",
74 'desc' => "{mkcol.maintainer}",
75 'type' => "string",
76 'reqd' => "no" },
77 { 'name' => "gs3mode",
78 'desc' => "{mkcol.gs3mode}",
79 'type' => "flag",
80 'reqd' => "no" },
81 { 'name' => "group",
82 'desc' => "{mkcol.group}",
83 'type' => "flag",
84 'reqd' => "no" },
85 { 'name' => "collectdir",
86 'desc' => "{mkcol.collectdir}",
87 'type' => "string",
88 'reqd' => "no" }, # For gs3, this collectdir must be provided.
89 { 'name' => "site",
90 'desc' => "{mkcol.site}",
91 'type' => "string",
92 'reqd' => "no" }, # For gs3, this collectdir must be provided.
93 { 'name' => "public",
94 'desc' => "{mkcol.public}",
95 'type' => "enum",
96 'deft' => "true",
97 'list' => $public_list,
98 'reqd' => "no" },
99 { 'name' => "title",
100 'desc' => "{mkcol.title}",
101 'type' => "string",
102 'reqd' => "no" },
103 { 'name' => "about",
104 'desc' => "{mkcol.about}",
105 'type' => "string",
106 'reqd' => "no" },
107 { 'name' => "plugin",
108 'desc' => "{mkcol.plugin}",
109 'type' => "string",
110 'reqd' => "no" },
111 { 'name' => "quiet",
112 'desc' => "{mkcol.quiet}",
113 'type' => "flag",
114 'reqd' => "no" },
115 { 'name' => "language",
116 'desc' => "{scripts.language}",
117 'type' => "string",
118 'reqd' => "no" },
119 { 'name' => "win31compat",
120 'desc' => "{mkcol.win31compat}",
121 'type' => "enum",
122 'deft' => "false",
123 'list' => $win31compat_list,
124 'reqd' => "no" },
125 { 'name' => "gli",
126 'desc' => "",
127 'type' => "flag",
128 'reqd' => "no",
129 'hiddengli' => "yes" },
130 { 'name' => "xml",
131 'desc' => "{scripts.xml}",
132 'type' => "flag",
133 'reqd' => "no",
134 'hiddengli' => "yes" }
135 ];
136
137my $options = { 'name' => "mkcol.pl",
138 'desc' => "{mkcol.desc}",
139 'args' => $arguments };
140
141# options
142my ($creator, $optionfile, $maintainer, $gs3mode, $group, $collectdir, $site,
143 $public,
144 $title, $about, $plugin, $quiet, $language, $win31compat, $gli);
145
146#other variables
147my ($collection, $capcollection,
148 $collection_tail, $capcollection_tail,
149 $pluginstring, @plugin);
150
151&main();
152
153
154sub traverse_dir
155{
156 my ($modeldir, $coldir) = @_;
157 my ($newfile, @filetext);
158
159 if (!(-e $coldir)) {
160
161
162 my $store_umask = umask(0002);
163 my $mkdir_ok = mkdir ($coldir, 0777);
164 umask($store_umask);
165
166 if (!$mkdir_ok)
167 {
168 die "$!";
169 }
170 }
171
172 opendir(DIR, $modeldir) ||
173 (&gsprintf(STDERR, "{common.cannot_read}\n", $modeldir) && die);
174 my @files = grep(!/^(\.\.?|CVS|\.svn)$/, readdir(DIR));
175 closedir(DIR);
176
177 foreach my $file (@files)
178 {
179 if ($file =~ /^macros$/) {
180
181 # don't want macros folder for gs3mode
182 next if $gs3mode;
183 }
184 if ($file =~ /^import$/) {
185 # don't want import for group
186 next if $group;
187 }
188
189 my $thisfile = &util::filename_cat ($modeldir, $file);
190
191 if (-d $thisfile) {
192 my $colfiledir = &util::filename_cat ($coldir, $file);
193 traverse_dir ($thisfile, $colfiledir);
194
195 } else {
196
197 next if ($file =~ /~$/);
198
199 my $destfile = $file;
200 $destfile =~ s/^modelcol/$collection/;
201 $destfile =~ s/^MODELCOL/$capcollection/;
202
203 # There are three configuration files in modelcol directory:
204 # collect.cfg, group.cfg and collectionConfig.xml.
205 # If it is gs2, copy relevant collect.cfg or group.cfg file; if gs3, copy collectionConfig.xml.
206
207 if ($file =~ /^collect\.cfg$/) {
208 next if ($gs3mode || $group);
209 }
210 elsif ($file =~ /^group\.cfg$/) {
211 next unless $group;
212 $destfile =~ s/group\.cfg/collect\.cfg/;
213 }
214 elsif ($file =~ /^collectionConfig\.xml$/) {
215 next unless $gs3mode;
216 }
217
218 &gsprintf(STDOUT, "{mkcol.doing_replacements}\n", $destfile)
219 unless $quiet;
220 $destfile = &util::filename_cat ($coldir, $destfile);
221
222 open (INFILE, $thisfile) ||
223 (&gsprintf(STDERR, "{common.cannot_read_file}\n", $thisfile) && die);
224 open (OUTFILE, ">$destfile") ||
225 (&gsprintf(STDERR, "{common.cannot_create_file}\n", $destfile) && die);
226
227 while (defined (my $line = <INFILE>)) {
228 $line =~ s/\*\*collection\*\*/$collection_tail/g;
229 $line =~ s/\*\*COLLECTION\*\*/$capcollection_tail/g;
230 $line =~ s/\*\*creator\*\*/$creator/g;
231 $line =~ s/\*\*maintainer\*\*/$maintainer/g;
232 $line =~ s/\*\*public\*\*/$public/g;
233 $line =~ s/\*\*title\*\*/$title/g;
234 $line =~ s/\*\*about\*\*/$about/g;
235 if (!$gs3mode) {
236 $line =~ s/\*\*plugins\*\*/$pluginstring/g;
237 }
238
239 print OUTFILE $line;
240 }
241
242 close (OUTFILE);
243 close (INFILE);
244 }
245 }
246}
247
248
249sub main {
250
251 my $xml = 0;
252
253
254 my $hashParsingResult = {};
255 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
256
257 # If parse returns -1 then something has gone wrong
258 if ($intArgLeftinAfterParsing == -1)
259 {
260 &PrintUsage::print_txt_usage($options, "{mkcol.params}");
261 die "\n";
262 }
263
264 foreach my $strVariable (keys %$hashParsingResult)
265 {
266 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
267 }
268
269 # If $language has been specified, load the appropriate resource bundle
270 # (Otherwise, the default resource bundle will be loaded automatically)
271 if ($language && $language =~ /\S/) {
272 &gsprintf::load_language_specific_resource_bundle($language);
273 }
274
275 if ($xml) {
276 &PrintUsage::print_xml_usage($options);
277 print "\n";
278 return;
279 }
280
281 if ($gli) { # the gli wants strings to be in UTF-8
282 &gsprintf::output_strings_in_UTF8;
283 }
284
285 # now check that we had exactly one leftover arg, which should be
286 # the collection name. We don't want to do this earlier, cos
287 # -xml arg doesn't need a collection name
288 # Or if the user specified -h, then we output the usage also
289 if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
290 {
291 &PrintUsage::print_txt_usage($options, "{mkcol.params}");
292 die "\n";
293 }
294
295 if ($optionfile =~ /\w/) {
296 open (OPTIONS, $optionfile) ||
297 (&gsprintf(STDERR, "{common.cannot_open}\n", $optionfile) && die);
298 my $line = [];
299 my $options = [];
300 while (defined ($line = &cfgread::read_cfg_line ('mkcol::OPTIONS'))) {
301 push (@$options, @$line);
302 }
303 close OPTIONS;
304 my $optionsParsingResult = {};
305 if (parse2::parse($options,$arguments,$optionsParsingResult) == -1) {
306 &PrintUsage::print_txt_usage($options, "{mkcol.params}");
307 die "\n";
308 }
309
310 foreach my $strVariable (keys %$optionsParsingResult)
311 {
312 eval "\$$strVariable = \$optionsParsingResult->{\"\$strVariable\"}";
313 }
314 }
315
316 # load default plugins if none were on command line
317 if (!scalar(@plugin)) {
318 @plugin = (ZIPPlugin,GreenstoneXMLPlugin,TextPlugin,HTMLPlugin,EmailPlugin,
319 PDFPlugin,RTFPlugin,WordPlugin,PostScriptPlugin,PowerPointPlugin,ExcelPlugin,ImagePlugin,ISISPlugin,NulPlugin,MetadataXMLPlugin,ArchivesInfPlugin,DirectoryPlugin);
320 }
321
322 # get and check the collection name
323 ($collection) = @ARGV;
324
325 # get capitalised version of the collection
326 $capcollection = $collection;
327 $capcollection =~ tr/a-z/A-Z/;
328
329 $collection_tail = &util::get_dirsep_tail($collection);
330 $capcollection_tail = &util::get_dirsep_tail($capcollection);
331
332
333 if (!defined($collection)) {
334 &gsprintf(STDOUT, "{mkcol.no_colname}\n");
335 &PrintUsage::print_txt_usage($options, "{mkcol.params}");
336 die "\n";
337 }
338
339 if (($win31compat eq "true") && (length($collection_tail)) > 8) {
340 &gsprintf(STDOUT, "{mkcol.long_colname}\n");
341 die "\n";
342 }
343
344 if ($collection eq "modelcol") {
345 &gsprintf(STDOUT, "{mkcol.bad_name_modelcol}\n");
346 die "\n";
347 }
348
349 if ($collection_tail eq "CVS") {
350 &gsprintf(STDOUT, "{mkcol.bad_name_cvs}\n");
351 die "\n";
352 }
353
354 if ($collection_tail eq ".svn") {
355 &gsprintf(STDOUT, "{mkcol.bad_name_svn}\n");
356 die "\n";
357 }
358
359 if (defined($creator) && (!defined($maintainer) || $maintainer eq "")) {
360 $maintainer = $creator;
361 }
362
363 $public = "true" unless defined $public;
364
365 if (!defined($title) || $title eq "") {
366 $title = $collection_tail;
367 }
368
369 if ($gs3mode && $group) {
370 &gsprintf(STDERR,"{mkcol.group_not_valid_in_gs3}\n");
371 die "\n";
372 }
373
374 # get the strings to include.
375 $pluginstring = "";
376 foreach my $plug (@plugin) {
377 $pluginstring .= "plugin $plug\n";
378 }
379
380 if ($gs3mode) {
381 if (!defined $site) {
382 print STDERR "Warning: -gs3mode is deprecated.\n";
383 print STDERR "Use -site <name> instead to create a Greenstone 3 collection\n";
384 }
385 }
386 else {
387 # gs3mode not set
388 if (defined $site && $site =~ /\w/) {
389 # Using -site, so -gs3mode implicitly is true
390 $gs3mode = 1;
391 }
392 }
393
394 my $mdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", "modelcol");
395 my $cdir;
396 if (defined $collectdir && $collectdir =~ /\w/) {
397 if (!-d $collectdir) {
398 &gsprintf(STDOUT, "{mkcol.no_collectdir}\n", $collectdir);
399 die "\n";
400 }
401 $cdir = &util::filename_cat ($collectdir, $collection);
402 } else {
403 if (!$gs3mode) {
404 $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
405 }else {
406 if (defined $site && $site =~ /\w/) {
407 die "GSDL3HOME not set\n" unless defined $ENV{'GSDL3HOME'};
408
409 $cdir = &util::filename_cat($ENV{'GSDL3HOME'}, "sites", $site, "collect");
410 if (!-d $cdir) {
411 &gsprintf(STDOUT, "{mkcol.no_collectdir}\n", $cdir);
412 die "\n";
413 }
414 $cdir = &util::filename_cat ($cdir, $collection);
415 } else {
416 &gsprintf(STDOUT, "{mkcol.no_collectdir_specified}\n");
417 die "\n";
418 }
419 }
420 }
421
422 # make sure the model collection exists
423 (&gsprintf(STDERR, "{mkcol.cannot_find_modelcol}\n", $mdir) && die) unless (-d $mdir);
424
425 # make sure this collection does not already exist
426 if (-e $cdir) {
427 &gsprintf(STDOUT, "{mkcol.col_already_exists}\n");
428 die "\n";
429 }
430
431 # start creating the collection
432 &gsprintf(STDOUT, "\n{mkcol.creating_col}...\n", $collection)
433 unless $quiet;
434
435 &traverse_dir ($mdir, $cdir);
436 &gsprintf(STDOUT, "\n{mkcol.success}\n", $cdir)
437 unless $quiet;
438}
439
440
Note: See TracBrowser for help on using the repository browser.