1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | ###########################################################################
|
---|
4 | #
|
---|
5 | # gs2_mkcol.pl -- create the framework for a gs2 compatible collection in
|
---|
6 | # gs3
|
---|
7 | # A component of the Greenstone digital library software
|
---|
8 | # from the New Zealand Digital Library Project at the
|
---|
9 | # University of Waikato, New Zealand.
|
---|
10 | #
|
---|
11 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
12 | #
|
---|
13 | # This program is free software; you can redistribute it and/or modify
|
---|
14 | # it under the terms of the GNU General Public License as published by
|
---|
15 | # the Free Software Foundation; either version 2 of the License, or
|
---|
16 | # (at your option) any later version.
|
---|
17 | #
|
---|
18 | # This program is distributed in the hope that it will be useful,
|
---|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
21 | # GNU General Public License for more details.
|
---|
22 | #
|
---|
23 | # You should have received a copy of the GNU General Public License
|
---|
24 | # along with this program; if not, write to the Free Software
|
---|
25 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
26 | #
|
---|
27 | ###########################################################################
|
---|
28 |
|
---|
29 |
|
---|
30 | # This program will setup a new collection from a model one. It does this by
|
---|
31 | # copying the model, moving files to have the correct names, and replacing
|
---|
32 | # text within the files to match the parameters.
|
---|
33 |
|
---|
34 | package gs2_mkcol;
|
---|
35 |
|
---|
36 | BEGIN {
|
---|
37 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
38 | die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
|
---|
39 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
40 | }
|
---|
41 |
|
---|
42 | use parsargv;
|
---|
43 | use util;
|
---|
44 | use cfgread;
|
---|
45 |
|
---|
46 | sub print_usage {
|
---|
47 | print STDOUT "\n";
|
---|
48 | print STDOUT "gs2_mkcol.pl: Creates the directory structure for a new\n";
|
---|
49 | print STDOUT " Greenstone2 compatible collection in Greenstone3.\n\n";
|
---|
50 | print STDOUT " usage: $0 -site site-home -creator email [options] collection-name\n\n";
|
---|
51 | print STDOUT " options:\n";
|
---|
52 | print STDOUT " -optionfile file Get options from file, useful on systems where\n";
|
---|
53 | print STDOUT " long command lines may cause problems\n";
|
---|
54 | print STDOUT " -collectdir Directory where new collection will be created.\n";
|
---|
55 | print STDOUT " Default is " .
|
---|
56 | &util::filename_cat("site-home", "collect") . "\n";
|
---|
57 | print STDOUT " -maintainer email The collection maintainer's email address (if\n";
|
---|
58 | print STDOUT " different from the creator)\n";
|
---|
59 | print STDOUT " -public true|false If this collection has anonymous access\n";
|
---|
60 | print STDOUT " -buildtype mg|mgpp Whether to use mg or mgpp to build the collection\n";
|
---|
61 | print STDOUT " Default is mgpp.\n";
|
---|
62 | print STDOUT " -title text The title for the collection\n";
|
---|
63 | print STDOUT " -about text The about text for the collection\n";
|
---|
64 | print STDOUT " -plugin text perl plugin module to use (there may be multiple\n";
|
---|
65 | print STDOUT " plugin entries)\n";
|
---|
66 | print STDOUT " -quiet Operate quietly\n";
|
---|
67 | print STDOUT " Note that -creator and -site must be specified. You can make changes to all\n";
|
---|
68 | print STDOUT " options later by editing the collect.cfg configuration file for your\n";
|
---|
69 | print STDOUT " new collection (it'll be in the \"etc\" directory).\n\n";
|
---|
70 | print STDOUT " [Type \"perl -S mkcol.pl | more\" if this help text scrolled off your screen]";
|
---|
71 | print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
72 | }
|
---|
73 |
|
---|
74 | sub traverse_dir
|
---|
75 | {
|
---|
76 | my ($modeldir, $coldir) = @_;
|
---|
77 | my ($newfile, @filetext);
|
---|
78 |
|
---|
79 | if (!(-e $coldir)) {
|
---|
80 |
|
---|
81 | my $store_umask = umask(0002);
|
---|
82 | my $mkdir_ok = mkdir ($coldir, 0777);
|
---|
83 | umask($store_umask);
|
---|
84 |
|
---|
85 | if (!$mkdir_ok)
|
---|
86 | {
|
---|
87 | die "$!";
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | opendir(DIR, $modeldir) || die "Can't read $modeldir";
|
---|
92 | my @files = grep(!/^(\.\.?|CVS)$/, readdir(DIR));
|
---|
93 | closedir(DIR);
|
---|
94 |
|
---|
95 | foreach $file (@files)
|
---|
96 | {
|
---|
97 | my $thisfile = &util::filename_cat ($modeldir, $file);
|
---|
98 | if (-d $thisfile) {
|
---|
99 | my $colfiledir = &util::filename_cat ($coldir, $file);
|
---|
100 | traverse_dir ($thisfile, $colfiledir);
|
---|
101 |
|
---|
102 | } else {
|
---|
103 | my $destfile = $file;
|
---|
104 | $destfile =~ s/^modelcol/$collection/;
|
---|
105 | $destfile =~ s/^MODELCOL/$capcollection/;
|
---|
106 | print STDOUT " doing replacements for $destfile\n" unless $quiet;
|
---|
107 | $destfile = &util::filename_cat ($coldir, $destfile);
|
---|
108 |
|
---|
109 | open (INFILE, $thisfile) ||
|
---|
110 | die "ERROR: Can't read file $thisfile";
|
---|
111 | open (OUTFILE, ">$destfile") ||
|
---|
112 | die "ERROR: Can't create file $destfile";
|
---|
113 |
|
---|
114 | while (defined ($line = <INFILE>)) {
|
---|
115 | $line =~ s/\*\*collection\*\*/$collection/g;
|
---|
116 | $line =~ s/\*\*COLLECTION\*\*/$capcollection/g;
|
---|
117 | $line =~ s/\*\*creator\*\*/$creator/g;
|
---|
118 | $line =~ s/\*\*maintainer\*\*/$maintainer/g;
|
---|
119 | $line =~ s/\*\*public\*\*/$public/g;
|
---|
120 | $line =~ s/\*\*title\*\*/$title/g;
|
---|
121 | $line =~ s/\*\*about\*\*/$about/g;
|
---|
122 | $line =~ s/\*\*plugins\*\*/$pluginstring/g;
|
---|
123 | $line =~ s/\*\*buildtype\*\*/$buildtype/g;
|
---|
124 | $line =~ s/\*\*searchtype\*\*/$searchtype/g;
|
---|
125 | $line =~ s/\*\*indexes\*\*/$indexes/g;
|
---|
126 | $line =~ s/\*\*defaultindex\*\*/$defaultindex/g;
|
---|
127 | $line =~ s/\*\*indexmeta\*\*/$indexmeta/g;
|
---|
128 | $line =~ s/\*\*xmlindexes\*\*/$xmlindexes/g;
|
---|
129 | #$line =~ s/\*\*xmlplugins\*\*/$xmlpluginstring/g;
|
---|
130 |
|
---|
131 | print OUTFILE $line;
|
---|
132 | }
|
---|
133 |
|
---|
134 | close (OUTFILE);
|
---|
135 | close (INFILE);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | # get and check options
|
---|
141 | sub parse_args {
|
---|
142 | my ($argref) = @_;
|
---|
143 | if (!&parsargv::parse($argref,
|
---|
144 | 'optionfile/.*/', \$optionfile,
|
---|
145 | 'collectdir/.*/', \$collectdir,
|
---|
146 | 'site/.*/', \$sitehome,
|
---|
147 | 'creator/\w+\@[\w\.]+/', \$creator,
|
---|
148 | 'maintainer/\w+\@[\w\.]+/', \$maintainer,
|
---|
149 | 'public/true|false/true', \$public,
|
---|
150 | 'buildtype/mg|mgpp/mgpp', \$buildtype,
|
---|
151 | 'title/.+/', \$title,
|
---|
152 | 'about/.+/', \$about,
|
---|
153 | 'plugin/.+', \@plugin,
|
---|
154 | 'quiet', \$quiet,
|
---|
155 | )) {
|
---|
156 | &print_usage();
|
---|
157 | die "\n";
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | sub main {
|
---|
162 |
|
---|
163 | &parse_args (\@ARGV);
|
---|
164 | if ($optionfile =~ /\w/) {
|
---|
165 | open (OPTIONS, $optionfile) || die "Couldn't open $optionfile\n";
|
---|
166 | my $line = [];
|
---|
167 | my $options = [];
|
---|
168 | while (defined ($line = &cfgread::read_cfg_line ('mkcol::OPTIONS'))) {
|
---|
169 | push (@$options, @$line);
|
---|
170 | }
|
---|
171 | close OPTIONS;
|
---|
172 | &parse_args ($options);
|
---|
173 |
|
---|
174 | }
|
---|
175 |
|
---|
176 | # load default plugins if none were on command line
|
---|
177 | if (!scalar(@plugin)) {
|
---|
178 | @plugin = (ZIPPlug,GAPlug,TEXTPlug,HTMLPlug,EMAILPlug,
|
---|
179 | PDFPlug,RTFPlug,WordPlug,PSPlug,ArcPlug,RecPlug);
|
---|
180 | }
|
---|
181 |
|
---|
182 | # get and check the collection name
|
---|
183 | ($collection) = @ARGV;
|
---|
184 | if (!defined($collection)) {
|
---|
185 | print STDOUT "ERROR: no collection name was specified\n";
|
---|
186 | &print_usage();
|
---|
187 | die "\n";
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (length($collection) > 8) {
|
---|
191 | print STDOUT "ERROR: The collection name must be less than 8 characters\n";
|
---|
192 | print STDOUT " so compatibility with earlier filesystems can be\n";
|
---|
193 | print STDOUT " maintained.\n";
|
---|
194 | die "\n";
|
---|
195 | }
|
---|
196 |
|
---|
197 | if ($collection eq "gs2model" || $collection eq "gs3model") {
|
---|
198 | print STDOUT "ERROR: No collection can be named gs2model or gs3model as these are the\n";
|
---|
199 | print STDOUT " names of the model collections.\n";
|
---|
200 | die "\n";
|
---|
201 | }
|
---|
202 |
|
---|
203 | if ($collection eq "CVS") {
|
---|
204 | print STDOUT "ERROR: No collection can be named CVS as this may interfere\n";
|
---|
205 | print STDOUT " with directories created by the CVS versioning system\n";
|
---|
206 | die "\n";
|
---|
207 | }
|
---|
208 |
|
---|
209 | #check that -site has been specified
|
---|
210 | if (!defined($sitehome) || $sitehome eq "") {
|
---|
211 | print STDOUT "ERROR: The site was not defined. This variable is\n";
|
---|
212 | print STDOUT " needed to locate the collect directory.\n";
|
---|
213 | die "\n";
|
---|
214 | }
|
---|
215 | #check that its a valid directory
|
---|
216 | if (!-d $sitehome) {
|
---|
217 | print STDOUT "ERROR: $sitehome doesn't exist\n";
|
---|
218 | die "\n";
|
---|
219 | }
|
---|
220 | if (!defined($creator) || $creator eq "") {
|
---|
221 | print STDOUT "ERROR: The creator was not defined. This variable is\n";
|
---|
222 | print STDOUT " needed to recognise duplicate collection names.\n";
|
---|
223 | die "\n";
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (!defined($maintainer) || $maintainer eq "") {
|
---|
227 | $maintainer = $creator;
|
---|
228 | }
|
---|
229 |
|
---|
230 | $public = "true" unless defined $public;
|
---|
231 | $buildtype = "mgpp" unless defined $buildtype;
|
---|
232 |
|
---|
233 | $searchtype = "";
|
---|
234 | if ($buildtype eq "mgpp") {
|
---|
235 | $searchtype = "searchtype plain form";
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (!defined($title) || $title eq "") {
|
---|
239 | $title = $collection;
|
---|
240 | }
|
---|
241 |
|
---|
242 | # get capitalised version of the collection
|
---|
243 | $capcollection = $collection;
|
---|
244 | $capcollection =~ tr/a-z/A-Z/;
|
---|
245 |
|
---|
246 | # get the strings to include.
|
---|
247 | $pluginstring = "";
|
---|
248 | foreach $plugin (@plugin) {
|
---|
249 | if ($plugin eq RecPlug) {
|
---|
250 | $pluginstring .= "plugin $plugin -use_metadata_files\n";
|
---|
251 | } else {
|
---|
252 | $pluginstring .= "plugin $plugin\n";
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | $mdir = &util::filename_cat ($sitehome, "collect", "gs2model");
|
---|
257 |
|
---|
258 | if (defined $collectdir && $collectdir =~ /\w/) {
|
---|
259 | if (!-d $collectdir) {
|
---|
260 | print STDOUT "ERROR: $collectdir doesn't exist\n";
|
---|
261 | die "\n";
|
---|
262 | }
|
---|
263 | $cdir = &util::filename_cat ($collectdir, $collection);
|
---|
264 | } else {
|
---|
265 | $cdir = &util::filename_cat ($sitehome, "collect", $collection);
|
---|
266 | }
|
---|
267 |
|
---|
268 | # make sure the model collection exists
|
---|
269 | die "ERROR: Cannot find the model collection $mdir" unless (-d $mdir);
|
---|
270 |
|
---|
271 | # make sure this collection does not already exist
|
---|
272 | if (-e $cdir) {
|
---|
273 | print STDOUT "ERROR: This collection already exists\n";
|
---|
274 | die "\n";
|
---|
275 | }
|
---|
276 |
|
---|
277 | # set up the default indexes - this could be a command line option at some stage
|
---|
278 | # the names are added in here for the xml ones, but they should be added after building once the names are known.
|
---|
279 | if ($buildtype eq "mg") {
|
---|
280 | $indexes = "document:text document:Title document:Source";
|
---|
281 | $defaultindex = "defaultindex document:text";
|
---|
282 | $indexmeta = "collectionmeta .document:text \"text\"\ncollectionmeta .document:Title \"titles\"\ncollectionmeta .document:Source \"filenames\"\n";
|
---|
283 | } elsif ($buildtype eq "mgpp") {
|
---|
284 | $indexes = "allfields text metadata";
|
---|
285 | $defaultindex = "";
|
---|
286 | $indexmeta = "collectionmeta .text \"text\"\ncollectionmeta .allfields \"entire documents\"\n";
|
---|
287 | } else {
|
---|
288 | print STDOUT "Error: buildtype should be mg or mgpp, but its $buildtype\n";
|
---|
289 | }
|
---|
290 | # start creating the collection
|
---|
291 | print STDOUT "\nCreating the collection $collection...\n" unless $quiet;
|
---|
292 | &traverse_dir ($mdir, $cdir);
|
---|
293 | print STDOUT "\nThe new collection was created successfully at\n" unless $quiet;
|
---|
294 | print STDOUT "$cdir\n" unless $quiet;
|
---|
295 | }
|
---|
296 |
|
---|
297 | &main ();
|
---|