#!/usr/bin/perl -w

###########################################################################
#
# buildcol.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 build a particular collection.

package buildcol;

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/cpan");
  unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/XML/XPath");
  unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
  unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");

  if (defined $ENV{'GSDLEXTS'})
  {
    my @extensions = split(/:/,$ENV{'GSDLEXTS'});
    foreach my $e (@extensions)
    {
      my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
      unshift (@INC, "$ext_prefix/perllib");
      unshift (@INC, "$ext_prefix/perllib/cpan");
      unshift (@INC, "$ext_prefix/perllib/plugins");
      unshift (@INC, "$ext_prefix/perllib/classify");
    }
  }
  if (defined $ENV{'GSDL3EXTS'})
  {
    my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
    foreach my $e (@extensions)
    {
      my $ext_prefix = "$ENV{'GSDL3SRCHOME'}/ext/$e";
      unshift (@INC, "$ext_prefix/perllib");
      unshift (@INC, "$ext_prefix/perllib/cpan");
      unshift (@INC, "$ext_prefix/perllib/plugins");
      unshift (@INC, "$ext_prefix/perllib/classify");
    }
  }
}

use colcfg;
use dbutil;
use util;
use scriptutil;
use FileHandle;
use gsprintf;
use printusage;
use parse2;

use strict;
no strict 'refs'; # allow filehandles to be variables and vice versa
no strict 'subs'; # allow barewords (eg STDERR) as function arguments


my $mode_list =
    [ { 'name' => "all",
        'desc' => "{buildcol.mode.all}" },
      { 'name' => "compress_text",
        'desc' => "{buildcol.mode.compress_text}" },
      { 'name' => "build_index",
        'desc' => "{buildcol.mode.build_index}" },
      { 'name' => "infodb",
        'desc' => "{buildcol.mode.infodb}" } ];

my $sec_index_list = 
    [ {'name' => "never",
       'desc' => "{buildcol.sections_index_document_metadata.never}" },
      {'name' => "always",
       'desc' => "{buildcol.sections_index_document_metadata.always}" },
      {'name' => "unless_section_metadata_exists",
       'desc' => "{buildcol.sections_index_document_metadata.unless_section_metadata_exists}" }
      ];

my $arguments =
    [ { 'name' => "remove_empty_classifications",
	'desc' => "{buildcol.remove_empty_classifications}",
	'type' => "flag",
	'reqd' => "no",
	'modegli' => "2" },
      { 'name' => "archivedir",
	'desc' => "{buildcol.archivedir}",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "builddir",
	'desc' => "{buildcol.builddir}",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "yes" },
#     { 'name' => "cachedir",
#	'desc' => "{buildcol.cachedir}",
#	'type' => "string",
#	'reqd' => "no" },
      { 'name' => "collectdir",
	'desc' => "{buildcol.collectdir}",
	'type' => "string",
	# parsearg left "" as default
	#'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "site",
	'desc' => "{buildcol.site}",
	'type' => "string",
	'deft' => "",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "debug",
	'desc' => "{buildcol.debug}",
	'type' => "flag",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "faillog",
	'desc' => "{buildcol.faillog}",
	'type' => "string",
	# parsearg left "" as default
	#'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
	'reqd' => "no",
	'modegli' => "3" },
      # Parallel Build Customization
      # - this option was called 'index' but that doesn't get thru parsing [jmt12]
      { 'name' => "indexname",
	'desc' => "{buildcol.index}",
	'type' => "string",
	'reqd' => "no",
	'modegli' => "3" },
      # Parallel Build Customization
      # - a new option to separate building of index levels [jmt12]
      { 'name' => "indexlevel",
	'desc' => "{buildcol.indexlevel}",
	'type' => "string",
	'reqd' => "no",
	'modegli' => "3" },
      { 'name' => "incremental",
	'desc' => "{buildcol.incremental}",
	'type' => "flag",
	'hiddengli' => "yes" },
      { 'name' => "keepold",
	'desc' => "{buildcol.keepold}",
	'type' => "flag",
	'reqd' => "no",
        #'modegli' => "3",
	'hiddengli' => "yes" },
      { 'name' => "removeold",
	'desc' => "{buildcol.removeold}",
	'type' => "flag",
	'reqd' => "no",
	#'modegli' => "3",
	'hiddengli' => "yes"  },
      { 'name' => "language",
	'desc' => "{scripts.language}",
	'type' => "string",
	'reqd' => "no",
	'modegli' => "3" },
      { 'name' => "maxdocs",
	'desc' => "{buildcol.maxdocs}",
	'type' => "int",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "maxnumeric",
	'desc' => "{buildcol.maxnumeric}",
	'type' => "int",
	'reqd' => "no",
	'deft' => "4",
	'range' => "4,512",
	'modegli' => "3" },
      { 'name' => "mode",
	'desc' => "{buildcol.mode}",
	'type' => "enum",
	'list' => $mode_list,
	# parsearg left "" as default
#	'deft' => "all",
	'reqd' => "no",
	'modegli' => "3" },
      { 'name' => "no_strip_html",
	'desc' => "{buildcol.no_strip_html}",
	'type' => "flag",
	'reqd' => "no",
	'modegli' => "3" },
      { 'name' => "no_text",
	'desc' => "{buildcol.no_text}",
	'type' => "flag",
	'reqd' => "no",
	'modegli' => "2" },
      { 'name' => "sections_index_document_metadata",
	'desc' => "{buildcol.sections_index_document_metadata}",
	'type' => "enum",
	'list' => $sec_index_list,
	'reqd' => "no",
	'modegli' => "2" },
      { 'name' => "out",
	'desc' => "{buildcol.out}",
	'type' => "string",
	'deft' => "STDERR",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "verbosity",
	'desc' => "{buildcol.verbosity}",
	'type' => "int",
	# parsearg left "" as default
	#'deft' => "2",
	'reqd' => "no",
	'modegli' => "3" },
      { 'name' => "gli",
	'desc' => "",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "xml",
	'desc' => "{scripts.xml}",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "parallel",
	'desc' => "{scripts.parallel}",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      ];

my $options = { 'name' => "buildcol.pl",
		'desc' => "{buildcol.desc}",
		'args' => $arguments };


# globals
my $collection;
my $configfilename;
my $out;

# used to signify "gs2"(default) or "gs3"
my $gs_mode = "gs2";

## @method gsprintf()
#  Print a string to the screen after looking it up from a locale dependant
#  strings file. This function is losely based on the idea of resource
#  bundles as used in Java.
#
#  @param  $error The STDERR stream.
#  @param  $text The string containing GS keys that should be replaced with
#                their locale dependant equivilents.
#  @param  $out The output stream.
#  @return The locale-based string to output.
#
sub gsprintf()
{
    return &gsprintf::gsprintf(@_);
}
## gsprintf() ##

&main();

## @method main()
#
#  [Parses up and validates the arguments to the build process before creating
#  the appropriate build process to do the actual work - John]
#
#  @note Added true incremental support - John Thompson, DL Consulting Ltd.
#  @note There were several bugs regarding using directories other than 
#        "import" or "archives" during import and build quashed. - John 
#        Thompson, DL Consulting Ltd.
#
#  @param  $incremental If true indicates this build should not regenerate all
#                       the index and metadata files, and should instead just
#                       append the information found in the archives directory
#                       to the existing files. If this requires some complex
#                       work so as to correctly insert into a classifier so be
#                       it. Of course none of this is done here - instead the
#                       incremental argument is passed to the document
#                       processor.
#
sub main
{
  # command line args
  # - new argument to allow control of index level [jmt12]
  my ($verbosity, $archivedir, $cachedir, $builddir, $site, $maxdocs, 
      $debug, $mode, $indexname, $removeold, $keepold, 
      $incremental, $incremental_mode,
      $remove_empty_classifications,
      $collectdir, $build, $type, $textindex,
      $no_strip_html, $no_text, $faillog, $gli, $index, $language,
      $sections_index_document_metadata, $maxnumeric, $indexlevel,
      $parallel);

    my $xml = 0;
    my $hashParsingResult = {};
    # general options available to all plugins
    my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");

    # If parse returns -1 then something has gone wrong
    if ($intArgLeftinAfterParsing == -1)
    {
	&PrintUsage::print_txt_usage($options, "{buildcol.params}");
	die "\n";
    }
    
    foreach my $strVariable (keys %$hashParsingResult)
    {
	eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
    }

    # If $language has been specified, load the appropriate resource bundle
    # (Otherwise, the default resource bundle will be loaded automatically)
    if ($language && $language =~ /\S/) {
	&gsprintf::load_language_specific_resource_bundle($language);
    }

    if ($xml) {
        &PrintUsage::print_xml_usage($options);
	print "\n";
	return;
    }

    if ($gli) { # the gli wants strings to be in UTF-8
	&gsprintf::output_strings_in_UTF8; 
    }

    # now check that we had exactly one leftover arg, which should be 
    # the collection name. We don't want to do this earlier, cos 
    # -xml arg doesn't need a collection name
    # Or if the user specified -h, then we output the usage also
    if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
    {
	&PrintUsage::print_txt_usage($options, "{buildcol.params}");
	die "\n";
    }
    
    $textindex = "";
    my $close_out = 0;
    if ($out !~ /^(STDERR|STDOUT)$/i) {
	open (OUT, ">$out") ||
	    (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
	$out = "buildcol::OUT";
	$close_out = 1;
    }
    $out->autoflush(1);

    # get and check the collection
    if (($collection = &colcfg::use_collection($site, @ARGV, $collectdir)) eq "") {
	&PrintUsage::print_txt_usage($options, "{buildcol.params}");
	die "\n";
    }

    if ($faillog eq "") {
	$faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
    }
    # note that we're appending to the faillog here (import.pl clears it each time)
    # this could potentially create a situation where the faillog keeps being added 
    # to over multiple builds (if the import process is being skipped)
    open (FAILLOG, ">>$faillog") ||
	(&gsprintf(STDERR, "{common.cannot_open_fail_log}\n", $faillog) && die);
    $faillog = 'buildcol::FAILLOG';
    $faillog->autoflush(1);

    # Don't know why this didn't already happen, but now collection specific
    # classify and plugins directory also added to include path
    unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib"); # [jmt12]
    unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib/classify"); # [jmt12]
    unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib/plugins"); # [jmt12]

    # Read in the collection configuration file.
    my ($collectcfg, $buildtype, $orthogonalbuildtypes); 
    ($configfilename, $gs_mode) = &colcfg::get_collect_cfg_name($out);
    $collectcfg = &colcfg::read_collection_cfg ($configfilename, $gs_mode);
    
    # If the infodbtype value wasn't defined in the collect.cfg file, use the default
    if (!defined($collectcfg->{'infodbtype'}))
    {
      $collectcfg->{'infodbtype'} = &dbutil::get_default_infodb_type();
    }
    # sanity check - you currently can't have SQLite as the infodb while
    # asking for a parallel build
    elsif ($collectcfg->{'infodbtype'} eq 'sqlite' && $parallel)
    {
      print STDERR "WARNING: Parallel builds not current supported by SQLite - reverting to serial build\n";
      $parallel = 0;
    }

    if ($verbosity !~ /\d+/) {
	if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
	    $verbosity = $collectcfg->{'verbosity'};
	} else {
	    $verbosity = 2; # the default
	}
    }
    # we use searchtype for determining buildtype, but for old versions, use buildtype
    if (defined $collectcfg->{'buildtype'}) {
	$buildtype = $collectcfg->{'buildtype'};
    } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
	$buildtype = "mgpp";
    } else {
	$buildtype = "mg"; #mg is the default
    }

    if (defined $collectcfg->{'orthogonalbuildtypes'}) {
	$orthogonalbuildtypes = $collectcfg->{'orthogonalbuildtypes'};
    }

    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 ($maxdocs !~ /\-?\d+/) {
	if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
	    $maxdocs = $collectcfg->{'maxdocs'};
	} else {
	    $maxdocs = -1; # the default
	}
    }
    if (defined $collectcfg->{'maxnumeric'} && $collectcfg->{'maxnumeric'} =~ /\d+/) {
	$maxnumeric = $collectcfg->{'maxnumeric'};
    } 
    
    if ($maxnumeric < 4 || $maxnumeric > 512) {
	$maxnumeric = 4;
    }
    
    if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
	$debug = 1;
    }
    if ($mode !~ /^(all|compress_text|build_index|infodb)$/) {
	if (defined $collectcfg->{'mode'} && $collectcfg->{'mode'} =~ /^(all|compress_text|build_index|infodb)$/) {
	    $mode = $collectcfg->{'mode'};
	} else {
	    $mode = "all"; # the default
	}
    }
    # - 'index' doesn't make it through parsing so I renamed this option
    # 'indexname' [jmt12]
    if (defined $collectcfg->{'indexname'} && $indexname eq "")
    {
      $indexname = $collectcfg->{'indexname'};
    }
    # - we may also define the index level to build now
    if (defined $collectcfg->{'indexlevel'} && $indexlevel eq "")
    {
      $indexlevel = $collectcfg->{'indexlevel'};
    }
    if (defined $collectcfg->{'no_text'} && $no_text == 0) {
	if ($collectcfg->{'no_text'} =~ /^true$/i) {
	    $no_text = 1;
	}
    }
    if (defined $collectcfg->{'no_strip_html'} && $no_strip_html == 0) {
	if ($collectcfg->{'no_strip_html'} =~ /^true$/i) {
	    $no_strip_html = 1;
	}
    }
    if (defined $collectcfg->{'remove_empty_classifications'} && $remove_empty_classifications == 0) {
	if ($collectcfg->{'remove_empty_classifications'} =~ /^true$/i) {
	    $remove_empty_classifications = 1;
	}
    }
    
    if ($buildtype eq "mgpp" && defined $collectcfg->{'textcompress'}) {
	$textindex = $collectcfg->{'textcompress'};
    }
    if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
	$gli = 1;
    }

    if ($sections_index_document_metadata !~ /\S/ && defined $collectcfg->{'sections_index_document_metadata'}) {
	$sections_index_document_metadata = $collectcfg->{'sections_index_document_metadata'};
    }
    
    if ($sections_index_document_metadata !~ /^(never|always|unless_section_metadata_exists)$/) {
	$sections_index_document_metadata = "never";
    }
    
    ($removeold, $keepold, $incremental, $incremental_mode) 
	= &scriptutil::check_removeold_and_keepold($removeold, $keepold, 
						   $incremental, "building", 
						   $collectcfg);
 
    $gli = 0 unless defined $gli;
    
    # New argument to track whether build is incremental
    $incremental = 0 unless defined $incremental;

    print STDERR "<Build>\n" if $gli;

    #set the text index
    if (($buildtype eq "mgpp") || ($buildtype eq "lucene")) {
	if ($textindex eq "") {
	    $textindex = "text";
	}
    }
    else {
	$textindex = "section:text";
    }

    # fill in the default archives and building directories if none
    # were supplied, turn all \ into / and remove trailing /

    my ($realarchivedir, $realbuilddir);
    # Modified so that the archivedir, if provided as an argument, is made
    # absolute if it isn't already
    if ($archivedir eq "")
      {
        $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
      }
    else
      {
        $archivedir = &util::make_absolute($ENV{'GSDLCOLLECTDIR'}, $archivedir);
      }
    # End Mod
    $archivedir =~ s/[\\\/]+/\//g;
    $archivedir =~ s/\/$//;

    if ($builddir eq "") {
	$builddir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building");
	if ($incremental) {
	    &gsprintf($out, "{buildcol.incremental_default_builddir}\n");
	}
    }
    $builddir =~ s/[\\\/]+/\//g;
    $builddir =~ s/\/$//;

    # update the archive cache if needed
    if ($cachedir) {
	&gsprintf($out, "{buildcol.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);

    my ($buildertype, $builderdir,  $builder);
    # if a builder class has been created for this collection, use it
    # otherwise, use the mg or mgpp builder
    if (-e "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib/custombuilder.pm") {
	$builderdir = "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib";
	$buildertype = "custombuilder";
    } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/custombuilder.pm") {
	$builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
	$buildertype = "custombuilder";
    } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}builder.pm") {
	$builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
	$buildertype = "${collection}builder";
    } else {	

	$builderdir = undef;
	if ($buildtype ne "") {
	    # caters for extension-based build types, such as 'solr'
	    $buildertype = $buildtype."builder";
	}
	else {
	    # Default to mgpp
	    $buildertype = "mgppbuilder";
	}
    }
    # check for extension specific builders 
    # (that will then be run after main builder.pm

    my @builderdir_list = ($builderdir);
    my @buildertype_list = ($buildertype);
    
    if (defined $orthogonalbuildtypes) {
	foreach my $obt (@$orthogonalbuildtypes) {

	    push(@builderdir_list,undef); # rely on @INC to find it
	    push(@buildertype_list,$obt."Builder");
	}
    }

    # Set up array of the main builder.pm, followed by any ones
    # from the extension folders
    
    my $num_builders = scalar(@buildertype_list);
    my @builders = ();

    for (my $i=0; $i<$num_builders; $i++) {
	my $this_builder;
	my $this_buildertype = $buildertype_list[$i];
	my $this_builderdir  = $builderdir_list[$i];

	if ((defined $this_builderdir) && ($this_builderdir ne "")) {
	    require "$this_builderdir/$this_buildertype.pm";
	}
	else {
	    require "$this_buildertype.pm";
	}

	eval("\$this_builder = new $this_buildertype(\$site, \$collection, " .
	     "\$realarchivedir, \$realbuilddir, \$verbosity, " .
	     "\$maxdocs, \$debug, \$keepold, \$incremental, \$incremental_mode, " .
	     "\$remove_empty_classifications, " .
	     "\$out, \$no_text, \$faillog, \$gli)");
	die "$@" if $@;

	push(@builders,$this_builder);
    }
    
    # Init phase for builders
    for (my $i=0; $i<$num_builders; $i++) {
	my $this_buildertype = $buildertype_list[$i];
	my $this_builderdir  = $builderdir_list[$i];
	my $this_builder     = $builders[$i];

	$this_builder->init();
	$this_builder->set_maxnumeric($maxnumeric);
    
	if (($this_buildertype eq "mgppbuilder") && $no_strip_html) {
	    $this_builder->set_strip_html(0);
	}
	if ($sections_index_document_metadata ne "never") {
	    $this_builder->set_sections_index_document_metadata($sections_index_document_metadata);
	}
    }
        
    # Run the requested passes

  # Parallel Building Support
  # - if parallel building is requested then we subvert the normal 'all' mode
  #   process, insert attempting to create an XML 'recipe' for building this
  #   collection. We then pass this recipe to an Open MPI augmented compiled
  #   executable (which will in turn make multiple calls back to buildcol.pl
  #   according to the instructions in the recipe)!
  if ($parallel)
  {
    print $out "*** parallel building\n";
    # Some infodb modes (namely GDBMServer at the moment) need to open the
    # connection to the database in such a way that it persists over the
    # child threads. We do this by adding a dummy call to build the file path
    # to archiveinf-doc as it is the database in question. The '1' at the end
    # means launch the server... it will then persist until this block passes
    # out of scope (presumably after all the child mpi processes are done)
    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($collectcfg->{'infodbtype'}, "archiveinf-doc", $archivedir, 1);

    # we initially create the recipe as a datastructure to make it easier for
    # each builder to determine what has already been defined
    # - each step of the recipe will have a command as a string and a (possibly
    #   empty) array of steps that depend on this step (possibly recursive)
    print $out "Generating build 'recipe'\n";
    my $recipe = [];
    # pass to each builder to have it populated with appropriate commands
    map { local $_=$_; $_->prepare_build_recipe($collection, $recipe); } @builders;
    # now write the recipe to an XML file, resolving any path macros
    my $max_parallel_tasks = scalar(@{$recipe});
    my $xml_lines = ();
    push(@{$xml_lines},'<?xml version="1.0" standalone="no" ?>');
    push(@{$xml_lines},'<Recipe>');
    foreach my $item (@{$recipe})
    {
      my $max_parallel_child_tasks = &print_recipe($xml_lines, $item);
      if ($max_parallel_child_tasks > $max_parallel_tasks)
      {
        $max_parallel_tasks = $max_parallel_child_tasks;
      }
    }
    push(@{$xml_lines}, '</Recipe>');
    my $recipe_path = &util::get_tmp_filename('.xml');
    open(XMLOUT, ">:utf8", $recipe_path) or die("Error! Failed to open recipe file for writing: " . $recipe_path . "\nReason: " . $!);
    print XMLOUT join("\n", @{$xml_lines});
    close(XMLOUT);
    # determine the 'optimal' number of threads (based on number of processor
    # cores and number of indexes)
    my $number_of_threads = $max_parallel_tasks + 1; # any more is waste
    my $number_of_cores = `grep "processor" /proc/cpuinfo | wc -l`;
    $number_of_cores =~ s/\r?\n//g;
    print $out "Calculating optimal threads => Max parallel tasks: $max_parallel_tasks, Number of cores: $number_of_cores\n";
    if ($number_of_cores =~ /^\d+$/ && $max_parallel_tasks > $number_of_cores)
    {
      # optimal threads for processor bound load (even though it's probably
      # IO load that has us bound)
      $number_of_threads = $number_of_cores + 1;
    }
    print $out "Optimal threads: " . $number_of_threads . "\n";
    # call mpibuildcol executable using mpirun and passing path to recipe
    #rint "The path *in* Perl: " . $ENV{'PATH'} . "\n";
    my $mpirun_cmd = 'mpirun -n ' . $number_of_threads . ' mpibuildcol "' . $recipe_path . '"';
    #$mpirun_cmd = 'echo $PATH; echo $PERLLIB;';
    print $out "Running command: " . $mpirun_cmd . "\n";
    print `$mpirun_cmd`;
    # clean up recipe
    #unlink($recipe_path);
  }
  # Normal mode - run compress text, followed by index building, followed by
  # infodb
  elsif ($mode =~ /^all$/i) {

	# 'map' modifies the elements of the original array, so calling
	# methods -- as done below -- will cause (by default) @builders
	# to be changed to whatever these functions return (which is *not*
	# what we want -- we want to leave the values unchanged) 
	# => Use 'local' (dynamic scoping) to give each 'map' call its
	#    own local copy This could also be done with: 
	#      (my $new =$_)->method(); $new 
	#    but is a bit more cumbersome to write
	
	map { local $_=$_; $_->compress_text($textindex); } @builders;
        # - note we pass the required indexname and indexlevel (if specified)
        #   to the processor
	map { local $_=$_; $_->build_indexes($indexname, $indexlevel); } @builders;
	map { local $_=$_; $_->make_infodatabase(); }  @builders;
	map { local $_=$_; $_->collect_specific(); } @builders;
    } elsif ($mode =~ /^compress_text$/i) {
	map { local $_=$_; $_->compress_text($textindex); } @builders;
    } elsif ($mode =~ /^build_index$/i) {
	map { local $_=$_; $_->build_indexes($indexname, $indexlevel); } @builders;
    } elsif ($mode =~ /^infodb$/i) {
	map { local $_=$_; $_->make_infodatabase(); } @builders;
    } else {
	(&gsprintf(STDERR, "{buildcol.unknown_mode}\n", $mode) && die);
    }

    if (!$debug && !$parallel) {
	map {local $_=$_; $_->make_auxiliary_files(); } @builders;
    }
    map {local $_=$_; $_->deinit(); } @builders;
    
    if (($realbuilddir ne $builddir) && !$debug) {
	&gsprintf($out, "{buildcol.copying_back_cached_build}\n")
	    if ($verbosity >= 1);
	&util::rm_r ($builddir);
	&util::cp_r ($realbuilddir, $builddir);
    }

    close OUT if $close_out;
    close FAILLOG;

    print STDERR "</Build>\n" if $gli;
}

sub print_recipe
{
  my ($xml_lines, $item) = @_;
  my $max_parallel_tasks = 0;

  # start building up the command in our xml buffer
  push(@{$xml_lines}, '<Task>');
  my $command = $item->{'command'};
  $command =~ s/&/&amp;/g;
  $command =~ s/</&lt;/g;
  $command =~ s/>/&gt;/g;
  push(@{$xml_lines}, '<Command>' . $command . '</Command>');

  # - print children before closing task
  if (defined $item->{'children'})
  {
    $max_parallel_tasks = scalar(@{$item->{'children'}});

    foreach my $child_item (@{$item->{'children'}})
    {
      my $max_parallel_child_tasks = &print_recipe($xml_lines, $child_item);
      if ($max_parallel_child_tasks > $max_parallel_tasks)
      {
        $max_parallel_tasks = $max_parallel_child_tasks;
      }
    }
  }
  # - now we can close the task having printed nested children
  push(@{$xml_lines},'</Task>');
  # done
  return $max_parallel_tasks;
}
