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