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