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' => "",
|
---|
79 | 'type' => "flag",
|
---|
80 | 'reqd' => "no" },
|
---|
81 | { 'name' => "collectdir",
|
---|
82 | 'desc' => "{mkcol.collectdir}",
|
---|
83 | 'type' => "string",
|
---|
84 | 'reqd' => "no" }, # For gs3, this collectdir must be provided.
|
---|
85 | { 'name' => "public",
|
---|
86 | 'desc' => "{mkcol.public}",
|
---|
87 | 'type' => "enum",
|
---|
88 | 'deft' => "true",
|
---|
89 | 'list' => $public_list,
|
---|
90 | 'reqd' => "no" },
|
---|
91 | { 'name' => "title",
|
---|
92 | 'desc' => "{mkcol.title}",
|
---|
93 | 'type' => "string",
|
---|
94 | 'reqd' => "no" },
|
---|
95 | { 'name' => "about",
|
---|
96 | 'desc' => "{mkcol.about}",
|
---|
97 | 'type' => "string",
|
---|
98 | 'reqd' => "no" },
|
---|
99 | { 'name' => "plugin",
|
---|
100 | 'desc' => "{mkcol.plugin}",
|
---|
101 | 'type' => "string",
|
---|
102 | 'reqd' => "no" },
|
---|
103 | { 'name' => "quiet",
|
---|
104 | 'desc' => "{mkcol.quiet}",
|
---|
105 | 'type' => "flag",
|
---|
106 | 'reqd' => "no" },
|
---|
107 | { 'name' => "language",
|
---|
108 | 'desc' => "{scripts.language}",
|
---|
109 | 'type' => "string",
|
---|
110 | 'reqd' => "no" },
|
---|
111 | { 'name' => "win31compat",
|
---|
112 | 'desc' => "{mkcol.win31compat}",
|
---|
113 | 'type' => "enum",
|
---|
114 | 'deft' => "true",
|
---|
115 | 'list' => $win31compat_list,
|
---|
116 | 'reqd' => "no" },
|
---|
117 | { 'name' => "gli",
|
---|
118 | 'desc' => "",
|
---|
119 | 'type' => "flag",
|
---|
120 | 'reqd' => "no",
|
---|
121 | 'hiddengli' => "yes" },
|
---|
122 | { 'name' => "xml",
|
---|
123 | 'desc' => "{scripts.xml}",
|
---|
124 | 'type' => "flag",
|
---|
125 | 'reqd' => "no",
|
---|
126 | 'hiddengli' => "yes" }
|
---|
127 | ];
|
---|
128 |
|
---|
129 | my $options = { 'name' => "mkcol.pl",
|
---|
130 | 'desc' => "{mkcol.desc}",
|
---|
131 | 'args' => $arguments };
|
---|
132 |
|
---|
133 | # options
|
---|
134 | my ($creator, $optionfile, $maintainer, $gs3mode, $collectdir, $public,
|
---|
135 | $title, $about, $plugin, $quiet, $language, $win31compat, $gli);
|
---|
136 |
|
---|
137 | #other variables
|
---|
138 | my ($collection, $capcollection, $pluginstring, @plugin);
|
---|
139 |
|
---|
140 | &main();
|
---|
141 |
|
---|
142 |
|
---|
143 | sub traverse_dir
|
---|
144 | {
|
---|
145 | my ($modeldir, $coldir, $gs3) = @_;
|
---|
146 | my ($newfile, @filetext);
|
---|
147 |
|
---|
148 | if (!(-e $coldir)) {
|
---|
149 |
|
---|
150 |
|
---|
151 | my $store_umask = umask(0002);
|
---|
152 | my $mkdir_ok = mkdir ($coldir, 0777);
|
---|
153 | umask($store_umask);
|
---|
154 |
|
---|
155 | if (!$mkdir_ok)
|
---|
156 | {
|
---|
157 | die "$!";
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | opendir(DIR, $modeldir) ||
|
---|
162 | (&gsprintf(STDERR, "{common.cannot_read}\n", $modeldir) && die);
|
---|
163 | my @files = grep(!/^(\.\.?|CVS)$/, readdir(DIR));
|
---|
164 | closedir(DIR);
|
---|
165 |
|
---|
166 | foreach my $file (@files)
|
---|
167 | {
|
---|
168 | my $thisfile = &util::filename_cat ($modeldir, $file);
|
---|
169 |
|
---|
170 | if (-d $thisfile) {
|
---|
171 | my $colfiledir = &util::filename_cat ($coldir, $file);
|
---|
172 | traverse_dir ($thisfile, $colfiledir, $gs3);
|
---|
173 |
|
---|
174 | } else {
|
---|
175 |
|
---|
176 | # There are only two configuration files in modelcol directory:
|
---|
177 | # collect.cfg and collectionConfig.xml.
|
---|
178 | # If it is gs2, copy collect.cfg file; if gs3, copy collectionConfig.xml.
|
---|
179 | if ( ($gs3 && $file =~ /collect\.cfg/) || (!$gs3 && $file =~ /collectionConfig\.xml/) ) {
|
---|
180 |
|
---|
181 | next;
|
---|
182 | }
|
---|
183 | my $destfile = $file;
|
---|
184 | $destfile =~ s/^modelcol/$collection/;
|
---|
185 | $destfile =~ s/^MODELCOL/$capcollection/;
|
---|
186 | &gsprintf(STDOUT, "{mkcol.doing_replacements}\n", $destfile)
|
---|
187 | unless $quiet;
|
---|
188 | $destfile = &util::filename_cat ($coldir, $destfile);
|
---|
189 |
|
---|
190 | open (INFILE, $thisfile) ||
|
---|
191 | (&gsprintf(STDERR, "{common.cannot_read_file}\n", $thisfile) && die);
|
---|
192 | open (OUTFILE, ">$destfile") ||
|
---|
193 | (&gsprintf(STDERR, "{common.cannot_create_file}\n", $destfile) && die);
|
---|
194 |
|
---|
195 | while (defined (my $line = <INFILE>)) {
|
---|
196 | $line =~ s/\*\*collection\*\*/$collection/g;
|
---|
197 | $line =~ s/\*\*COLLECTION\*\*/$capcollection/g;
|
---|
198 | $line =~ s/\*\*creator\*\*/$creator/g;
|
---|
199 | $line =~ s/\*\*maintainer\*\*/$maintainer/g;
|
---|
200 | $line =~ s/\*\*public\*\*/$public/g;
|
---|
201 | $line =~ s/\*\*title\*\*/$title/g;
|
---|
202 | $line =~ s/\*\*about\*\*/$about/g;
|
---|
203 | if (!$gs3) {
|
---|
204 | $line =~ s/\*\*plugins\*\*/$pluginstring/g;
|
---|
205 | }
|
---|
206 |
|
---|
207 | print OUTFILE $line;
|
---|
208 | }
|
---|
209 |
|
---|
210 | close (OUTFILE);
|
---|
211 | close (INFILE);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | sub main {
|
---|
218 |
|
---|
219 | my $xml = 0;
|
---|
220 |
|
---|
221 |
|
---|
222 | my $hashParsingResult = {};
|
---|
223 | my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
|
---|
224 |
|
---|
225 | # If parse returns -1 then something has gone wrong
|
---|
226 | if ($intArgLeftinAfterParsing == -1)
|
---|
227 | {
|
---|
228 | &PrintUsage::print_txt_usage($options, "{mkcol.params}");
|
---|
229 | die "\n";
|
---|
230 | }
|
---|
231 |
|
---|
232 | foreach my $strVariable (keys %$hashParsingResult)
|
---|
233 | {
|
---|
234 | eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
|
---|
235 | }
|
---|
236 |
|
---|
237 | # If $language has been specified, load the appropriate resource bundle
|
---|
238 | # (Otherwise, the default resource bundle will be loaded automatically)
|
---|
239 | if ($language && $language =~ /\S/) {
|
---|
240 | &gsprintf::load_language_specific_resource_bundle($language);
|
---|
241 | }
|
---|
242 |
|
---|
243 | if ($xml) {
|
---|
244 | &PrintUsage::print_xml_usage($options);
|
---|
245 | print "\n";
|
---|
246 | return;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if ($gli) { # the gli wants strings to be in UTF-8
|
---|
250 | &gsprintf::output_strings_in_UTF8;
|
---|
251 | }
|
---|
252 |
|
---|
253 | # now check that we had exactly one leftover arg, which should be
|
---|
254 | # the collection name. We don't want to do this earlier, cos
|
---|
255 | # -xml arg doesn't need a collection name
|
---|
256 | # Or if the user specified -h, then we output the usage also
|
---|
257 | if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
|
---|
258 | {
|
---|
259 | &PrintUsage::print_txt_usage($options, "{mkcol.params}");
|
---|
260 | die "\n";
|
---|
261 | }
|
---|
262 |
|
---|
263 | if ($optionfile =~ /\w/) {
|
---|
264 | open (OPTIONS, $optionfile) ||
|
---|
265 | (&gsprintf(STDERR, "{common.cannot_open}\n", $optionfile) && die);
|
---|
266 | my $line = [];
|
---|
267 | my $options = [];
|
---|
268 | while (defined ($line = &cfgread::read_cfg_line ('mkcol::OPTIONS'))) {
|
---|
269 | push (@$options, @$line);
|
---|
270 | }
|
---|
271 | close OPTIONS;
|
---|
272 | my $optionsParsingResult = {};
|
---|
273 | if (parse2::parse($options,$arguments,$optionsParsingResult) == -1) {
|
---|
274 | &PrintUsage::print_txt_usage($options, "{mkcol.params}");
|
---|
275 | die "\n";
|
---|
276 | }
|
---|
277 |
|
---|
278 | foreach my $strVariable (keys %$optionsParsingResult)
|
---|
279 | {
|
---|
280 | eval "\$$strVariable = \$optionsParsingResult->{\"\$strVariable\"}";
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | # load default plugins if none were on command line
|
---|
285 | if (!scalar(@plugin)) {
|
---|
286 | @plugin = (ZIPPlug,GAPlug,TEXTPlug,"HTMLPlug -smart_block","EMAILPlug",
|
---|
287 | PDFPlug,RTFPlug,WordPlug,PSPlug,ImagePlug,ISISPlug,NULPlug,MetadataXMLPlug,ArcPlug,RecPlug);
|
---|
288 | }
|
---|
289 |
|
---|
290 | # get and check the collection name
|
---|
291 | ($collection) = @ARGV;
|
---|
292 | if (!defined($collection)) {
|
---|
293 | &gsprintf(STDOUT, "{mkcol.no_colname}\n");
|
---|
294 | &PrintUsage::print_txt_usage($options, "{mkcol.params}");
|
---|
295 | die "\n";
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (($win31compat eq "true") && (length($collection)) > 8) {
|
---|
299 | &gsprintf(STDOUT, "{mkcol.long_colname}\n");
|
---|
300 | die "\n";
|
---|
301 | }
|
---|
302 |
|
---|
303 | if ($collection eq "modelcol") {
|
---|
304 | &gsprintf(STDOUT, "{mkcol.bad_name_modelcol}\n");
|
---|
305 | die "\n";
|
---|
306 | }
|
---|
307 |
|
---|
308 | if ($collection eq "CVS") {
|
---|
309 | &gsprintf(STDOUT, "{mkcol.bad_name_cvs}\n");
|
---|
310 | die "\n";
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (defined($creator) && (!defined($maintainer) || $maintainer eq "")) {
|
---|
314 | $maintainer = $creator;
|
---|
315 | }
|
---|
316 |
|
---|
317 | $public = "true" unless defined $public;
|
---|
318 |
|
---|
319 | if (!defined($title) || $title eq "") {
|
---|
320 | $title = $collection;
|
---|
321 | }
|
---|
322 |
|
---|
323 | # get capitalised version of the collection
|
---|
324 | $capcollection = $collection;
|
---|
325 | $capcollection =~ tr/a-z/A-Z/;
|
---|
326 |
|
---|
327 | # get the strings to include.
|
---|
328 | $pluginstring = "";
|
---|
329 | foreach my $plug (@plugin) {
|
---|
330 | $pluginstring .= "plugin $plug\n";
|
---|
331 | }
|
---|
332 |
|
---|
333 | my $mdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", "modelcol");
|
---|
334 | my $cdir;
|
---|
335 | if (defined $collectdir && $collectdir =~ /\w/) {
|
---|
336 | if (!-d $collectdir) {
|
---|
337 | &gsprintf(STDOUT, "{mkcol.no_collectdir}\n", $collectdir);
|
---|
338 | die "\n";
|
---|
339 | }
|
---|
340 | $cdir = &util::filename_cat ($collectdir, $collection);
|
---|
341 | } else {
|
---|
342 | if (!$gs3mode) {
|
---|
343 | $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
344 | }else {
|
---|
345 | &gsprintf(STDOUT, "{mkcol.no_collectdir}\n");
|
---|
346 | die "In gs3mode, '-collectdir <directory>' must be specified.\n";
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | # make sure the model collection exists
|
---|
351 | (&gsprintf(STDERR, "{mkcol.cannot_find_modelcol}\n", $mdir) && die) unless (-d $mdir);
|
---|
352 |
|
---|
353 | # make sure this collection does not already exist
|
---|
354 | if (-e $cdir) {
|
---|
355 | &gsprintf(STDOUT, "{mkcol.col_already_exists}\n");
|
---|
356 | die "\n";
|
---|
357 | }
|
---|
358 |
|
---|
359 | # start creating the collection
|
---|
360 | &gsprintf(STDOUT, "\n{mkcol.creating_col}...\n", $collection)
|
---|
361 | unless $quiet;
|
---|
362 |
|
---|
363 | &traverse_dir ($mdir, $cdir, $gs3mode);
|
---|
364 | &gsprintf(STDOUT, "\n{mkcol.success}\n", $cdir)
|
---|
365 | unless $quiet;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|