1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | ###########################################################################
|
---|
4 | #
|
---|
5 | # downloadinfo.pl -- prints out information about a download module
|
---|
6 | # A component of the Greenstone digital library software
|
---|
7 | # from the New Zealand Digital Library Project at the
|
---|
8 | # University of Waikato, New Zealand.
|
---|
9 | #
|
---|
10 | # Copyright (C) 2006 New Zealand Digital Library Project
|
---|
11 | #
|
---|
12 | # This program is free software; you can redistribute it and/or modify
|
---|
13 | # it under the terms of the GNU General Public License as published by
|
---|
14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
15 | # (at your option) any later version.
|
---|
16 | #
|
---|
17 | # This program is distributed in the hope that it will be useful,
|
---|
18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | # GNU General Public License for more details.
|
---|
21 | #
|
---|
22 | # You should have received a copy of the GNU General Public License
|
---|
23 | # along with this program; if not, write to the Free Software
|
---|
24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | #
|
---|
26 | ###########################################################################
|
---|
27 |
|
---|
28 |
|
---|
29 | use strict;
|
---|
30 | no strict 'subs'; # allow barewords (eg STDERR) as function arguments
|
---|
31 |
|
---|
32 | BEGIN {
|
---|
33 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
34 | die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
|
---|
35 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
36 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
|
---|
37 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/downloaders");
|
---|
38 | }
|
---|
39 |
|
---|
40 | use download;
|
---|
41 | use util;
|
---|
42 | use gsprintf;
|
---|
43 | use printusage;
|
---|
44 | use parse2;
|
---|
45 |
|
---|
46 | my $arguments =
|
---|
47 | [ { 'name' => "collection",
|
---|
48 | 'desc' => "{downloadinfo.collection}",
|
---|
49 | 'type' => "string",
|
---|
50 | 'reqd' => "no" },
|
---|
51 | { 'name' => "xml",
|
---|
52 | 'desc' => "{scripts.xml}",
|
---|
53 | 'type' => "flag",
|
---|
54 | 'reqd' => "no"},
|
---|
55 | { 'name' => "listall",
|
---|
56 | 'desc' => "{scripts.listall}",
|
---|
57 | 'type' => "flag",
|
---|
58 | 'reqd' => "no" },
|
---|
59 | { 'name' => "describeall",
|
---|
60 | 'desc' => "{scripts.describeall}",
|
---|
61 | 'type' => "flag",
|
---|
62 | 'reqd' => "no" },
|
---|
63 | { 'name' => "language",
|
---|
64 | 'desc' => "{scripts.language}",
|
---|
65 | 'type' => "string",
|
---|
66 | 'reqd' => "no" } ];
|
---|
67 |
|
---|
68 | my $options = { 'name' => "downloadinfo.pl",
|
---|
69 | 'desc' => "{downloadinfo.desc}",
|
---|
70 | 'args' => $arguments };
|
---|
71 |
|
---|
72 | sub gsprintf
|
---|
73 | {
|
---|
74 | return &gsprintf::gsprintf(@_);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | sub main {
|
---|
79 |
|
---|
80 | my $collection = "";
|
---|
81 | my $xml = 0;
|
---|
82 | my $listall = 0;
|
---|
83 | my $describeall = 0;
|
---|
84 | my ($language, $encoding);
|
---|
85 | my $hashParsingResult = {};
|
---|
86 | my $unparsed_args = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
|
---|
87 |
|
---|
88 | # parse returns -1 on error
|
---|
89 | if ($unparsed_args == -1) {
|
---|
90 | PrintUsage::print_txt_usage($options, "{downloadinfo.params}");
|
---|
91 | die "\n";
|
---|
92 | }
|
---|
93 |
|
---|
94 | foreach my $strVariable (keys %$hashParsingResult)
|
---|
95 | {
|
---|
96 | my $value = $hashParsingResult->{$strVariable};
|
---|
97 | # test to make sure the variable name is 'safe'
|
---|
98 | if ($strVariable !~ /^\w+$/) {
|
---|
99 | die "variable name '$strVariable' isn't safe!";
|
---|
100 | }
|
---|
101 | eval "\$$strVariable = \$value";
|
---|
102 | }
|
---|
103 |
|
---|
104 | # if language wasn't specified, see if it is set in the environment
|
---|
105 | # (LC_ALL or LANG)
|
---|
106 | if (!$language && ($_=$ENV{'LC_ALL'} or $_=$ENV{'LANG'})) {
|
---|
107 | m/^([^\.]+)\.?(.*)/;
|
---|
108 | $language=$1;
|
---|
109 | $encoding=$2; # might be undef...
|
---|
110 | # gsprintf::load_language* thinks "fr" is completely different to "fr_FR"...
|
---|
111 | $language =~ s/_.*$//;
|
---|
112 | }
|
---|
113 |
|
---|
114 | # If $language has been set, load the appropriate resource bundle
|
---|
115 | # (Otherwise, the default resource bundle will be loaded automatically)
|
---|
116 | if ($language) {
|
---|
117 | gsprintf::load_language_specific_resource_bundle($language);
|
---|
118 | if ($encoding) {
|
---|
119 | $encoding =~ tr/-/_/;
|
---|
120 | $encoding = lc($encoding);
|
---|
121 | $encoding =~ s/utf_8/utf8/; # special
|
---|
122 | $gsprintf::specialoutputencoding=$encoding;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | # If there is not exactly one argument left (download name), then the arguments were wrong
|
---|
127 | # Or if the user specified -h, then we output the usage also
|
---|
128 |
|
---|
129 | if((@ARGV && $ARGV[0] =~ /^\-+h/) )
|
---|
130 | {
|
---|
131 | PrintUsage::print_txt_usage($options, "{downloadinfo.params}");
|
---|
132 | die "\n";
|
---|
133 | }
|
---|
134 |
|
---|
135 | # If there is not exactly 1 argument left (download name), then the arguments were wrong (apart from if we had listall or describeall set)
|
---|
136 | if ($listall == 0 && $describeall ==0 && $unparsed_args == 0) {
|
---|
137 | gsprintf(STDERR, "{downloadinfo.no_download_name}\n\n");
|
---|
138 | PrintUsage::print_txt_usage($options, "{downloadinfo.params}", 1);
|
---|
139 | die "\n";
|
---|
140 | }
|
---|
141 |
|
---|
142 | # we had some arguments that we weren't expecting
|
---|
143 | if ($unparsed_args > 1) {
|
---|
144 | pop(@ARGV); # assume that the last arg is the download name
|
---|
145 | gsprintf(STDERR, "{common.invalid_options}\n\n", join (',', @ARGV));
|
---|
146 | PrintUsage::print_txt_usage($options, "{downloadinfo.params}", 1);
|
---|
147 | die "\n";
|
---|
148 | }
|
---|
149 | my $download_name = shift (@ARGV);
|
---|
150 | if (defined $download_name) {
|
---|
151 | $download_name =~ s/\.pm$//; # allow xxx.pm as the argument
|
---|
152 | }
|
---|
153 |
|
---|
154 | if ($collection ne "") {
|
---|
155 | $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
|
---|
156 | } else {
|
---|
157 | $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
|
---|
158 | }
|
---|
159 |
|
---|
160 | if ($listall || $describeall) {
|
---|
161 | my $downloaders_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "downloaders");
|
---|
162 | my @downloader_list = ();
|
---|
163 | if (opendir (INDIR, $downloaders_dir)) {
|
---|
164 | @downloader_list = grep (/Download\.pm$/, readdir (INDIR));
|
---|
165 | closedir (INDIR);
|
---|
166 | }
|
---|
167 |
|
---|
168 | print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
---|
169 | print STDERR "<DownloadList length=\"" . scalar(@downloader_list) . "\">\n";
|
---|
170 | foreach my $downloader (@downloader_list) {
|
---|
171 | $downloader =~ s/\.pm$//;
|
---|
172 | my $downloaderobj = &download::load_download ($downloader);
|
---|
173 | if ($describeall) {
|
---|
174 | $downloaderobj->print_xml_usage(0);
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | $downloaderobj->print_xml_usage(0, 1);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | print STDERR "</DownloadList>\n";
|
---|
181 | }
|
---|
182 | else {
|
---|
183 | &print_single_download($download_name, $xml, 1);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | sub print_single_download {
|
---|
189 | my ($download, $xml, $header) = @_;
|
---|
190 | my @options = ("-gsdlinfo");
|
---|
191 | my $downloadobj = &download::load_download ($download, \@options );
|
---|
192 | if ($xml) {
|
---|
193 | $downloadobj->print_xml_usage($header);
|
---|
194 | }
|
---|
195 | else {
|
---|
196 | gsprintf(STDERR, "{downloadinfo.option_types}:\n\n");
|
---|
197 | gsprintf(STDERR, "{downloadinfo.specific_options}\n\n");
|
---|
198 | gsprintf(STDERR, "{downloadinfo.general_options}\n\n");
|
---|
199 | gsprintf(STDERR, "$download {common.info}:\n\n");
|
---|
200 |
|
---|
201 | $downloadobj->print_txt_usage();
|
---|
202 | }
|
---|
203 |
|
---|
204 | }
|
---|
205 |
|
---|
206 | &main ();
|
---|