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

###########################################################################
#
# buildcol.pl -- This program will build a particular collection
# 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.
#
###########################################################################

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 colcfg;
use parsargv;
use util;

&main();

sub print_usage {
    print STDERR "\n  usage: $0 [options] collection-name\n\n";
    print STDERR "  options:\n";
    print STDERR "   -verbosity number     0=none, 3=lots\n";
    print STDERR "   -archivedir directory Where the archives live\n";
    print STDERR "   -cachedir directory   Where to cache the archives\n";
    print STDERR "   -builddir directory   Where to put the built indexes\n";
    print STDERR "   -maxdocs number       Maximum number of documents to build\n";
    print STDERR "   -allclassifications   Don't remove empty classifications\n\n";
}


sub main
{
    if (!parsargv::parse(\@ARGV, 
			 'verbosity/\d+/2', \$verbosity,
			 'archivedir/.*/', \$archivedir,
			 'cachedir/.*/', \$cachedir,
			 'builddir/.*/', \$builddir,
			 'maxdocs/\d+/', \$maxdocs,
			 'allclassifications', \$allclassifications)) {
	&print_usage();
	die "\n";
    }

    # get and check the collection
    if (($collection = &util::use_collection(@ARGV)) eq "") {
	&print_usage();
	die "\n";
    }

    # only here to prevent a perl warning
    $maxdocs = "" if $maxdocs !~ /\d/; 
    $allclassifications = 0 unless $allclassifications == 1;

    # read the configuration file
    $textindex = "section:text";
    $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc/collect.cfg");
    if (-e $configfilename) {
	$collectcfg = &colcfg::read_collect_cfg ($configfilename);
	if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
	    $archivedir = $collectcfg->{'archivedir'};
	}
	if (defined $collectcfg->{'cachedir'} && $cachedir eq "") {
	    $cachedir = $collectcfg->{'cachedir'};
	}
	if (defined $collectcfg->{'builddir'} && $builddir eq "") {
	    $builddir = $collectcfg->{'builddir'};
	}
	if (defined $collectcfg->{'textindex'}) {
	    $textindex = $collectcfg->{'textindex'};
	}
    } else {
	die "Couldn't find the configuration file $configfilename\n";
    }
    
    # fill in the default archives and building directories if none
    # were supplied, turn all \ into / and remove trailing /
    $archivedir = "$ENV{'GSDLCOLLECTDIR'}/archives" if $archivedir eq "";
    $archivedir =~ s/[\\\/]+/\//g;
    $archivedir =~ s/\/$//;
    $builddir = "$ENV{'GSDLCOLLECTDIR'}/building" if $builddir eq "";
    $builddir =~ s/[\\\/]+/\//g;
    $builddir =~ s/\/$//;

    # update the archive cache if needed
    if ($cachedir) {
	print STDERR "Updating archive cache\n" if ($verbosity >= 1);

	$cachedir =~ s/[\\\/]+$//;
	$cachedir .= "/collect/$collection" unless 
	    $cachedir =~ /collect\/$collection/;

	$realarchivedir = "$cachedir/archives";
	$realbuilddir = "$cachedir/building";
	&util::mk_all_dir ($realarchivedir);
	&util::mk_all_dir ($realbuilddir);
	&util::cachedir ($archivedir, $realarchivedir, $verbosity);

    } else {
	$realarchivedir = $archivedir;
	$realbuilddir = $builddir;
    }

    # build it in realbuilddir
    &util::mk_all_dir ($realbuilddir);


    # if a builder class has been created for this collection, use it
    # otherwise, use the mg builder
    if (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}builder.pm") {
	$builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
	$buildertype = "${collection}builder";
    } else {
	$builderdir = "$ENV{'GSDLHOME'}/perllib";
	$buildertype = "mgbuilder";
    }
	
    require "$builderdir/$buildertype.pm";

    eval("\$builder = new $buildertype(\$collection, " .
	 "\$realarchivedir, \$realbuilddir, \$verbosity, " .
	 "\$maxdocs, \$allclassifications)");
    die "$@" if $@;

    $builder->init();
    $builder->compress_text($textindex);
    $builder->build_indexes();
    $builder->make_infodatabase();
    $builder->collect_specific();
    $builder->make_auxiliary_files();
    $builder->deinit();


    if ($realbuilddir ne $builddir) {
	print STDERR "Copying back the cached build\n" if ($verbosity >= 1);
	&util::rm_r ($builddir);
	&util::cp_r ($realbuilddir, $builddir);
    }
}


