1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | # This perl script may be called directly or by running build.bat on
|
---|
4 | # windows (build.bat is in bin\windows)
|
---|
5 |
|
---|
6 | BEGIN {
|
---|
7 |
|
---|
8 | die "GSDLHOME not set - did you remember to source setup.bash (unix) or " .
|
---|
9 | "run setup.bat (windows)?\n" unless defined $ENV{'GSDLHOME'};
|
---|
10 | die "GSDLOS not set - did you remember to source setup.bash (unix) or " .
|
---|
11 | "run setup.bat (windows)?\n" unless defined $ENV{'GSDLOS'};
|
---|
12 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
13 | }
|
---|
14 |
|
---|
15 | use parsargv;
|
---|
16 | use util;
|
---|
17 |
|
---|
18 |
|
---|
19 | if (!parsargv::parse(\@ARGV,
|
---|
20 | 'buildtype/^(build|import)$/import', \$buildtype,
|
---|
21 | 'maxdocs/^\-?\d+/-1', \$maxdocs)) {
|
---|
22 | &print_usage();
|
---|
23 | die "\n";
|
---|
24 | }
|
---|
25 |
|
---|
26 | my ($collection) = @ARGV;
|
---|
27 |
|
---|
28 | if (!defined $collection || $collection !~ /\w/) {
|
---|
29 | print STDERR "You must specify a collection to build\n";
|
---|
30 | &print_usage();
|
---|
31 | die "\n";
|
---|
32 | }
|
---|
33 |
|
---|
34 | if ($maxdocs == -1) {
|
---|
35 | $maxdocs = "";
|
---|
36 | } else {
|
---|
37 | $maxdocs = "-maxdocs $maxdocs";
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | my $collectdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
42 | my $importdir = &util::filename_cat ($collectdir, "import");
|
---|
43 | my $archivedir = &util::filename_cat ($collectdir, "archives");
|
---|
44 | my $buildingdir = &util::filename_cat ($collectdir, "building");
|
---|
45 | my $indexdir = &util::filename_cat ($collectdir, "index");
|
---|
46 | my $bindir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin");
|
---|
47 |
|
---|
48 | &main();
|
---|
49 |
|
---|
50 | sub print_usage {
|
---|
51 | print STDERR "\n usage: $0 [options] collection-name\n\n";
|
---|
52 | print STDERR " options:\n";
|
---|
53 | print STDERR " -buildtype build|import If 'build' attempt to build directly\n";
|
---|
54 | print STDERR " from archives directory (bypassing import\n";
|
---|
55 | print STDERR " stage). Defaults to 'import'\n";
|
---|
56 | print STDERR " -maxdocs number Maximum number of documents to build\n\n";
|
---|
57 | }
|
---|
58 |
|
---|
59 | sub main {
|
---|
60 |
|
---|
61 | if (-e &util::filename_cat ($archivedir, "archives.inf")) {
|
---|
62 | if (&has_content ($importdir)) {
|
---|
63 | if ($buildtype eq "build") {
|
---|
64 | &gsdl_build();
|
---|
65 | } else {
|
---|
66 | &gsdl_import();
|
---|
67 | &gsdl_build();
|
---|
68 | }
|
---|
69 | } else {
|
---|
70 | # there are archives but no import, build directly from archives
|
---|
71 | print STDERR "build: no import material was found, building directly\n";
|
---|
72 | print STDERR " from archives\n";
|
---|
73 | &gsdl_build();
|
---|
74 | }
|
---|
75 | } else {
|
---|
76 | if (&has_content ($importdir)) {
|
---|
77 | if ($buildtype eq "build") {
|
---|
78 | print STDERR "build: can't build directly from archives as no\n";
|
---|
79 | print STDERR " imported archives exist (did you forget to\n";
|
---|
80 | print STDERR " move the contents of $collection/import to\n";
|
---|
81 | print STDERR " collection/archives?)\n";
|
---|
82 | }
|
---|
83 | &gsdl_import();
|
---|
84 | &gsdl_build();
|
---|
85 | } else {
|
---|
86 | # no import or archives
|
---|
87 | print STDERR "build: ERROR: The $collection collection has no import\n";
|
---|
88 | print STDERR " or archives data. Try downloading an unbuilt version\n";
|
---|
89 | print STDERR " of the collection from www.nzdl.org\n";
|
---|
90 | die "\n";
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | sub gsdl_import {
|
---|
96 |
|
---|
97 | print STDERR "importing the $collection collection\n\n";
|
---|
98 |
|
---|
99 | my $import = &util::filename_cat ($bindir, "script", "import.pl");
|
---|
100 | system ("perl $import -removeold $maxdocs $collection");
|
---|
101 | if (-e &util::filename_cat ($archivedir, "archives.inf")) {
|
---|
102 | print STDERR "$collection collection imported successfully\n\n";
|
---|
103 | } else {
|
---|
104 | die "\nimport.pl failed\n";
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | sub gsdl_build {
|
---|
109 |
|
---|
110 | print STDERR "building the $collection collection\n\n";
|
---|
111 |
|
---|
112 | my $buildcol = &util::filename_cat ($bindir, "script", "buildcol.pl");
|
---|
113 | system ("perl $buildcol $maxdocs $collection");
|
---|
114 | if (-e &util::filename_cat ($buildingdir, "text", "$collection.ldb") ||
|
---|
115 | -e &util::filename_cat ($buildingdir, "text", "$collection.bdb")) {
|
---|
116 | print STDERR "$collection collection built successfully\n\n";
|
---|
117 | } else {
|
---|
118 | die "\nbuildcol.pl failed\n";
|
---|
119 | }
|
---|
120 |
|
---|
121 | # replace old indexes with new ones
|
---|
122 | if (&has_content ($indexdir)) {
|
---|
123 | print STDERR "removing old indexes\n";
|
---|
124 | &util::rm_r ($indexdir);
|
---|
125 | }
|
---|
126 | rmdir ($indexdir) if -d $indexdir;
|
---|
127 | rename ($buildingdir, $indexdir);
|
---|
128 | }
|
---|
129 |
|
---|
130 | sub has_content {
|
---|
131 | my ($dir) = @_;
|
---|
132 |
|
---|
133 | if (!-d $dir) {return 0;}
|
---|
134 |
|
---|
135 | opendir (DIR, $dir) || return 0;
|
---|
136 | my @files = readdir DIR;
|
---|
137 | close DIR;
|
---|
138 |
|
---|
139 | foreach my $file (@files) {
|
---|
140 | if ($file !~ /^\.{1,2}$/) {
|
---|
141 | return 1;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return 0;
|
---|
145 | }
|
---|