Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / scripts / installation_check.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Long;
5 use Pod::Usage;
6 use Term::ANSIColor;
7 our $master_templates;
8 BEGIN {
9   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
10   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
11
12   # this is a default dir. may be wrong in your installation, change it then
13   $master_templates = './templates/print/';
14 }
15
16 use SL::InstallationCheck;
17
18 my %check;
19 Getopt::Long::Configure ("bundling");
20 GetOptions(
21   "v|verbose"   => \ my $v,
22   "a|all"       => \ $check{a},
23   "o|optional!" => \ $check{o},
24   "d|devel!"    => \ $check{d},
25   "l|latex!"    => \ $check{l},
26   "r|required!" => \ $check{r},
27   "h|help"      => sub { pod2usage(-verbose => 2) },
28   "c|color!"    => \ ( my $c = 1 ),
29 );
30
31 # if nothing is requested check "required"
32 $check{r} = 1 unless defined $check{a} ||
33                      defined $check{l} ||
34                      defined $check{o} ||
35                      defined $check{d};
36 my $default_run ='1' if $check{r};  # no parameter, therefore print a note after default run
37 if ($check{a}) {
38   foreach my $check (keys %check) {
39     $check{$check} = 1 unless defined $check{$check};
40   }
41 }
42
43
44 $| = 1;
45
46 if ($check{r}) {
47   print_header('Checking Required Modules');
48   check_module($_, required => 1) for @SL::InstallationCheck::required_modules;
49   print_header('Standard check for required modules done. See additional parameters for more checks (-- help)') if $default_run;
50 }
51 if ($check{o}) {
52   print_header('Checking Optional Modules');
53   check_module($_, optional => 1) for @SL::InstallationCheck::optional_modules;
54 }
55 if ($check{d}) {
56   print_header('Checking Developer Modules');
57   check_module($_, devel => 1) for @SL::InstallationCheck::developer_modules;
58 }
59 if ($check{l}) {
60   check_latex();
61 }
62
63 sub check_latex {
64   my ($res) = check_kpsewhich();
65   print_result("Looking for LaTeX kpsewhich", $res ? ('ok', 'green') : ('NOT ok', 'red'));
66   if ($res) {
67     check_template_dir($_) for SL::InstallationCheck::template_dirs($master_templates);
68   }
69 }
70
71 sub check_template_dir {
72   my ($dir) = @_;
73   my $path  = $master_templates . $dir;
74
75   print_header("Checking LaTeX Dependencies for Master Templates '$dir'");
76   kpsewhich($path, 'cls', $_) for SL::InstallationCheck::classes_from_latex($path, '\documentclass');
77   kpsewhich($path, 'sty', $_) for SL::InstallationCheck::classes_from_latex($path, '\usepackage');
78 }
79
80 our $mastertemplate_path = './templates/print/';
81
82 sub check_kpsewhich {
83   return 1 if SL::InstallationCheck::check_kpsewhich();
84
85   print STDERR <<EOL if $v;
86 +------------------------------------------------------------------------------+
87   Can't find kpsewhich, is there a proper installed LaTeX?
88   On Debian you may run "aptitude install texlive-base-bin"
89 +------------------------------------------------------------------------------+
90 EOL
91   return 0;
92 }
93
94 sub kpsewhich {
95   my ($dw, $type, $package) = @_;
96   $package =~ s/[^-_0-9A-Za-z]//g;
97   my $type_desc = $type eq 'cls' ? 'document class' : 'package';
98
99   eval { use String::ShellQuote; 1 } or warn "can't load String::ShellQuote" && return;
100      $dw         = shell_quote $dw;
101   my $e_package  = shell_quote $package;
102   my $e_type     = shell_quote $type;
103
104   my $exit = system(qq|TEXINPUTS=".:$dw:" kpsewhich $e_package.$e_type > /dev/null|);
105   my $res  = $exit > 0 ? 0 : 1;
106
107   print_result("Looking for LaTeX $type_desc $package", $res);
108   if (!$res) {
109     print STDERR <<EOL if $v;
110 +------------------------------------------------------------------------------+
111   LaTeX $type_desc $package could not be loaded.
112
113   On Debian you may find the needed *.deb package with:
114     apt-file search $package.$type
115
116   Maybe you need to install apt-file first by:
117     aptitude install apt-file && apt-file update
118 +------------------------------------------------------------------------------+
119 EOL
120   }
121 }
122
123 sub check_module {
124   my ($module, %role) = @_;
125
126   my $line = "Looking for $module->{fullname}";
127   my ($res, $ver) = SL::InstallationCheck::module_available($module->{"name"}, $module->{version});
128   if ($res) {
129     print_line($line, $ver || 'no version', 'green');
130   } else {
131     print_result($line, $res);
132   }
133
134
135   return if $res;
136
137   my $needed_text =
138       $role{optional} ? 'It is OPTIONAL for Lx-Office but RECOMMENDED for improved functionality.'
139     : $role{required} ? 'It is NEEDED by Lx-Office and must be installed.'
140     : $role{devel}    ? 'It is OPTIONAL for Lx-Office and only useful for developers.'
141     :                   'It is not listed as a dependancy yet. Please tell this the developers.';
142
143   my @source_texts = module_source_texts($module);
144   local $" = $/;
145   print STDERR <<EOL if $v;
146 +------------------------------------------------------------------------------+
147   $module->{fullname} could not be loaded.
148
149   This module is either too old or not available on your system.
150   $needed_text
151
152   Here are some ideas how to get it:
153
154 @source_texts
155 +------------------------------------------------------------------------------+
156 EOL
157 }
158
159 sub module_source_texts {
160   my ($module) = @_;
161   my @texts;
162   push @texts, <<EOL;
163   - You can get it from CPAN:
164       perl -MCPAN -e "install $module->{name}"
165 EOL
166   push @texts, <<EOL if $module->{url};
167   - You can download it from this URL and install it manually:
168       $module->{url}
169 EOL
170   push @texts, <<EOL if $module->{debian};
171   - On Debian, Ubuntu and other distros you can install it with apt-get:
172       sudo apt-get install $module->{debian}
173     Note: These may be out of date as well if your system is old.
174 EOL
175  # TODO: SuSE and Fedora packaging. Windows packaging.
176
177   return @texts;
178 }
179
180 sub mycolor {
181   return $_[0] unless $c;
182   return colored(@_);
183 }
184
185 sub print_result {
186   my ($test, $exit) = @_;
187   if ($exit) {
188     print_line($test, 'ok', 'green');
189   } else {
190     print_line($test, 'NOT ok', 'red');
191   }
192 }
193
194 sub print_line {
195   my ($text, $res, $color) = @_;
196   print $text, " ", ('.' x (78 - length($text) - length($res)));
197   print mycolor($res, $color);
198   print "\n";
199   return;
200 }
201
202 sub print_header {
203   print $/;
204   print "$_[0]:", $/;
205 }
206
207 1;
208
209 __END__
210
211 =encoding UTF-8
212
213 =head1 NAME
214
215 scripts/installation_check.pl - check Lx-Office dependancies
216
217 =head1 SYNOPSIS
218
219   scripts/installation_check.pl [OPTION]
220
221 =head1 DESCRIPTION
222
223 Check dependencys. List all perl modules needed by Lx-Office, probes for them,
224 and warns if one is not available.  List all LaTeX document classes and
225 packages needed by Lx-Office master templates, probes for them, and warns if
226 one is not available.
227
228
229 =head1 OPTIONS
230
231 =over 4
232
233 =item C<-a, --all>
234
235 Probe for all perl modules and all LaTeX master templates.
236
237 =item C<-c, --color>
238
239 Color output. Default on.
240
241 =item C<--no-color>
242
243 No color output. Helpful to avoid terminal escape problems.
244
245 =item C<-d, --devel>
246
247 Probe for perl developer dependancies. (Used for console  and tags file)
248
249 =item C<--no-devel>
250
251 Don't probe for perl developer dependancies. (Useful in combination with --all)
252
253 =item C<-h, --help>
254
255 Display this help.
256
257 =item C<-o, --optional>
258
259 Probe for optional modules.
260
261 =item C<--no-optional>
262
263 Don't probe for optional perl modules. (Useful in combination with --all)
264
265 =item C<-r, --required>
266
267 Probe for required perl modules (default).
268
269 =item C<--no-required>
270
271 Don't probe for required perl modules. (Useful in combination with --all)
272
273 =item C<-l. --latex>
274
275 Probe for LaTeX documentclasses and packages in master templates.
276
277 =item C<--no-latex>
278
279 Don't probe for LaTeX document classes and packages in master templates. (Useful in combination with --all)
280
281 =item C<-v. --verbose>
282
283 Print additional info for missing dependancies
284
285 =back
286
287 =head1 BUGS, CAVEATS and TODO
288
289 =over 4
290
291 =item *
292
293 Fedora packages not listed yet.
294
295 =item *
296
297 Not possible yet to generate a combined cpan/apt-get string to install all needed.
298
299 =item *
300
301 Not able to handle devel cpan modules yet.
302
303 =item *
304
305 Version requirements not fully tested yet.
306
307 =back
308
309 =head1 AUTHOR
310
311   Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
312   Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
313   Wulf Coulmann E<lt>wulf@coulmann.deE<gt>
314
315 =cut