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