[3688] | 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";
|
---|
[3771] | 60 | print STDOUT " -buildtype mg|mgpp Whether to use mg or mgpp to build the collection\n";
|
---|
| 61 | print STDOUT " Default is mgpp.\n";
|
---|
[3688] | 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;
|
---|
[3771] | 123 | $line =~ s/\*\*buildtype\*\*/$buildtype/g;
|
---|
| 124 | $line =~ s/\*\*indexes\*\*/$indexes/g;
|
---|
| 125 | $line =~ s/\*\*defaultindex\*\*/$defaultindex/g;
|
---|
| 126 | $line =~ s/\*\*indexmeta\*\*/$indexmeta/g;
|
---|
[3872] | 127 | $line =~ s/\*\*xmlindexes\*\*/$xmlindexes/g;
|
---|
| 128 | $line =~ s/\*\*xmlplugins\*\*/$xmlpluginstring/g;
|
---|
| 129 |
|
---|
[3688] | 130 | print OUTFILE $line;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | close (OUTFILE);
|
---|
| 134 | close (INFILE);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | # get and check options
|
---|
| 140 | sub parse_args {
|
---|
| 141 | my ($argref) = @_;
|
---|
| 142 | if (!&parsargv::parse($argref,
|
---|
| 143 | 'optionfile/.*/', \$optionfile,
|
---|
| 144 | 'collectdir/.*/', \$collectdir,
|
---|
| 145 | 'site/.*/', \$sitehome,
|
---|
| 146 | 'creator/\w+\@[\w\.]+/', \$creator,
|
---|
| 147 | 'maintainer/\w+\@[\w\.]+/', \$maintainer,
|
---|
| 148 | 'public/true|false/true', \$public,
|
---|
[3771] | 149 | 'buildtype/mg|mgpp/mgpp', \$buildtype,
|
---|
[3688] | 150 | 'title/.+/', \$title,
|
---|
| 151 | 'about/.+/', \$about,
|
---|
| 152 | 'plugin/.+', \@plugin,
|
---|
| 153 | 'quiet', \$quiet,
|
---|
| 154 | )) {
|
---|
| 155 | &print_usage();
|
---|
| 156 | die "\n";
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | sub main {
|
---|
| 161 |
|
---|
| 162 | &parse_args (\@ARGV);
|
---|
| 163 | if ($optionfile =~ /\w/) {
|
---|
| 164 | open (OPTIONS, $optionfile) || die "Couldn't open $optionfile\n";
|
---|
| 165 | my $line = [];
|
---|
| 166 | my $options = [];
|
---|
| 167 | while (defined ($line = &cfgread::read_cfg_line ('mkcol::OPTIONS'))) {
|
---|
| 168 | push (@$options, @$line);
|
---|
| 169 | }
|
---|
| 170 | close OPTIONS;
|
---|
| 171 | &parse_args ($options);
|
---|
| 172 |
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | # load default plugins if none were on command line
|
---|
| 176 | if (!scalar(@plugin)) {
|
---|
| 177 | @plugin = (ZIPPlug,GAPlug,TEXTPlug,HTMLPlug,EMAILPlug,
|
---|
| 178 | PDFPlug,RTFPlug,WordPlug,PSPlug,ArcPlug,RecPlug);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | # get and check the collection name
|
---|
| 182 | ($collection) = @ARGV;
|
---|
| 183 | if (!defined($collection)) {
|
---|
| 184 | print STDOUT "ERROR: no collection name was specified\n";
|
---|
| 185 | &print_usage();
|
---|
| 186 | die "\n";
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | if (length($collection) > 8) {
|
---|
| 190 | print STDOUT "ERROR: The collection name must be less than 8 characters\n";
|
---|
| 191 | print STDOUT " so compatibility with earlier filesystems can be\n";
|
---|
| 192 | print STDOUT " maintained.\n";
|
---|
| 193 | die "\n";
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | if ($collection eq "gs2model" || $collection eq "gs3model") {
|
---|
| 197 | print STDOUT "ERROR: No collection can be named gs2model or gs3model as these are the\n";
|
---|
| 198 | print STDOUT " names of the model collections.\n";
|
---|
| 199 | die "\n";
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | if ($collection eq "CVS") {
|
---|
| 203 | print STDOUT "ERROR: No collection can be named CVS as this may interfere\n";
|
---|
| 204 | print STDOUT " with directories created by the CVS versioning system\n";
|
---|
| 205 | die "\n";
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | #check that -site has been specified
|
---|
| 209 | if (!defined($sitehome) || $sitehome eq "") {
|
---|
| 210 | print STDOUT "ERROR: The site was not defined. This variable is\n";
|
---|
| 211 | print STDOUT " needed to locate the collect directory.\n";
|
---|
| 212 | die "\n";
|
---|
| 213 | }
|
---|
| 214 | #check that its a valid directory
|
---|
| 215 | if (!-d $sitehome) {
|
---|
| 216 | print STDOUT "ERROR: $sitehome doesn't exist\n";
|
---|
| 217 | die "\n";
|
---|
| 218 | }
|
---|
| 219 | if (!defined($creator) || $creator eq "") {
|
---|
| 220 | print STDOUT "ERROR: The creator was not defined. This variable is\n";
|
---|
| 221 | print STDOUT " needed to recognise duplicate collection names.\n";
|
---|
| 222 | die "\n";
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | if (!defined($maintainer) || $maintainer eq "") {
|
---|
| 226 | $maintainer = $creator;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | $public = "true" unless defined $public;
|
---|
[3771] | 230 | $buildtype = "mgpp" unless defined $buildtype;
|
---|
[3688] | 231 |
|
---|
[3771] | 232 |
|
---|
[3688] | 233 | if (!defined($title) || $title eq "") {
|
---|
| 234 | $title = $collection;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | # get capitalised version of the collection
|
---|
| 238 | $capcollection = $collection;
|
---|
| 239 | $capcollection =~ tr/a-z/A-Z/;
|
---|
| 240 |
|
---|
| 241 | # get the strings to include.
|
---|
| 242 | $pluginstring = "";
|
---|
[3872] | 243 | $xmlpluginstring = "";
|
---|
[3688] | 244 | foreach $plugin (@plugin) {
|
---|
| 245 | if ($plugin eq RecPlug) {
|
---|
| 246 | $pluginstring .= "plugin $plugin -use_metadata_files\n";
|
---|
[3872] | 247 | $xmlpluginstring .= "<plugin name='$plugin'><option name='use_metadata_files'/></plugin>\n";
|
---|
[3688] | 248 | } else {
|
---|
| 249 | $pluginstring .= "plugin $plugin\n";
|
---|
[3872] | 250 | $xmlpluginstring .= "<plugin name='$plugin'/>\n";
|
---|
[3688] | 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | $mdir = &util::filename_cat ($sitehome, "collect", "gs2model");
|
---|
| 255 |
|
---|
| 256 | if (defined $collectdir && $collectdir =~ /\w/) {
|
---|
| 257 | if (!-d $collectdir) {
|
---|
| 258 | print STDOUT "ERROR: $collectdir doesn't exist\n";
|
---|
| 259 | die "\n";
|
---|
| 260 | }
|
---|
| 261 | $cdir = &util::filename_cat ($collectdir, $collection);
|
---|
| 262 | } else {
|
---|
[3771] | 263 | $cdir = &util::filename_cat ($sitehome, "collect", $collection);
|
---|
[3688] | 264 | }
|
---|
| 265 |
|
---|
| 266 | # make sure the model collection exists
|
---|
| 267 | die "ERROR: Cannot find the model collection $mdir" unless (-d $mdir);
|
---|
| 268 |
|
---|
| 269 | # make sure this collection does not already exist
|
---|
| 270 | if (-e $cdir) {
|
---|
| 271 | print STDOUT "ERROR: This collection already exists\n";
|
---|
| 272 | die "\n";
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[3771] | 275 | # set up the default indexes - this could be a command line option at some stage
|
---|
[3872] | 276 | # the names are added in here for the xml ones, but they should be added after building once the names are known.
|
---|
[3771] | 277 | if ($buildtype eq "mg") {
|
---|
| 278 | $indexes = "document:text document:Title document:Source";
|
---|
| 279 | $defaultindex = "document:text";
|
---|
| 280 | $indexmeta = "collectionmeta .document:text \"text\"\ncollectionmeta .document:Title \"titles\"\ncollectionmeta .document:Source \"filenames\"\n";
|
---|
| 281 |
|
---|
[3872] | 282 | $xmlindexes = "<index name='dtx' content='text' level='Document'><displayName lang='en'>text</displayName></index>\n<index name='dtt' content='Title' level='Document'><displayName lang='en'>titles</displayName></index>\n<index name='dsr' content='Source' level='Document'><displayName lang='en'>filenames</displayName></index>";
|
---|
[3771] | 283 | } elsif ($buildtype eq "mgpp") {
|
---|
| 284 | $indexes = "text,metadata";
|
---|
| 285 | $defaultindex = "text,metadata";
|
---|
| 286 | $indexmeta = "collectionmeta .text,metadata \"text\"\n";
|
---|
[3872] | 287 | $xmlindexes = "<index name='tm' content='text,metadata' level='Document'><displayName lang='en'>text</displayName></index>";
|
---|
[3771] | 288 | } else {
|
---|
| 289 | print STDOUT "Error: buildtype should be mg or mgpp, but its $buildtype\n";
|
---|
| 290 | }
|
---|
[3688] | 291 | # start creating the collection
|
---|
| 292 | print STDOUT "\nCreating the collection $collection...\n" unless $quiet;
|
---|
| 293 | &traverse_dir ($mdir, $cdir);
|
---|
| 294 | print STDOUT "\nThe new collection was created successfully at\n" unless $quiet;
|
---|
| 295 | print STDOUT "$cdir\n" unless $quiet;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | &main ();
|
---|