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