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 |
|
---|
33 | package mkcol;
|
---|
34 |
|
---|
35 | BEGIN {
|
---|
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 |
|
---|
41 | use parse2;
|
---|
42 | use util;
|
---|
43 | use cfgread;
|
---|
44 | use gsprintf 'gsprintf';
|
---|
45 | use printusage;
|
---|
46 |
|
---|
47 | use strict;
|
---|
48 | no strict 'subs'; # allow barewords (eg STDERR) as function arguments
|
---|
49 |
|
---|
50 | my $public_list =
|
---|
51 | [ { 'name' => "true",
|
---|
52 | 'desc' => "{mkcol.public.true}"},
|
---|
53 | { 'name' => "false",
|
---|
54 | 'desc' => "{mkcol.public.false}"}
|
---|
55 | ];
|
---|
56 |
|
---|
57 | my $win31compat_list =
|
---|
58 | [ { 'name' => "true",
|
---|
59 | 'desc' => "{mkcol.win31compat.true}"},
|
---|
60 | { 'name' => "false",
|
---|
61 | 'desc' => "{mkcol.win31compat.false}"}
|
---|
62 | ];
|
---|
63 |
|
---|
64 | my $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 |
|
---|
137 | my $options = { 'name' => "mkcol.pl",
|
---|
138 | 'desc' => "{mkcol.desc}",
|
---|
139 | 'args' => $arguments };
|
---|
140 |
|
---|
141 | # options
|
---|
142 | my ($creator, $optionfile, $maintainer, $gs3mode, $group, $collectdir, $site,
|
---|
143 | $public,
|
---|
144 | $title, $about, $plugin, $quiet, $language, $win31compat, $gli);
|
---|
145 |
|
---|
146 | #other variables
|
---|
147 | my ($collection, $capcollection,
|
---|
148 | $collection_tail, $capcollection_tail,
|
---|
149 | $pluginstring, @plugin);
|
---|
150 |
|
---|
151 | &main();
|
---|
152 |
|
---|
153 |
|
---|
154 | sub 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 |
|
---|
249 | sub 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 | my $mdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", "modelcol");
|
---|
381 | my $cdir;
|
---|
382 | if (defined $collectdir && $collectdir =~ /\w/) {
|
---|
383 | if (!-d $collectdir) {
|
---|
384 | &gsprintf(STDOUT, "{mkcol.no_collectdir}\n", $collectdir);
|
---|
385 | die "\n";
|
---|
386 | }
|
---|
387 | $cdir = &util::filename_cat ($collectdir, $collection);
|
---|
388 | } else {
|
---|
389 | if (!$gs3mode) {
|
---|
390 | $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
391 | }else {
|
---|
392 | if (defined $site && $site =~ /\w/) {
|
---|
393 | die "GSDL3HOME not set\n" unless defined $ENV{'GSDL3HOME'};
|
---|
394 |
|
---|
395 | $cdir = &util::filename_cat($ENV{'GSDL3HOME'}, "sites", $site, "collect");
|
---|
396 | if (!-d $cdir) {
|
---|
397 | &gsprintf(STDOUT, "{mkcol.no_collectdir}\n", $cdir);
|
---|
398 | die "\n";
|
---|
399 | }
|
---|
400 | $cdir = &util::filename_cat ($cdir, $collection);
|
---|
401 | } else {
|
---|
402 | &gsprintf(STDOUT, "{mkcol.no_collectdir_specified}\n");
|
---|
403 | die "\n";
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | # make sure the model collection exists
|
---|
409 | (&gsprintf(STDERR, "{mkcol.cannot_find_modelcol}\n", $mdir) && die) unless (-d $mdir);
|
---|
410 |
|
---|
411 | # make sure this collection does not already exist
|
---|
412 | if (-e $cdir) {
|
---|
413 | &gsprintf(STDOUT, "{mkcol.col_already_exists}\n");
|
---|
414 | die "\n";
|
---|
415 | }
|
---|
416 |
|
---|
417 | # start creating the collection
|
---|
418 | &gsprintf(STDOUT, "\n{mkcol.creating_col}...\n", $collection)
|
---|
419 | unless $quiet;
|
---|
420 |
|
---|
421 | &traverse_dir ($mdir, $cdir);
|
---|
422 | &gsprintf(STDOUT, "\n{mkcol.success}\n", $cdir)
|
---|
423 | unless $quiet;
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|