#!/usr/bin/perl -w

###########################################################################
#
# full-rebuild.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) 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 will rebuild a collection from scratch
# Runs:        full-import.pl -removeold ...
# Followed by: full-buildcol.pl -removeold ...
# (assumming import.pl did not end with an error)


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;

use Config; # for getting the perlpath in the recommended way
use util;


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 [option shared between import.pl and buildcol.pl] collection\n";
	print STDERR "\n";

	exit(-1);
    }
	

    my $collect = pop @argv;


    my @filtered_argv = ();

    my $collect_dir = undef;
    my $build_dir  = 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);
	}
	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) {
	$build_dir = &util::filename_cat($this_collect_dir,"building");
	push(@filtered_argv,"-builddir",$build_dir);
    }

    
    my $quoted_argv = join(" ", map { "\"$_\"" } @argv);
    
    my $final_status = 0;

    my $launch_cmd = "";
    if($ENV{'PERLPATH'}) {
	# need to ensure that the path to perl is quoted (in case there's spaces in it)
	if($ENV{'GSDLOS'} =~ m/windows/) {
	    $launch_cmd = "\"$ENV{'PERLPATH'}\\Perl.exe\" -S ";
	} else {
	    $launch_cmd = "\"$ENV{'PERLPATH'}/perl\" -S ";
	}
    } else {
	#Config{perlpath}, like $^X, is a special variable containing the full path to the current perl executable we are in
	$launch_cmd = "\"$Config{perlpath}\" -S ";
    }
    
    print "\n";
    print "************************\n";
    print "* Running  Import  Stage\n";
    print "************************\n";

    my $import_cmd = $launch_cmd . "full-import.pl $quoted_argv \"$collect\"";

    my $import_status = system($import_cmd)/256;
    
    if ($import_status == 0) {
	print "\n";
	print "************************\n";
	print "* Running Buildcol Stage\n";
	print "************************\n";
	
	my $buildcol_cmd = $launch_cmd . "full-buildcol.pl $quoted_argv \"$collect\"";
	my $buildcol_status = system($buildcol_cmd)/256;
	if ($buildcol_status == 0) {
	    my $index_dir = &util::filename_cat($this_collect_dir,"index");
	    
	    if (-e $index_dir) {
		print "\n";
		print "************************\n";
		print "* Removing \"index\"\n";
		print "************************\n";
		
		&util::rm_r($index_dir);
	    }

	    print "\n";
	    print "************************\n";
	    print "* Moving \"building\" -> \"index\"\n";
	    print "************************\n";

	    &util::mv($build_dir,$index_dir);

	}
	else {
	    $final_status = $buildcol_status;
	}
    }
    else {
	$final_status = $import_status;
    }
    
    exit($final_status);
}

&main(scalar(@ARGV),@ARGV);

