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 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   my ($res, $ver) = SL::InstallationCheck::module_available($module->{"name"}, $module->{version});
159   if ($res) {
160     my $ver_string = ref $ver && $ver->can('numify') ? $ver->numify : $ver ? $ver : 'no version';
161     print_line($line, $ver_string, 'green');
162   } else {
163     print_result($line, $res);
164   }
165
166
167   return if $res;
168
169   my $needed_text =
170       $role{optional} ? 'It is OPTIONAL for kivitendo but RECOMMENDED for improved functionality.'
171     : $role{required} ? 'It is NEEDED by kivitendo and must be installed.'
172     : $role{devel}    ? 'It is OPTIONAL for kivitendo and only useful for developers.'
173     :                   'It is not listed as a dependancy yet. Please tell this the developers.';
174
175   my @source_texts = module_source_texts($module);
176   local $" = $/;
177   print STDERR <<EOL if $v;
178 +------------------------------------------------------------------------------+
179   $module->{fullname} could not be loaded.
180
181   This module is either too old or not available on your system.
182   $needed_text
183
184   Here are some ideas how to get it:
185
186 @source_texts
187 +------------------------------------------------------------------------------+
188 EOL
189 }
190
191 sub module_source_texts {
192   my ($module) = @_;
193   my @texts;
194   push @texts, <<EOL;
195   - You can get it from CPAN:
196       perl -MCPAN -e "install $module->{name}"
197 EOL
198   push @texts, <<EOL if $module->{url};
199   - You can download it from this URL and install it manually:
200       $module->{url}
201 EOL
202   push @texts, <<EOL if $module->{debian};
203   - On Debian, Ubuntu and other distros you can install it with apt-get:
204       sudo apt-get install $module->{debian}
205     Note: These may be out of date as well if your system is old.
206 EOL
207  # TODO: SuSE and Fedora packaging. Windows packaging.
208
209   return @texts;
210 }
211
212 sub mycolor {
213   return $_[0] unless $c;
214   return colored(@_);
215 }
216
217 sub print_result {
218   my ($test, $exit) = @_;
219   if ($exit) {
220     print_line($test, 'ok', 'green');
221   } else {
222     print_line($test, 'NOT ok', 'red');
223   }
224 }
225
226 sub print_line {
227   my ($text, $res, $color) = @_;
228   print $text, " ", ('.' x (78 - length($text) - length($res))), " ", mycolor($res, $color), $/;
229 }
230
231 sub print_header {
232   print $/;
233   print "$_[0]:", $/;
234 }
235
236 1;
237
238 __END__
239
240 =encoding UTF-8
241
242 =head1 NAME
243
244 scripts/installation_check.pl - check kivitendo dependancies
245
246 =head1 SYNOPSIS
247
248   scripts/installation_check.pl [OPTION]
249
250 =head1 DESCRIPTION
251
252 Check dependencys. List all perl modules needed by kivitendo, probes for them,
253 and warns if one is not available.  List all LaTeX document classes and
254 packages needed by kivitendo master templates, probes for them, and warns if
255 one is not available.
256
257
258 =head1 OPTIONS
259
260 =over 4
261
262 =item C<-a, --all>
263
264 Probe for all perl modules and all LaTeX master templates.
265
266 =item C<-c, --color>
267
268 Color output. Default on.
269
270 =item C<--no-color>
271
272 No color output. Helpful to avoid terminal escape problems.
273
274 =item C<-d, --devel>
275
276 Probe for perl developer dependancies. (Used for console  and tags file)
277
278 =item C<--no-devel>
279
280 Don't probe for perl developer dependancies. (Useful in combination with --all)
281
282 =item C<-h, --help>
283
284 Display this help.
285
286 =item C<-o, --optional>
287
288 Probe for optional modules.
289
290 =item C<--no-optional>
291
292 Don't probe for optional perl modules. (Useful in combination with --all)
293
294 =item C<-r, --required>
295
296 Probe for required perl modules (default).
297
298 =item C<--no-required>
299
300 Don't probe for required perl modules. (Useful in combination with --all)
301
302 =item C<-l. --latex>
303
304 Probe for LaTeX documentclasses and packages in master templates.
305
306 =item C<--no-latex>
307
308 Don't probe for LaTeX document classes and packages in master templates. (Useful in combination with --all)
309
310 =item C<-v. --verbose>
311
312 Print additional info for missing dependancies
313
314 =back
315
316 =head1 BUGS, CAVEATS and TODO
317
318 =over 4
319
320 =item *
321
322 Fedora packages not listed yet.
323
324 =item *
325
326 Not possible yet to generate a combined cpan/apt-get string to install all needed.
327
328 =item *
329
330 Not able to handle devel cpan modules yet.
331
332 =item *
333
334 Version requirements not fully tested yet.
335
336 =back
337
338 =head1 AUTHOR
339
340   Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
341   Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
342   Wulf Coulmann E<lt>wulf@coulmann.deE<gt>
343
344 =cut