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

###########################################################################
#
# gettext.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 output the text for a particular index

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

use parsargv;
use plugin;
use colcfg;
use util;

sub print_usage {
    print STDERR "\n  usage: $0 collection-name\n";
    print STDERR "  options:\n";
    print STDERR "   -archivedir directory Where the archives live\n";
    print STDERR "   -mode infodb for gdbm database, defaults to text\n";
    print STDERR "   -index index   The index to output\n\n";
}


&main ();

sub main {

    if (!parsargv::parse(\@ARGV, 
			 'archivedir/.*/', \$archivedir,
			 'index/.*/section:text', \$index,
			 'mode/.*/text', \$mode)) {
	&print_usage();
	die "\n";
    }

    # get and check the collection name
   if (($collection = &util::use_collection(@ARGV)) eq "") {
	&print_usage();
	die "\n";
    }
    
    # get the list of plugins for this collection
    @plugins = ();
    $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc/collect.cfg");
    if (-e $configfilename) {
	$collectcfg = &colcfg::read_collect_cfg ($configfilename);
	if (defined $collectcfg->{'plugins'}) {
	    @plugins = @{$collectcfg->{'plugins'}};
	}
	if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
	    $archivedir = $collectcfg->{'archivedir'};
	}
    } else {
	die "Couldn't find the configuration file $configfilename\n";
    }

    # fill in the default archives directories if none
    # was supplied, turn all \ into / and remove trailing /
    $archivedir = "$ENV{'GSDLCOLLECTDIR'}/archives" if $archivedir eq "";
    $archivedir =~ s/[\\\/]+/\//g;
    $archivedir =~ s/\/$//;

    # load all the plugins
    $pluginfo = &plugin::load_plugins ($collection, \@plugins);
    if (scalar(@$pluginfo) == 0) {
	print STDERR "No plugins were loaded.\n";
	die "\n";
    }

    # load up the document processor for building
    # if a buildproc class has been created for this collection, use it
    # otherwise, use the mg buildproc
    # if a builder class has been created for this collection, use it
    # otherwise, use the mg builder
    # load up the document processor for building
    # if a buildproc class has been created for this collection, use it
    # otherwise, use the mg buildproc
    my ($buildprocdir, $buildproctype);
    if (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}buildproc.pm") {
	$buildprocdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
	$buildproctype = "${collection}buildproc";
    } else {
	$buildprocdir = "$ENV{'GSDLHOME'}/perllib";
	$buildproctype = "mgbuildproc";
    }
    require "$buildprocdir/$buildproctype.pm";

    eval("\$buildproc = new $buildproctype(\$collection, " .
	 "\$archivedir, \"\$ENV{'GSDLCOLLECTDIR'}/building\", 1)");
    die "$@" if $@;

    # set up the document processor
    $buildproc->set_output_handle ('STDOUT');
    $buildproc->set_mode ($mode);
    $buildproc->set_index ($index);

    $buildproc->reset();

    # process the import directory
    &plugin::read ($pluginfo, $archivedir, 
		   "", {}, $buildproc);

}
