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' => "collect",
|
---|
48 | 'desc' => "{downloadinfo.collect}",
|
---|
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 $collect = "";
|
---|
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 | foreach my $strVariable (keys %$hashParsingResult)
|
---|
89 | {
|
---|
90 | my $value = $hashParsingResult->{$strVariable};
|
---|
91 | # test to make sure the variable name is 'safe'
|
---|
92 | if ($strVariable !~ /^\w+$/) {
|
---|
93 | die "variable name '$strVariable' isn't safe!";
|
---|
94 | }
|
---|
95 | eval "\$$strVariable = \$value";
|
---|
96 | }
|
---|
97 |
|
---|
98 | # if language wasn't specified, see if it is set in the environment
|
---|
99 | # (LC_ALL or LANG)
|
---|
100 | if (!$language && ($_=$ENV{'LC_ALL'} or $_=$ENV{'LANG'})) {
|
---|
101 | m/^([^\.]+)\.?(.*)/;
|
---|
102 | $language=$1;
|
---|
103 | $encoding=$2; # might be undef...
|
---|
104 | # gsprintf::load_language* thinks "fr" is completely different to "fr_FR"...
|
---|
105 | $language =~ s/_.*$//;
|
---|
106 | }
|
---|
107 |
|
---|
108 | # If $language has been set, load the appropriate resource bundle
|
---|
109 | # (Otherwise, the default resource bundle will be loaded automatically)
|
---|
110 | if ($language) {
|
---|
111 | gsprintf::load_language_specific_resource_bundle($language);
|
---|
112 | if ($encoding) {
|
---|
113 | $encoding =~ tr/-/_/;
|
---|
114 | $encoding = lc($encoding);
|
---|
115 | $encoding =~ s/utf_8/utf8/; # special
|
---|
116 | $gsprintf::specialoutputencoding=$encoding;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | # If there are more than one argument left after parsing, it mean user input too many arguments.
|
---|
121 | # Error occoured will return 0
|
---|
122 | if( $unparsed_args > 1 or (@ARGV && $ARGV[0] =~ /^\-+h/) )
|
---|
123 | {
|
---|
124 | PrintUsage::print_txt_usage($options, "{pluginfo.params}");
|
---|
125 | die "\n";
|
---|
126 | }
|
---|
127 |
|
---|
128 | my $download_name = shift (@ARGV);
|
---|
129 | if (defined $download_name) {
|
---|
130 | $download_name =~ s/\.pm$//; # allow xxx.pm as the argument
|
---|
131 | }
|
---|
132 | if (($listall == 0 && $describeall ==0) && (!defined $download_name || $download_name eq "")) {
|
---|
133 | gsprintf(STDERR, "{downloadinfo.no_download_name}\n\n");
|
---|
134 | PrintUsage::print_txt_usage($options, "{downloadinfo.params}", 1);
|
---|
135 | die "\n";
|
---|
136 | }
|
---|
137 |
|
---|
138 | if ($collect ne "") {
|
---|
139 | $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collect);
|
---|
140 | } else {
|
---|
141 | $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
|
---|
142 | }
|
---|
143 |
|
---|
144 | if ($listall) {
|
---|
145 | my $download_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib","downloaders");
|
---|
146 |
|
---|
147 | if (!opendir (INDIR, $download_dir)) {
|
---|
148 | print STDERR "downloadinfo.pl: could not open directory $download_dir\n";
|
---|
149 | }
|
---|
150 | else {
|
---|
151 | my @download_list = grep (/Download\.pm$/, readdir (INDIR));
|
---|
152 | closedir (INDIR);
|
---|
153 |
|
---|
154 | if ($xml) {
|
---|
155 | my $intNumDownloads = scalar(@download_list);
|
---|
156 |
|
---|
157 | print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
---|
158 | print STDERR "<!DOCTYPE DownloadList [\n";
|
---|
159 | print STDERR " <!ELEMENT DownloadList (DownloadName*)>\n";
|
---|
160 | print STDERR " <!ELEMENT DownloadName (#PCDATA)>\n";
|
---|
161 | print STDERR " <!ATTLIST DownloadList\n";
|
---|
162 | print STDERR " length CDATA #REQUIRED>\n";
|
---|
163 | print STDERR "]>\n";
|
---|
164 |
|
---|
165 | print STDERR "<DownloadList length=\"$intNumDownloads\">\n";
|
---|
166 | map { print STDERR " <DownloadName>$_</DownloadName>\n"; } @download_list;
|
---|
167 | print STDERR "</DownloadList>\n";
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | print STDERR join(" ",@download_list), "\n";
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | elsif ($describeall) {
|
---|
176 | my $download_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib","downloaders");
|
---|
177 | my @download_list;
|
---|
178 | if (!opendir (INDIR, $download_dir)) {
|
---|
179 | print STDERR "downloadinfo.pl: could not open directory $download_dir\n";
|
---|
180 | } else {
|
---|
181 | @download_list = grep (/Download\.pm$/, readdir (INDIR));
|
---|
182 | closedir (INDIR);
|
---|
183 | }
|
---|
184 | #sort (@download_list);
|
---|
185 | if ($xml) {
|
---|
186 | my $num_download = scalar(@download_list);
|
---|
187 | &PrintUsage::print_xml_header("download", 1);
|
---|
188 | print STDERR "<DownloadInfoList length=\"$num_download\">\n";
|
---|
189 | }
|
---|
190 | foreach my $pl (@download_list) {
|
---|
191 | $pl =~ s/\.pm$//;
|
---|
192 | &print_single_download($pl, $xml, 0);
|
---|
193 | }
|
---|
194 | if ($xml) {
|
---|
195 | print STDERR "</DownloadInfoList>\n";
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | else {
|
---|
200 | &print_single_download($download_name, $xml, 1);
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | sub print_single_download {
|
---|
205 | my ($download, $xml, $header) = @_;
|
---|
206 | my @options = ("-gsdlinfo");
|
---|
207 | my $downloadobj = &download::load_download ($download, \@options );
|
---|
208 | if ($xml) {
|
---|
209 | $downloadobj->print_xml_usage($header);
|
---|
210 | }
|
---|
211 | else {
|
---|
212 | gsprintf(STDERR, "{downloadinfo.option_types}:\n\n");
|
---|
213 | gsprintf(STDERR, "{downloadinfo.specific_options}\n\n");
|
---|
214 | gsprintf(STDERR, "{downloadinfo.general_options}\n\n");
|
---|
215 | gsprintf(STDERR, "$download {common.info}:\n\n");
|
---|
216 |
|
---|
217 | $downloadobj->print_txt_usage();
|
---|
218 | }
|
---|
219 |
|
---|
220 | }
|
---|
221 |
|
---|
222 | &main ();
|
---|