source: trunk/gsdl/bin/script/mkcol.pl@ 13186

Last change on this file since 13186 was 13186, checked in by kjdon, 17 years ago

removed RecPlug -use_metadata_files option, added MetadataXMLPlug

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