#!/usr/bin/perl -w

###########################################################################
#
# incremental-buildcol.pl -- runs buildcol.pl with -incremental on
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2009 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 is designed to support incremental building of Greenstone
# Runs:        buildcol.pl -incremental -builddir GSDLHOME/collect/$collect/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");
}


use strict;
no strict 'refs'; # allow filehandles to be variables and vice versa
no strict 'subs'; # allow barewords (eg STDERR) as function arguments

use util;
use colcfg;

sub main
{
    my ($argc,@argv) = @_;

    if (($argc==0)  || (($argc==1) && ($argv[0] =~ m/^--?h(elp)?$/))) {
	my ($progname) = ($0 =~ m/^.*\/(.*?)$/);


	print STDERR "\n";
	print STDERR "Usage: $progname [buildcol.pl options] collection\n";
	print STDERR "\n";

	exit(-1);
    }


    my $collect = pop @argv;

    my @filtered_argv = ();

    my $collect_dir = undef;
    my $build_dir  = undef;
    my $site = undef;

    while (my $arg = shift @argv) {
	if ($arg eq "-collectdir") {
	    $collect_dir = shift @argv;
	    push(@filtered_argv,$arg,$collect_dir);
	}
	elsif ($arg eq "-builddir") {
	    $build_dir = shift @argv;
	    push(@filtered_argv,$arg,$build_dir);
	}
	elsif ($arg eq "-site") {
	    $site = shift @argv;
	    push(@filtered_argv,$arg,$site);
	}
	else {
	    push(@filtered_argv,$arg);
	}
    }


    if (!defined $collect_dir) {
	$collect_dir = &util::filename_cat($ENV{'GSDLHOME'},"collect");
    }
    my $this_collect_dir = &util::filename_cat($collect_dir,$collect);

    if (!-d $this_collect_dir) {
	print STDERR "Error: $collect_dir does not exist\n";
	exit(-1);
    }


    if (!defined $build_dir) {
	# Yes this is intentional that 'build_dir' points to "index"
	$build_dir = &util::filename_cat($this_collect_dir,"index");
	push(@filtered_argv,"-builddir",$build_dir);
    }

    my $quoted_argv = join(" ", map { "\"$_\"" } @filtered_argv);
    
    my $buildcol_cmd = "buildcol.pl";
    if($ENV{'GSDLOS'} =~ m/windows/) {
	if($ENV{'PERLPATH'}) {
	    # need to ensure that the path to perl is quoted (in case there's spaces in it)
	    $buildcol_cmd = "\"$ENV{'PERLPATH'}\\Perl.exe\" -S $buildcol_cmd";
	} else {
	    $buildcol_cmd = "perl -S $buildcol_cmd";
	}
    }

    if ((&colcfg::use_collection($site, $collect, $collect_dir)) eq "") {
	print STDERR "Unable to use collection \"$collect\" within \"$collect_dir\"\n";
	exit -1;
    }
    # Read in the collection configuration file.
    my ($collect_cfg_filename, $gs_mode) = &colcfg::get_collect_cfg_name(STDERR);
	
    my $collectcfg = &colcfg::read_collection_cfg ($collect_cfg_filename,$gs_mode);

    # look for build.cfg/buildConfig.xml
    my $build_cfg_filename ="";

    if ($gs_mode eq "gs2") {
	$build_cfg_filename = &util::filename_cat($build_dir,"build.cfg");
    } else {
	$build_cfg_filename = &util::filename_cat($build_dir, "buildConfig.xml");
    }
    
    if (-e $build_cfg_filename) {

	# figure out if there has been a change of indexer 
	# (e.g. collect.cfg now says lucene, but build.cfg says mgpp)

	my $buildcfg = &colcfg::read_building_cfg ($build_cfg_filename, $gs_mode);
	if ($buildcfg->{'buildtype'} ne $collectcfg->{'buildtype'}) {
	    print STDERR "*****\n";
	    print STDERR "* Change of indexer detected. Switching to full buildcol.pl.\n";
	    print STDERR "*****\n";
	    $buildcol_cmd .= " -removeold";
	}
	else {

	    $buildcol_cmd .= " -incremental";
	}
    }
    else {
	# build.cfg doesn't exit
	print STDERR "*****\n";
	print STDERR "* First time built. Switching to full buildcol.pl.\n";
	print STDERR "*****\n";
	$buildcol_cmd .= " -removeold";
    }

    
    $buildcol_cmd .= " $quoted_argv \"$collect\"";
    
    my $buildcol_status = system($buildcol_cmd)/256;
    
    if ($buildcol_status != 0) {
	print STDERR "Error: Failed to run: $buildcol_cmd\n";
	print STDERR "       $!\n" if ($! ne "");
	exit(-1);
    }
}

&main(scalar(@ARGV),@ARGV);




