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