#!/usr/local/bin/perl5 -w

###########################################################################
#
# mkcol.pl --
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################


# This program will setup a new collection from a model one. It does this by
# copying the model, moving files to have the correct names, and replacing
# text within the files to match the parameters.

BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}

use parsargv;

sub print_usage {
    print STDERR "\n  usage: $0 [options] collection-name\n\n";
    print STDERR "  options:\n";
    print STDERR "   -creator email      Your email address\n";
    print STDERR "   -maintainer email   The current maintainer's email address\n";
    print STDERR "   -public true|false  If this collection has anonymous access\n";
    print STDERR "   -beta true|false    If this collection is still under development\n";
#    print STDERR "   -index type         The indexes which should be made\n";
#    print STDERR "   -indextext name     The index description\n";
#    print STDERR "   -defaultindex type  The index to use if no others are supplied\n";

    print STDERR "   -title text         The title for the collection\n";
    print STDERR "   -about text         The about text for the collection\n";
    print STDERR "   -plugins list       Space separated list of perl plugin modules to use\n";
    print STDERR "   -refine   list      Space separated list of perl plugin modules to use\n";


}

sub traverse_dir
{
    my ($modeldir, $coldir) = @_;
    my ($newfile, @filetext);

    if (!(-e $coldir)) {

	my $store_umask = umask(0002);
	my $mkdir_ok = mkdir ($coldir, 0777);
	umask($store_umask);

	if (!$mkdir_ok) 
	{
	    die "$!";
	}
    }

    opendir(DIR, $modeldir) || die "Can't read $modeldir";
    my @files = grep(!/^(\.\.?|CVS)$/, readdir(DIR));
    closedir(DIR);

    foreach $file (@files)
    {
	if (-d "$modeldir/$file") {
	    traverse_dir ("$modeldir/$file", "$coldir/$file");

	} else {
	    my $destfile = $file;
	    $destfile =~ s/^\modelcol/$collection/;
	    $destfile =~ s/^\MODELCOL/$capcollection/;

	    print STDERR "doing replacements for $modeldir/$file\n";
	    open (INFILE, "$modeldir/$file") || 
		die "Can't read file $modeldir/$file";
	    open (OUTFILE, ">$coldir/$destfile") ||
		die "Can't create file $coldir/destfile";

	    while (defined ($line = <INFILE>)) {
		$line =~ s/\*\*collection\*\*/$collection/g;
		$line =~ s/\*\*COLLECTION\*\*/$capcollection/g;
		$line =~ s/\*\*creator\*\*/$creator/g;
		$line =~ s/\*\*maintainer\*\*/$maintainer/g;
		$line =~ s/\*\*public\*\*/$public/g;
		$line =~ s/\*\*beta\*\*/$beta/g;
		$line =~ s/\*\*indexes\*\*/$indexesstr/g;
		$line =~ s/\*\*indexestext\*\*/$indexestextstr/g;
		$line =~ s/\*\*defaultindex\*\*/$defaultindex/g;
		$line =~ s/\*\*title\*\*/$title/g;
		$line =~ s/\*\*about\*\*/$about/g;
		$line =~ s/\*\*plugins\*\*/$plugins/g;
		$line =~ s/\*\*refine\*\*/$refine/g;

		print OUTFILE $line;
	    }
	    
	    close (OUTFILE);
	    close (INFILE);
	}
    }
}


my (@indexes, @indexestext);

# get and check options
if (!&parsargv::parse(\@ARGV,
		       'creator/\w+\@[\w\.]+/', \$creator,
		       'maintainer/\w+\@[\w\.]+/', \$maintainer,
		       'public/true|false/true', \$public,
		       'beta/true|false/true', \$beta,
		       'index/.*/document:all', \@indexes,
		       'indextext/\.*/Terms must appear within the same document', \@indexestext,
		       'defaultindex/.*/document:all', \$defaultindex,
		       'title/.+/', \$title,
		       'about/.+/', \$about,
		       'plugins/.+/GMLPlug TEXTPlug ArcPlug RecPlug/', \$plugins,
		       'refine/.+/', \$refine
		       )) {
    &print_usage();
    die "\n";
}
    
# get and check the collection name
($collection) = @ARGV;
if (!defined($collection)) {
    &print_usage();
    die "\n";
}

if (length($collection) > 8) {
    print STDERR "The collection name must be less than 8 characters\n";
    print STDERR "so compatibility with earlier filesystems can be\n";
    print STDERR "maintained.\n";
    die "\n";
}

if ($collection eq "modelcol") {
    print STDERR "No collection can be named modelcol as this is the\n";
    print STDERR "name of the model collection.\n";
    die "\n";
}

if ($collection eq "CVS") {
    print STDERR "No collection can be named CVS as this may interfere\n";
    print STDERR "with directories created by the CVS versioning system\n";
    die "\n";
}

if (!defined($creator) || $creator eq "") {
    print STDERR "The creator was not defined. This variable is\n";
    print STDERR "needed to recognise duplicate collection names.\n";
    die "\n";
}

if (!defined($maintainer) || $maintainer eq "") {
    $maintainer = $creator;
}

$public = "true" unless defined $public;
$beta = "true" unless defined $beta;


if (!defined($title) || $title eq "") {
    $title = $collection;
}

# get capitalised version of the collection
$capcollection = $collection;
$capcollection =~ tr/a-z/A-Z/;


# get the strings to include.
$indexesstr = join ("\t", @indexes);
$indexestextstr = "";
for ($i=0; $i<scalar(@indexestext); $i++) {
    $indexestextstr .= "_$indexes[$i]_[] {$indexestext[$i]}\n";
}


# make sure the model collection exists
die "Cannot find the model collection $ENV{'GSDLHOME'}/collect/modelcol" unless
    (-d "$ENV{'GSDLHOME'}/collect/modelcol");

# make sure this collection does not already exist
if (-e "$ENV{'GSDLHOME'}/collect/$collection") {
    print STDERR "This collection already exists\n";
    die "\n";
}


# start creating the collection
print STDERR "Creating the collection $collection\n\n";
&traverse_dir ("$ENV{'GSDLHOME'}/collect/modelcol", "$ENV{'GSDLHOME'}/collect/$collection");
print STDERR "\n\nThe new collection is in $ENV{'GSDLHOME'}/collect/$collection.\n\n";


