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