#!/usr/bin/perl -w

###########################################################################
#
# mgppbuildcol.pl -- This program will build a particular collection
# incorporates mgpp.
# 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");
}
    #$ENV{'PATH'} = "$ENV{'GSDLHOME'}/src/mgpp/text:".$ENV{'PATH'};

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 "   -debug                Print output to STDOUT\n";
    print STDERR "   -mode all|compress_text|build_index|infodb\n";
    print STDERR "   -index indexname      Index to build (will build all in\n"; 
    print STDERR "                         config file if not set)\n";
    print STDERR "   -keepold              will not destroy the current contents of the\n";
    print STDERR "                         building directory\n";
    print STDERR "   -allclassifications   Don't remove empty classifications\n\n";
}


sub main
{
    my ($verbosity, $archivedir, $cachedir, $builddir, $maxdocs, 
	$debug, $mode, $indexname, $keepold, $allclassifications);
    if (!parsargv::parse(\@ARGV, 
			 'verbosity/\d+/2', \$verbosity,
			 'archivedir/.*/', \$archivedir,
			 'cachedir/.*/', \$cachedir,
			 'builddir/.*/', \$builddir,
			 'maxdocs/^\-?\d+/-1', \$maxdocs,
			 'debug', \$debug,
			 'mode/^(all|compress_text|build_index|infodb)$/all', \$mode,
			 'index/.*/', \$indexname,
			 'keepold', \$keepold,
			 'allclassifications', \$allclassifications)) {
	&print_usage();
	die "\n";
    }

    # get and check the collection
    # this sets up GSDLCOLLECTION (eg demo) and GSDLCOLLECTDIR (eg
    # GSDLHOME/collect/demo )
    if (($collection = &util::use_collection(@ARGV)) eq "") {
	&print_usage();
	die "\n";
    }

    # read the configuration file
    $textindex = "text";
    $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc/collect.cfg");
    if (-e $configfilename) {
	$collectcfg = &colcfg::read_collect_cfg ($configfilename);
	if (defined $collectcfg->{'textcompress'}) {
	    $textindex = $collectcfg->{'textcompress'};
	}
	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'};
	}
    } 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 = "mgppbuilder";
    }
	
    require "$builderdir/$buildertype.pm";

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

    $builder->init();


    print STDERR "text index=$textindex, indexname= $indexname\n";
    if ($mode =~ /^all$/i) {
#	$builder->build_collection($textindex, $indexname);
	$builder->compress_text($textindex);
	$builder->build_indexes($indexname);
	$builder->make_infodatabase();
	$builder->collect_specific();
    } elsif ($mode =~ /^compress_text$/i) {
	$builder->compress_text($textindex);
    } elsif ($mode =~ /^build_index$/i) {
	$builder->build_indexes($indexname);	
    } elsif ($mode =~ /^infodb$/i) {
	$builder->make_infodatabase();
    } else {
	die "unknown mode: $mode\n";
    }

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


