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 parsargv;
|
---|
42 | use util;
|
---|
43 | use cfgread;
|
---|
44 | use gsprintf;
|
---|
45 | use printusage;
|
---|
46 |
|
---|
47 | my $arguments =
|
---|
48 | [ { 'name' => "creator",
|
---|
49 | 'desc' => "{mkcol.creator}",
|
---|
50 | 'type' => "string",
|
---|
51 | 'reqd' => "no" },
|
---|
52 | { 'name' => "optionfile",
|
---|
53 | 'desc' => "{mkcol.optionfile}",
|
---|
54 | 'type' => "string",
|
---|
55 | 'reqd' => "no" },
|
---|
56 | { 'name' => "maintainer",
|
---|
57 | 'desc' => "{mkcol.maintainer}",
|
---|
58 | 'type' => "string",
|
---|
59 | 'reqd' => "no" },
|
---|
60 | { 'name' => "collectdir",
|
---|
61 | 'desc' => "{mkcol.collectdir}",
|
---|
62 | 'type' => "string",
|
---|
63 | 'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
|
---|
64 | 'reqd' => "no" },
|
---|
65 | { 'name' => "public",
|
---|
66 | 'desc' => "{mkcol.public}",
|
---|
67 | 'type' => "string",
|
---|
68 | 'deft' => "true",
|
---|
69 | 'reqd' => "no" },
|
---|
70 | { 'name' => "title",
|
---|
71 | 'desc' => "{mkcol.title}",
|
---|
72 | 'type' => "string",
|
---|
73 | 'reqd' => "no" },
|
---|
74 | { 'name' => "about",
|
---|
75 | 'desc' => "{mkcol.about}",
|
---|
76 | 'type' => "string",
|
---|
77 | 'reqd' => "no" },
|
---|
78 | { 'name' => "plugin",
|
---|
79 | 'desc' => "{mkcol.plugin}",
|
---|
80 | 'type' => "string",
|
---|
81 | 'reqd' => "no" },
|
---|
82 | { 'name' => "quiet",
|
---|
83 | 'desc' => "{mkcol.quiet}",
|
---|
84 | 'type' => "flag",
|
---|
85 | 'reqd' => "no" },
|
---|
86 | { 'name' => "language",
|
---|
87 | 'desc' => "{scripts.language}",
|
---|
88 | 'type' => "string",
|
---|
89 | 'reqd' => "no" } ];
|
---|
90 |
|
---|
91 | my $options = { 'name' => "mkcol.pl",
|
---|
92 | 'desc' => "{mkcol.desc}",
|
---|
93 | 'args' => $arguments };
|
---|
94 |
|
---|
95 | sub gsprintf
|
---|
96 | {
|
---|
97 | return &gsprintf::gsprintf(@_);
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | sub traverse_dir
|
---|
102 | {
|
---|
103 | my ($modeldir, $coldir) = @_;
|
---|
104 | my ($newfile, @filetext);
|
---|
105 |
|
---|
106 | if (!(-e $coldir)) {
|
---|
107 |
|
---|
108 | my $store_umask = umask(0002);
|
---|
109 | my $mkdir_ok = mkdir ($coldir, 0777);
|
---|
110 | umask($store_umask);
|
---|
111 |
|
---|
112 | if (!$mkdir_ok)
|
---|
113 | {
|
---|
114 | die "$!";
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | opendir(DIR, $modeldir) ||
|
---|
119 | (&gsprintf(STDERR, "{common.cannot_read}\n", $modeldir) && die);
|
---|
120 | my @files = grep(!/^(\.\.?|CVS)$/, readdir(DIR));
|
---|
121 | closedir(DIR);
|
---|
122 |
|
---|
123 | foreach $file (@files)
|
---|
124 | {
|
---|
125 | my $thisfile = &util::filename_cat ($modeldir, $file);
|
---|
126 | if (-d $thisfile) {
|
---|
127 | my $colfiledir = &util::filename_cat ($coldir, $file);
|
---|
128 | traverse_dir ($thisfile, $colfiledir);
|
---|
129 |
|
---|
130 | } else {
|
---|
131 | my $destfile = $file;
|
---|
132 | $destfile =~ s/^modelcol/$collection/;
|
---|
133 | $destfile =~ s/^MODELCOL/$capcollection/;
|
---|
134 | &gsprintf(STDOUT, "{mkcol.doing_replacements}\n", $destfile)
|
---|
135 | unless $quiet;
|
---|
136 | $destfile = &util::filename_cat ($coldir, $destfile);
|
---|
137 |
|
---|
138 | open (INFILE, $thisfile) ||
|
---|
139 | (&gsprintf(STDERR, "{common.cannot_read_file}\n", $thisfile) && die);
|
---|
140 | open (OUTFILE, ">$destfile") ||
|
---|
141 | (&gsprintf(STDERR, "{common.cannot_create_file}\n", $destfile) && die);
|
---|
142 |
|
---|
143 | while (defined ($line = <INFILE>)) {
|
---|
144 | $line =~ s/\*\*collection\*\*/$collection/g;
|
---|
145 | $line =~ s/\*\*COLLECTION\*\*/$capcollection/g;
|
---|
146 | $line =~ s/\*\*creator\*\*/$creator/g;
|
---|
147 | $line =~ s/\*\*maintainer\*\*/$maintainer/g;
|
---|
148 | $line =~ s/\*\*public\*\*/$public/g;
|
---|
149 | $line =~ s/\*\*title\*\*/$title/g;
|
---|
150 | $line =~ s/\*\*about\*\*/$about/g;
|
---|
151 | $line =~ s/\*\*plugins\*\*/$pluginstring/g;
|
---|
152 |
|
---|
153 | print OUTFILE $line;
|
---|
154 | }
|
---|
155 |
|
---|
156 | close (OUTFILE);
|
---|
157 | close (INFILE);
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | # get and check options
|
---|
163 | sub parse_args {
|
---|
164 | my ($argref) = @_;
|
---|
165 | if (!&parsargv::parse($argref,
|
---|
166 | 'language/.*/', \$language,
|
---|
167 | 'optionfile/.*/', \$optionfile,
|
---|
168 | 'collectdir/.*/', \$collectdir,
|
---|
169 | 'creator/\w+\@[\w\.]+/', \$creator,
|
---|
170 | 'maintainer/\w+\@[\w\.]+/', \$maintainer,
|
---|
171 | 'public/true|false/true', \$public,
|
---|
172 | 'title/.+/', \$title,
|
---|
173 | 'about/.+/', \$about,
|
---|
174 | 'plugin/.+', \@plugin,
|
---|
175 | 'quiet', \$quiet,
|
---|
176 | 'gli', \$gli,
|
---|
177 | q^xml^, \$xml
|
---|
178 | )) {
|
---|
179 | &PrintUsage::print_txt_usage($options, "{mkcol.params}");
|
---|
180 | die "\n";
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | sub main {
|
---|
185 |
|
---|
186 | &parse_args (\@ARGV);
|
---|
187 |
|
---|
188 | # If $language has been specified, load the appropriate resource bundle
|
---|
189 | # (Otherwise, the default resource bundle will be loaded automatically)
|
---|
190 | if ($language) {
|
---|
191 | &gsprintf::load_language_specific_resource_bundle($language);
|
---|
192 | }
|
---|
193 |
|
---|
194 | if ($xml) {
|
---|
195 | &PrintUsage::print_xml_usage($options);
|
---|
196 | die "\n";
|
---|
197 | }
|
---|
198 |
|
---|
199 | if ($gli) { # the gli wants strings to be in UTF-8
|
---|
200 | &gsprintf::output_strings_in_UTF8;
|
---|
201 | }
|
---|
202 |
|
---|
203 | if ($optionfile =~ /\w/) {
|
---|
204 | open (OPTIONS, $optionfile) ||
|
---|
205 | (&gsprintf(STDERR, "{common.cannot_open}\n", $optionfile) && die);
|
---|
206 | my $line = [];
|
---|
207 | my $options = [];
|
---|
208 | while (defined ($line = &cfgread::read_cfg_line ('mkcol::OPTIONS'))) {
|
---|
209 | push (@$options, @$line);
|
---|
210 | }
|
---|
211 | close OPTIONS;
|
---|
212 | &parse_args ($options);
|
---|
213 | }
|
---|
214 |
|
---|
215 | # load default plugins if none were on command line
|
---|
216 | if (!scalar(@plugin)) {
|
---|
217 | @plugin = (ZIPPlug,GAPlug,TEXTPlug,HTMLPlug,EMAILPlug,
|
---|
218 | PDFPlug,RTFPlug,WordPlug,PSPlug,ArcPlug,RecPlug);
|
---|
219 | }
|
---|
220 |
|
---|
221 | # get and check the collection name
|
---|
222 | ($collection) = @ARGV;
|
---|
223 | if (!defined($collection)) {
|
---|
224 | &gsprintf(STDOUT, "{mkcol.no_colname}\n");
|
---|
225 | &PrintUsage::print_txt_usage($options, "{mkcol.params}");
|
---|
226 | die "\n";
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (length($collection) > 8) {
|
---|
230 | &gsprintf(STDOUT, "{mkcol.long_colname}\n");
|
---|
231 | die "\n";
|
---|
232 | }
|
---|
233 |
|
---|
234 | if ($collection eq "modelcol") {
|
---|
235 | &gsprintf(STDOUT, "{mkcol.bad_name_modelcol}\n");
|
---|
236 | die "\n";
|
---|
237 | }
|
---|
238 |
|
---|
239 | if ($collection eq "CVS") {
|
---|
240 | &gsprintf(STDOUT, "{mkcol.bad_name_cvs}\n");
|
---|
241 | die "\n";
|
---|
242 | }
|
---|
243 |
|
---|
244 | # We don't want creator to be required anymore - John Thompson 28-11-2003
|
---|
245 | #if (!defined($creator) || $creator eq "") {
|
---|
246 | # &gsprintf(STDOUT, "{mkcol.creator_undefined}\n");
|
---|
247 | # die "\n";
|
---|
248 | #}
|
---|
249 |
|
---|
250 | #Of course thats means we don't want to default maintainer unless a creator is provided
|
---|
251 | if (defined($creator) && (!defined($maintainer) || $maintainer eq "")) {
|
---|
252 | $maintainer = $creator;
|
---|
253 | }
|
---|
254 |
|
---|
255 | $public = "true" unless defined $public;
|
---|
256 |
|
---|
257 | if (!defined($title) || $title eq "") {
|
---|
258 | $title = $collection;
|
---|
259 | }
|
---|
260 |
|
---|
261 | # get capitalised version of the collection
|
---|
262 | $capcollection = $collection;
|
---|
263 | $capcollection =~ tr/a-z/A-Z/;
|
---|
264 |
|
---|
265 | # get the strings to include.
|
---|
266 | $pluginstring = "";
|
---|
267 | foreach $plugin (@plugin) {
|
---|
268 | $pluginstring .= "plugin $plugin\n";
|
---|
269 | }
|
---|
270 |
|
---|
271 | $mdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", "modelcol");
|
---|
272 | if (defined $collectdir && $collectdir =~ /\w/) {
|
---|
273 | if (!-d $collectdir) {
|
---|
274 | &gsprintf(STDOUT, "{mkcol.no_collectdir}\n", $collectdir);
|
---|
275 | die "\n";
|
---|
276 | }
|
---|
277 | $cdir = &util::filename_cat ($collectdir, $collection);
|
---|
278 | } else {
|
---|
279 | $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
280 | }
|
---|
281 |
|
---|
282 | # make sure the model collection exists
|
---|
283 | (&gsprintf(STDERR, "{mkcol.cannot_find_modelcol}\n", $mdir) && die) unless (-d $mdir);
|
---|
284 |
|
---|
285 | # make sure this collection does not already exist
|
---|
286 | if (-e $cdir) {
|
---|
287 | &gsprintf(STDOUT, "{mkcol.col_already_exists}\n");
|
---|
288 | die "\n";
|
---|
289 | }
|
---|
290 |
|
---|
291 | # start creating the collection
|
---|
292 | &gsprintf(STDOUT, "\n{mkcol.creating_col}...\n", $collection)
|
---|
293 | unless $quiet;
|
---|
294 | &traverse_dir ($mdir, $cdir);
|
---|
295 | &gsprintf(STDOUT, "\n{mkcol.success}\n", $cdir)
|
---|
296 | unless $quiet;
|
---|
297 | }
|
---|
298 |
|
---|
299 | &main ();
|
---|