root/trunk/gsdl3/bin/script/gs2_mkcol.pl @ 3771

Revision 3771, 9.7 KB (checked in by kjdon, 17 years ago)

added support for mg collections

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