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

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

Options supported by mkcol.pl expanding to include -buildtype and -infodbtype

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