#!/usr/bin/perl

# This perl script may be called directly or by running build.bat on
# windows (build.bat is in bin\windows)

BEGIN {

    die "GSDLHOME not set - did you remember to source setup.bash (unix) or " .
	"run setup.bat (windows)?\n" unless defined $ENV{'GSDLHOME'};
    die "GSDLOS not set - did you remember to source setup.bash (unix) or " .
	"run setup.bat (windows)?\n" unless defined $ENV{'GSDLOS'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}

use parsargv;
use util;


if (!parsargv::parse(\@ARGV, 
		     'buildtype/^(build|import)$/import', \$buildtype,
		     'maxdocs/^\-?\d+/-1', \$maxdocs)) {
    &print_usage();
    die "\n";
}

my ($collection) = @ARGV;

if (!defined $collection || $collection !~ /\w/) {
    print STDERR "You must specify a collection to build\n";
    &print_usage();
    die "\n";
}

if ($maxdocs == -1) {
    $maxdocs = "";
} else {
    $maxdocs = "-maxdocs $maxdocs";
}


my $collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
my $importdir = &util::filename_cat ($collectdir, "import");
my $archivedir = &util::filename_cat ($collectdir, "archives");
my $buildingdir = &util::filename_cat ($collectdir, "building");
my $indexdir = &util::filename_cat ($collectdir, "index");
my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");

&main();

sub print_usage {
    print STDERR "\n  usage: $0 [options] collection-name\n\n";
    print STDERR "  options:\n";
    print STDERR "   -buildtype build|import If 'build' attempt to build directly\n";
    print STDERR "                           from archives directory (bypassing import\n";
    print STDERR "                           stage). Defaults to 'import'\n";
    print STDERR "   -maxdocs number         Maximum number of documents to build\n\n";
}

sub main {

    if (-e &util::filename_cat ($archivedir, "archives.inf")) {
	if (&has_content ($importdir)) {
	    if ($buildtype eq "build") {
		&gsdl_build();
	    } else {
		&gsdl_import();
		&gsdl_build();
	    }
	} else {
	    # there are archives but no import, build directly from archives
	    print STDERR "build: no import material was found, building directly\n";
	    print STDERR "       from archives\n";
	    &gsdl_build();
	}
    } else {
	if (&has_content ($importdir)) {
	    if ($buildtype eq "build") {
		print STDERR "build: can't build directly from archives as no\n";
		print STDERR "       imported archives exist (did you forget to\n";
		print STDERR "       move the contents of $collection/import to\n";
		print STDERR "       collection/archives?)\n";
	    }
	    &gsdl_import();
	    &gsdl_build();
	} else {
	    # no import or archives
	    print STDERR "build: ERROR: The $collection collection has no import\n";
	    print STDERR "       or archives data. Try downloading an unbuilt version\n";
	    print STDERR "       of the collection from www.nzdl.org\n";
	    die "\n";
	}
    }
}

sub gsdl_import {

    print STDERR "importing the $collection collection\n\n";

    my $import = &util::filename_cat ($bindir, "script", "import.pl");
    system ("perl $import -removeold $maxdocs $collection");
    if (-e &util::filename_cat ($archivedir, "archives.inf")) {
	print STDERR "$collection collection imported successfully\n\n";
    } else {
	die "\nimport.pl failed\n";
    }
}

sub gsdl_build {

    print STDERR "building the $collection collection\n\n";

    my $buildcol = &util::filename_cat ($bindir, "script", "buildcol.pl");
    system ("perl $buildcol $maxdocs $collection");
    if (-e &util::filename_cat ($buildingdir, "text", "$collection.ldb") ||
	-e &util::filename_cat ($buildingdir, "text", "$collection.bdb")) {
	print STDERR "$collection collection built successfully\n\n";
    } else {
	die "\nbuildcol.pl failed\n";
    }

    # replace old indexes with new ones
    if (&has_content ($indexdir)) {
	print STDERR "removing old indexes\n";
	&util::rm_r ($indexdir);
    }
    rmdir ($indexdir) if -d $indexdir;
    rename ($buildingdir, $indexdir);
}

sub has_content {
    my ($dir) = @_;

    if (!-d $dir) {return 0;}

    opendir (DIR, $dir) || return 0;
    my @files = readdir DIR;
    close DIR;
    
    foreach my $file (@files) {
	if ($file !~ /^\.{1,2}$/) {
	    return 1;
	}
    }
    return 0;
}
