#!/usr/bin/perl # This perl script is intended mostly for use on unix systems (hence no .pl # extension and the use of '/'s in paths). On windows it's suggested you # use build.bat (it's the same perl code with a batch file wrapper). BEGIN { die "GSDLHOME not set - did you remember to source setup.bash?\n" unless defined $ENV{'GSDLHOME'}; die "GSDLOS not set - did you remember to source setup.bash?\n" unless defined $ENV{'GSDLOS'}; unshift (@INC, "$ENV{'GSDLHOME'}/perllib"); } use parsargv; use util; &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 (!parsargv::parse(\@ARGV, 'buildtype/^(build|import)$/import', \$buildtype, 'maxdocs/^\-?\d+/-1', \$maxdocs)) { &print_usage(); die "\n"; } ($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"; } if (-e "$ENV{'GSDLHOME'}/collect/$collection/archives/archives.inf") { if (&has_content ("$ENV{'GSDLHOME'}/collect/$collection/import")) { 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 ("$ENV{'GSDLHOME'}/collect/$collection/import")) { 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"; system ("perl $ENV{'GSDLHOME'}/bin/script/import.pl -removeold $maxdocs $collection"); if (-e "$ENV{'GSDLHOME'}/collect/$collection/archives/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"; system ("perl $ENV{'GSDLHOME'}/bin/script/buildcol.pl $maxdocs $collection"); if (-e "$ENV{'GSDLHOME'}/collect/$collection/building/text/$collection.ldb" || -e "$ENV{'GSDLHOME'}/collect/$collection/building/text/$collection.bdb") { print STDERR "$collection collection built successfully\n\n"; } else { die "\nbuildcol.pl failed\n"; } # replace old indexes with new ones my $indexdir = "$ENV{'GSDLHOME'}/collect/$collection/index"; if (&has_content ($indexdir)) { print STDERR "removing old indexes\n"; &util::rm_r ($indexdir); rename ("$ENV{'GSDLHOME'}/collect/$collection/building", $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; } __END__ :endofperl