source: main/tags/2.40/gsdl/bin/script/mkcol.pl@ 21085

Last change on this file since 21085 was 4776, checked in by mdewsnip, 21 years ago

Now uses the PrintUsage module to automatically generate usage text from the $options and $arguments structures.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 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 parsargv;
42use util;
43use cfgread;
44use printusage;
45
46my $arguments =
47 [ { 'name' => "creator",
48 'desc' => "The collection creator's e-mail address.",
49 'type' => "string",
50 'reqd' => "yes" },
51 { 'name' => "optionfile",
52 'desc' => "Get options from file, useful on systems where long command lines may cause problems.",
53 'type' => "string",
54 'reqd' => "no" },
55 { 'name' => "maintainer",
56 'desc' => "The collection maintainer's email address (if different from the creator).",
57 'type' => "string",
58 'reqd' => "no" },
59 { 'name' => "collectdir",
60 'desc' => "Directory where new collection will be created.",
61 'type' => "string",
62 'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
63 'reqd' => "no" },
64 { 'name' => "public",
65 'desc' => "If this collection has anonymous access (true/false).",
66 'type' => "string",
67 'deft' => "true",
68 'reqd' => "no" },
69 { 'name' => "title",
70 'desc' => "The title of the collection.",
71 'type' => "string",
72 'reqd' => "no" },
73 { 'name' => "about",
74 'desc' => "The about text for the collection.",
75 'type' => "string",
76 'reqd' => "no" },
77 { 'name' => "plugin",
78 'desc' => "Perl plugin module to use (there may be multiple plugin entries).",
79 'type' => "string",
80 'reqd' => "no" },
81 { 'name' => "quiet",
82 'desc' => "Operate quietly.",
83 'type' => "flag",
84 'reqd' => "no" } ];
85
86my $options = { 'name' => "mkcol.pl",
87 'desc' => "PERL script used to create the directory structure for a new Greenstone collection.",
88 'args' => $arguments };
89
90
91sub print_xml_usage
92{
93 &PrintUsage::print_xml_header();
94
95 print STDERR "<Info>\n";
96 print STDERR " <Name>$options->{'name'}</Name>\n";
97 print STDERR " <Desc>$options->{'desc'}</Desc>\n";
98 print STDERR " <Arguments>\n";
99 if (defined($options->{'args'})) {
100 &PrintUsage::print_options_xml($options->{'args'});
101 }
102 print STDERR " </Arguments>\n";
103 print STDERR "</Info>\n";
104}
105
106
107sub print_txt_usage
108{
109 local $programname = $options->{'name'};
110 local $programargs = $options->{'args'};
111
112 # Find the length of the longest option string
113 local $descoffset = 0;
114 if (defined($programargs)) {
115 $descoffset = &PrintUsage::find_longest_option_string($programargs);
116 }
117
118 # Produce the usage information using the data structure above
119 print STDERR " usage: $programname [options] collection-name\n\n";
120
121 # Display the program options, if there are some
122 if (defined($programargs)) {
123 # Calculate the column offset of the option descriptions
124 local $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
125
126 print STDERR " options:\n";
127
128 # Display the program options
129 &PrintUsage::print_options_txt($programargs, $optiondescoffset);
130 }
131}
132
133
134# sub print_usage {
135# print STDOUT "\n";
136# print STDOUT "mkcol.pl: Creates the directory structure for a new\n";
137# print STDOUT " Greenstone collection.\n\n";
138# print STDOUT " usage: $0 -creator email [options] collection-name\n\n";
139# print STDOUT " options:\n";
140# print STDOUT " -optionfile file Get options from file, useful on systems where\n";
141# print STDOUT " long command lines may cause problems\n";
142# print STDOUT " -collectdir Directory where new collection will be created.\n";
143# print STDOUT " Default is " .
144# &util::filename_cat($ENV{'GSDLHOME'}, "collect") . "\n";
145# print STDOUT " -maintainer email The collection maintainer's email address (if\n";
146# print STDOUT " different from the creator)\n";
147# print STDOUT " -public true|false If this collection has anonymous access\n";
148# print STDOUT " -title text The title for the collection\n";
149# print STDOUT " -about text The about text for the collection\n";
150# print STDOUT " -plugin text perl plugin module to use (there may be multiple\n";
151# print STDOUT " plugin entries)\n";
152# print STDOUT " -quiet Operate quietly\n";
153# print STDOUT " Note that -creator must be specified. You can make changes to all\n";
154# print STDOUT " options later by editing the collect.cfg configuration file for your\n";
155# print STDOUT " new collection (it'll be in the \"etc\" directory).\n\n";
156# print STDOUT " [Type \"perl -S mkcol.pl | more\" if this help text scrolled off your screen]";
157# print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
158# }
159
160sub traverse_dir
161{
162 my ($modeldir, $coldir) = @_;
163 my ($newfile, @filetext);
164
165 if (!(-e $coldir)) {
166
167 my $store_umask = umask(0002);
168 my $mkdir_ok = mkdir ($coldir, 0777);
169 umask($store_umask);
170
171 if (!$mkdir_ok)
172 {
173 die "$!";
174 }
175 }
176
177 opendir(DIR, $modeldir) || die "Can't read $modeldir";
178 my @files = grep(!/^(\.\.?|CVS)$/, readdir(DIR));
179 closedir(DIR);
180
181 foreach $file (@files)
182 {
183 my $thisfile = &util::filename_cat ($modeldir, $file);
184 if (-d $thisfile) {
185 my $colfiledir = &util::filename_cat ($coldir, $file);
186 traverse_dir ($thisfile, $colfiledir);
187
188 } else {
189 my $destfile = $file;
190 $destfile =~ s/^modelcol/$collection/;
191 $destfile =~ s/^MODELCOL/$capcollection/;
192 print STDOUT " doing replacements for $destfile\n" unless $quiet;
193 $destfile = &util::filename_cat ($coldir, $destfile);
194
195 open (INFILE, $thisfile) ||
196 die "ERROR: Can't read file $thisfile";
197 open (OUTFILE, ">$destfile") ||
198 die "ERROR: Can't create file $destfile";
199
200 while (defined ($line = <INFILE>)) {
201 $line =~ s/\*\*collection\*\*/$collection/g;
202 $line =~ s/\*\*COLLECTION\*\*/$capcollection/g;
203 $line =~ s/\*\*creator\*\*/$creator/g;
204 $line =~ s/\*\*maintainer\*\*/$maintainer/g;
205 $line =~ s/\*\*public\*\*/$public/g;
206 $line =~ s/\*\*title\*\*/$title/g;
207 $line =~ s/\*\*about\*\*/$about/g;
208 $line =~ s/\*\*plugins\*\*/$pluginstring/g;
209
210 print OUTFILE $line;
211 }
212
213 close (OUTFILE);
214 close (INFILE);
215 }
216 }
217}
218
219# get and check options
220sub parse_args {
221 my ($argref) = @_;
222 if (!&parsargv::parse($argref,
223 'optionfile/.*/', \$optionfile,
224 'collectdir/.*/', \$collectdir,
225 'creator/\w+\@[\w\.]+/', \$creator,
226 'maintainer/\w+\@[\w\.]+/', \$maintainer,
227 'public/true|false/true', \$public,
228 'title/.+/', \$title,
229 'about/.+/', \$about,
230 'plugin/.+', \@plugin,
231 'quiet', \$quiet,
232 q^xml^, \$xml
233 )) {
234 &print_txt_usage();
235 die "\n";
236 }
237}
238
239sub main {
240
241 &parse_args (\@ARGV);
242
243 if ($xml) {
244 &print_xml_usage();
245 die "\n";
246 }
247
248 if ($optionfile =~ /\w/) {
249 open (OPTIONS, $optionfile) || die "Couldn't open $optionfile\n";
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 &parse_args ($options);
257
258 }
259
260 # load default plugins if none were on command line
261 if (!scalar(@plugin)) {
262 @plugin = (ZIPPlug,GAPlug,TEXTPlug,HTMLPlug,EMAILPlug,
263 PDFPlug,RTFPlug,WordPlug,PSPlug,ArcPlug,RecPlug);
264 }
265
266 # get and check the collection name
267 ($collection) = @ARGV;
268 if (!defined($collection)) {
269 print STDOUT "ERROR: no collection name was specified\n";
270 &print_txt_usage();
271 die "\n";
272 }
273
274 if (length($collection) > 8) {
275 print STDOUT "ERROR: The collection name must be less than 8 characters\n";
276 print STDOUT " so compatibility with earlier filesystems can be\n";
277 print STDOUT " maintained.\n";
278 die "\n";
279 }
280
281 if ($collection eq "modelcol") {
282 print STDOUT "ERROR: No collection can be named modelcol as this is the\n";
283 print STDOUT " name of the model collection.\n";
284 die "\n";
285 }
286
287 if ($collection eq "CVS") {
288 print STDOUT "ERROR: No collection can be named CVS as this may interfere\n";
289 print STDOUT " with directories created by the CVS versioning system\n";
290 die "\n";
291 }
292
293 if (!defined($creator) || $creator eq "") {
294 print STDOUT "ERROR: The creator was not defined. This variable is\n";
295 print STDOUT " needed to recognise duplicate collection names.\n";
296 die "\n";
297 }
298
299 if (!defined($maintainer) || $maintainer eq "") {
300 $maintainer = $creator;
301 }
302
303 $public = "true" unless defined $public;
304
305 if (!defined($title) || $title eq "") {
306 $title = $collection;
307 }
308
309 # get capitalised version of the collection
310 $capcollection = $collection;
311 $capcollection =~ tr/a-z/A-Z/;
312
313 # get the strings to include.
314 $pluginstring = "";
315 foreach $plugin (@plugin) {
316 $pluginstring .= "plugin $plugin\n";
317 }
318
319 $mdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", "modelcol");
320 if (defined $collectdir && $collectdir =~ /\w/) {
321 if (!-d $collectdir) {
322 print STDOUT "ERROR: $collectdir doesn't exist\n";
323 die "\n";
324 }
325 $cdir = &util::filename_cat ($collectdir, $collection);
326 } else {
327 $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
328 }
329
330 # make sure the model collection exists
331 die "ERROR: Cannot find the model collection $mdir" unless (-d $mdir);
332
333 # make sure this collection does not already exist
334 if (-e $cdir) {
335 print STDOUT "ERROR: This collection already exists\n";
336 die "\n";
337 }
338
339 # start creating the collection
340 print STDOUT "\nCreating the collection $collection...\n" unless $quiet;
341 &traverse_dir ($mdir, $cdir);
342 print STDOUT "\nThe new collection was created successfully at\n" unless $quiet;
343 print STDOUT "$cdir\n" unless $quiet;
344}
345
346&main ();
Note: See TracBrowser for help on using the repository browser.