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 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
38 | }
|
---|
39 |
|
---|
40 | use parsargv;
|
---|
41 | use util;
|
---|
42 | use cfgread;
|
---|
43 |
|
---|
44 | sub print_usage {
|
---|
45 | print STDERR "\n";
|
---|
46 | print STDERR "mkcol.pl: Creates the directory structure for a new\n";
|
---|
47 | print STDERR " Greenstone collection.\n\n";
|
---|
48 | print STDERR " usage: $0 [options] collection-name\n\n";
|
---|
49 | print STDERR " options:\n";
|
---|
50 | print STDERR " -optionfile file Get options from file, useful on systems where\n";
|
---|
51 | print STDERR " long command lines may cause problems\n";
|
---|
52 | print STDERR " -collectdir Directory where new collection will be created.\n";
|
---|
53 | print STDERR " Default is " .
|
---|
54 | &util::filename_cat($ENV{'GSDLHOME'}, "collect") . "\n";
|
---|
55 | print STDERR " -creator email Your email address\n";
|
---|
56 | print STDERR " -maintainer email The collection maintainer's email address (if\n";
|
---|
57 | print STDERR " different from the creator)\n";
|
---|
58 | print STDERR " -public true|false If this collection has anonymous access\n";
|
---|
59 | print STDERR " -beta true|false If this collection is still under development\n";
|
---|
60 | print STDERR " -title text The title for the collection\n";
|
---|
61 | print STDERR " -about text The about text for the collection\n";
|
---|
62 | print STDERR " -plugin text perl plugin module to use (there may be multiple\n";
|
---|
63 | print STDERR " plugin entries)\n";
|
---|
64 | print STDERR " Note that -creator is the only option to mkcol.pl that is mandatory.\n";
|
---|
65 | print STDERR " You can make changes to all options later by editing the collect.cfg\n";
|
---|
66 | print STDERR " configuration file for your new collection (it'll be in the \"etc\"\n";
|
---|
67 | print STDERR " directory).\n\n";
|
---|
68 | }
|
---|
69 |
|
---|
70 | sub traverse_dir
|
---|
71 | {
|
---|
72 | my ($modeldir, $coldir) = @_;
|
---|
73 | my ($newfile, @filetext);
|
---|
74 |
|
---|
75 | if (!(-e $coldir)) {
|
---|
76 |
|
---|
77 | my $store_umask = umask(0002);
|
---|
78 | my $mkdir_ok = mkdir ($coldir, 0777);
|
---|
79 | umask($store_umask);
|
---|
80 |
|
---|
81 | if (!$mkdir_ok)
|
---|
82 | {
|
---|
83 | die "$!";
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | opendir(DIR, $modeldir) || die "Can't read $modeldir";
|
---|
88 | my @files = grep(!/^(\.\.?|CVS)$/, readdir(DIR));
|
---|
89 | closedir(DIR);
|
---|
90 |
|
---|
91 | foreach $file (@files)
|
---|
92 | {
|
---|
93 | my $thisfile = &util::filename_cat ($modeldir, $file);
|
---|
94 | if (-d $thisfile) {
|
---|
95 | my $colfiledir = &util::filename_cat ($coldir, $file);
|
---|
96 | traverse_dir ($thisfile, $colfiledir);
|
---|
97 |
|
---|
98 | } else {
|
---|
99 | my $destfile = $file;
|
---|
100 | $destfile =~ s/^modelcol/$collection/;
|
---|
101 | $destfile =~ s/^MODELCOL/$capcollection/;
|
---|
102 | $destfile = &util::filename_cat ($coldir, $destfile);
|
---|
103 |
|
---|
104 | print STDERR "doing replacements for $thisfile\n";
|
---|
105 | open (INFILE, $thisfile) ||
|
---|
106 | die "Can't read file $thisfile";
|
---|
107 | open (OUTFILE, ">$destfile") ||
|
---|
108 | die "Can't create file $destfile";
|
---|
109 |
|
---|
110 | while (defined ($line = <INFILE>)) {
|
---|
111 | $line =~ s/\*\*collection\*\*/$collection/g;
|
---|
112 | $line =~ s/\*\*COLLECTION\*\*/$capcollection/g;
|
---|
113 | $line =~ s/\*\*creator\*\*/$creator/g;
|
---|
114 | $line =~ s/\*\*maintainer\*\*/$maintainer/g;
|
---|
115 | $line =~ s/\*\*public\*\*/$public/g;
|
---|
116 | $line =~ s/\*\*beta\*\*/$beta/g;
|
---|
117 | $line =~ s/\*\*title\*\*/$title/g;
|
---|
118 | $line =~ s/\*\*about\*\*/$about/g;
|
---|
119 | $line =~ s/\*\*plugins\*\*/$pluginstring/g;
|
---|
120 |
|
---|
121 | print OUTFILE $line;
|
---|
122 | }
|
---|
123 |
|
---|
124 | close (OUTFILE);
|
---|
125 | close (INFILE);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | # get and check options
|
---|
131 | sub parse_args {
|
---|
132 | my ($argref) = @_;
|
---|
133 | if (!&parsargv::parse($argref,
|
---|
134 | 'optionfile/.*/', \$optionfile,
|
---|
135 | 'collectdir/.*/', \$collectdir,
|
---|
136 | 'creator/\w+\@[\w\.]+/', \$creator,
|
---|
137 | 'maintainer/\w+\@[\w\.]+/', \$maintainer,
|
---|
138 | 'public/true|false/true', \$public,
|
---|
139 | 'beta/true|false/true', \$beta,
|
---|
140 | 'title/.+/', \$title,
|
---|
141 | 'about/.+/', \$about,
|
---|
142 | 'plugin/.+', \@plugin
|
---|
143 | )) {
|
---|
144 | &print_usage();
|
---|
145 | die "\n";
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | sub main {
|
---|
150 |
|
---|
151 | &parse_args (\@ARGV);
|
---|
152 | if ($optionfile =~ /\w/) {
|
---|
153 | open (OPTIONS, $optionfile) || die "Couldn't open $optionfile\n";
|
---|
154 | my $line = [];
|
---|
155 | my $options = [];
|
---|
156 | while (defined ($line = &cfgread::read_cfg_line ('mkcol::OPTIONS'))) {
|
---|
157 | push (@$options, @$line);
|
---|
158 | }
|
---|
159 | close OPTIONS;
|
---|
160 | &parse_args ($options);
|
---|
161 |
|
---|
162 | }
|
---|
163 |
|
---|
164 | # load default plugins if none were on command line
|
---|
165 | if (!scalar(@plugin)) {
|
---|
166 | @plugin = (ZIPPlug,GMLPlug,TEXTPlug,HTMLPlug,EMAILPlug,ArcPlug,RecPlug);
|
---|
167 | }
|
---|
168 |
|
---|
169 | # get and check the collection name
|
---|
170 | ($collection) = @ARGV;
|
---|
171 | if (!defined($collection)) {
|
---|
172 | print STDERR "no collection name was specified\n";
|
---|
173 | &print_usage();
|
---|
174 | die "\n";
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (length($collection) > 8) {
|
---|
178 | print STDERR "The collection name must be less than 8 characters\n";
|
---|
179 | print STDERR "so compatibility with earlier filesystems can be\n";
|
---|
180 | print STDERR "maintained.\n";
|
---|
181 | die "\n";
|
---|
182 | }
|
---|
183 |
|
---|
184 | if ($collection eq "modelcol") {
|
---|
185 | print STDERR "No collection can be named modelcol as this is the\n";
|
---|
186 | print STDERR "name of the model collection.\n";
|
---|
187 | die "\n";
|
---|
188 | }
|
---|
189 |
|
---|
190 | if ($collection eq "CVS") {
|
---|
191 | print STDERR "No collection can be named CVS as this may interfere\n";
|
---|
192 | print STDERR "with directories created by the CVS versioning system\n";
|
---|
193 | die "\n";
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (!defined($creator) || $creator eq "") {
|
---|
197 | print STDERR "The creator was not defined. This variable is\n";
|
---|
198 | print STDERR "needed to recognise duplicate collection names.\n";
|
---|
199 | die "\n";
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (!defined($maintainer) || $maintainer eq "") {
|
---|
203 | $maintainer = $creator;
|
---|
204 | }
|
---|
205 |
|
---|
206 | $public = "true" unless defined $public;
|
---|
207 | $beta = "true" unless defined $beta;
|
---|
208 |
|
---|
209 | if (!defined($title) || $title eq "") {
|
---|
210 | $title = $collection;
|
---|
211 | }
|
---|
212 |
|
---|
213 | # get capitalised version of the collection
|
---|
214 | $capcollection = $collection;
|
---|
215 | $capcollection =~ tr/a-z/A-Z/;
|
---|
216 |
|
---|
217 | # get the strings to include.
|
---|
218 | $pluginstring = "";
|
---|
219 | foreach $plugin (@plugin) {
|
---|
220 | $pluginstring .= "plugin $plugin\n";
|
---|
221 | }
|
---|
222 |
|
---|
223 | $mdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", "modelcol");
|
---|
224 | if (defined $collectdir && $collectdir =~ /\w/) {
|
---|
225 | if (!-d $collectdir) {
|
---|
226 | print STDERR "ERROR: $collectdir doesn't exist\n";
|
---|
227 | die "\n";
|
---|
228 | }
|
---|
229 | $cdir = &util::filename_cat ($collectdir, $collection);
|
---|
230 | } else {
|
---|
231 | $cdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
232 | }
|
---|
233 |
|
---|
234 | # make sure the model collection exists
|
---|
235 | die "Cannot find the model collection $mdir" unless (-d $mdir);
|
---|
236 |
|
---|
237 | # make sure this collection does not already exist
|
---|
238 | if (-e $cdir) {
|
---|
239 | print STDERR "This collection already exists\n";
|
---|
240 | die "\n";
|
---|
241 | }
|
---|
242 |
|
---|
243 | # start creating the collection
|
---|
244 | print STDERR "Creating the collection $collection\n";
|
---|
245 | &traverse_dir ($mdir, $cdir);
|
---|
246 | print STDERR "The new collection is in $cdir.\n";
|
---|
247 | }
|
---|
248 |
|
---|
249 | &main ();
|
---|