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