Bug 20982: Sanitize category to prevent XSS on opac-shelves.pl
[koha.git] / Makefile.PL
1 # Copyright 2007 MJ Ray
2 # Copyright 2016 PTFS Europe
3 #
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 #
19 # Current maintainer MJR http://mjr.towers.org.uk/
20 #
21
22 use strict;
23 use warnings;
24 use ExtUtils::MakeMaker;
25 use POSIX;
26 use File::Spec;
27 use Getopt::Long qw/HelpMessage/;
28 use FindBin; # we need to enforce which C4::Installer::PerlModule is used in case more than one is installed
29
30 use lib $FindBin::Bin;
31
32 use C4::Installer::PerlModules;
33
34 my $koha_pm = C4::Installer::PerlModules->new;
35
36 my $DEBUG = 0;
37 die "perl 5.10 or later required" unless ($] >= 5.010000);
38
39 # Hash up directory structure & files beginning with the directory we were called from (should be the base of koha)...
40
41 my $dirtree = hashdir('.');
42 my %result = ();
43
44 =head1 NAME
45
46 Makefile.PL - Koha packager and installer
47
48 =head1 SYNOPSIS
49
50 =head2 BASIC INSTALLATION
51
52     perl Makefile.PL
53     make
54     make test
55     sudo make install
56
57 =head2 UPGRADE INSTALLATION
58
59     NOTE: This option is only available if koha-install-log exists.
60
61     perl Makefile.PL --prev-install-log /path/to/koha-install-log
62
63     make
64     make test
65     sudo make upgrade
66
67 =head2 PACKAGING RELEASE TARBALLS
68
69     make manifest tardist
70     make manifest zipdist
71
72 =head2 CLEANING UP
73
74         make clean
75
76 =head2 CLI PARAMETERS
77
78     --prev-install-log           Read configuration from previous installation
79     --install_mode               Installation mode (dev, standard, single)
80     --db_type                    Database (mysql, Pg)
81     --db_host                    Database host (e.g. localhost)
82     --db_port                    Database port (e.g. 3306)
83     --db_name                    Database name (e.g. koha)
84     --db_user                    Database user (e.g. kohaadmin)
85     --db_pass                    Database password (e.g. katikoan)
86     --zebra_marc_format          Zebra MARC format (marc21, normarc, unimarc)
87     --zebra_language             Zebra language (e.g. en)
88     --zebra_tokenizer            Zebra tokenizer (chr, icu)
89     --zebra_user                 Zebra user (e.g. kohauser)
90     --zebra_pass                 Zebra password (e.g. zebrastripes)
91     --zebra_sru_host             Zebra SRU servername (e.g. localhost)
92     --zebra_sru_biblios_port     Zebra SRU biblios port (e.g. 9998)
93     --zebra_sru_authorities_port Zebra SRU biblios port (e.g. 9999)
94     --koha_user                  Koha Unix user (e.g. koha)
95     --koha_group                 Koha Unix group (e.g. koha)
96     --install_sru                Install the SRU server (yes, no)
97     --install_pazpar2            Install PazPar2 (yes, no)
98     --use_memcached              Use Memcached (yes, no)
99     --font_dir                   Location of fonts (e.g. /usr/share/fonts/truetype/dejavu)
100     --run_database_tests         Run database dependent tests (yes, no)
101     --install_base               Base directory of installation (e.g. /usr/share/koha)
102     --template-cache-dir         Specify a template cache directory (e.g. /var/cache/koha)
103     --help                       Display this help message
104
105 =head1 DESCRIPTION
106
107 This is a packager and installer that uses
108 ExtUtils::MakeMaker, which is fairly common
109 on perl systems.
110 As well as building tar or zip files
111 and installing with the above commands,
112 it allows us to check pre-requisites
113 and generate configuration files.
114
115 =head1 VARIABLES
116
117 =head2 NAME, VERSION_FROM, ABSTRACT, AUTHOR
118
119 Basic metadata about this software.
120
121 =head2 NO_META
122
123 Suppress generation of META.yml file.
124
125 =head2 PREREQ_PM
126
127 Hash of perl modules and versions required.
128
129 =head2 PM
130
131 Hash of file mappings
132
133 =head2 PL_FILES
134
135 This is a hash of PL scripts to run after installation and
136 the files to ask them to generate.
137 Maybe use the values from CONFIGURE
138 to generate initial configuration files in future.
139
140 =cut
141
142 =head2 target_map
143
144 This is a hash mapping directories and files in the
145 source tree to installation target directories.  The rules
146 for this mapping are:
147
148 =over 4
149
150 =item If a directory or file is specified, it and its
151 contents will be copied to the installation target directory.
152
153 =item If a subdirectory of a mapped directory is specified,
154 its target overrides the parent's target for that subdirectory.
155
156 =item The value of each map entry may either be a scalar containing
157 one target or a reference to a hash containing 'target' and 'trimdir'
158 keys.
159
160 =item Any files at the top level of the source tree that are
161 not included in the map will not be installed.
162
163 =item Any directories at the top level of the source tree
164 that are not included in the map will be installed in
165 INTRANET_CGI_DIR.  This is a sensible default given the
166 current organization of the source tree, but (FIXME) it
167 would be better to reorganize the source tree to better
168 match the installation system, to allow adding new directories
169 without having to adjust Makefile.PL each time.  The idea
170 is to make the C<$target_map> hash as minimal as possible.
171
172 =back
173
174 The permitted installation targets are:
175
176 =over 4
177
178 =item INTRANET_CGI_DIR
179
180 CGI scripts for intranet (staff) interface.
181
182 =item INTRANET_TMPL_DIR
183
184 HTML templates for the intranet interface.
185
186 =item INTRANET_WWW_DIR
187
188 HTML files, images, etc. for DocumentRoot for the intranet interface.
189
190 =item OPAC_CGI_DIR
191
192 CGI scripts for OPAC (public) interface.
193
194 =item OPAC_TMPL_DIR
195
196 HTML templates for the OPAC interface.
197
198 =item OPAC_WWW_DIR
199
200 HTML files, images, etc. for DocumentRoot for the OPAC interface.
201
202 =item PERL_MODULE_DIR
203
204 Perl modules (at present just the C4 modules) that are intimately
205 tied to Koha.  Depending on the installation options, these
206 may or may not be installed one of the standard directories
207 in Perl's default @LIB.
208
209 =item KOHA_CONF_DIR
210
211 Directory for Koha configuration files.
212
213 =item ZEBRA_CONF_DIR
214
215 Directory for Zebra configuration files.
216
217 =item ZEBRA_LOCK_DIR
218
219 Directory for Zebra's lock files.  This includes subdirs for authorities,
220 biblios, and the zebra rebuild function.  Any activity to reindex
221 zebra from koha should interlock here with rebuild_zebra.pl.
222
223 =item ZEBRA_DATA_DIR
224
225 Directory for Zebra's data files.
226
227 =item ZEBRA_RUN_DIR
228
229 Directory for Zebra's UNIX-domain sockets.
230
231 =item ELASTICSEARCH_SERVERS
232
233 Server:port of the Elasticsearch server to use, as a comma separated list:
234 eg. 192.168.0.100:9200, localhost:9200
235
236 =item ELASTICSEARCH_INDEX
237
238 Unique index in Elasticsearch, for this Koha-instance
239 eg. koha-mykoha  or just  koha
240
241 =item MISC_DIR
242
243 Directory for for miscellaenous scripts, among other
244 things the translation toolkit and RSS feed tools.
245
246 =item SCRIPT_DIR
247
248 Directory for command-line scripts and daemons to
249 be set up all for installation modes.
250
251 =item SCRIPT_NONDEV_DIR
252
253 Directory for command-line scripts that should
254 be installed in the same directory as the
255 SCRIPT_DIR target except 'dev' installs.
256
257 =item MAN_DIR
258
259 Directory for man pages created from POD -- will mostly
260 contain information of interest to Koha developers.
261
262 =item DOC_DIR
263
264 Directory for Koha documentation accessed from the
265 command-line, e.g., READMEs.
266
267 =item LOCK_DIR
268
269 Directory for Koha scripts to put their locks.
270
271 =item LOG_DIR
272
273 Directory for Apache and Zebra logs produced by Koha.
274
275 =item BACKUP_DIR
276
277 Directory for backup files produced by Koha.
278
279 =item PLUGINS_DIR
280
281 Directory for external Koha plugins.
282
283 =item PAZPAR2_CONF_DIR
284
285 Directory for PazPar2 configuration files.
286
287 =item FONT_DIR
288
289 Directory where DejaVu fonts are installed.
290
291 =item NONE
292
293 This is a dummy target used to explicitly state
294 that a given file or directory is not to be installed.
295 This is used either for parts of the installer itself
296 or for development tools that are not applicable to a
297 production installation.
298
299 =back
300
301 =cut
302
303 my $target_map = {
304   './about.pl'                  => 'INTRANET_CGI_DIR',
305   './acqui'                     => 'INTRANET_CGI_DIR',
306   './admin'                     => 'INTRANET_CGI_DIR',
307   './api'                       => { target => 'API_CGI_DIR', trimdir => -1 },
308   './app.psgi'                  => 'SCRIPT_DIR',
309   './authorities'               => 'INTRANET_CGI_DIR',
310   './basket'                    => 'INTRANET_CGI_DIR',
311   './bin'                       => { target => 'SCRIPT_DIR', trimdir => -1 },
312   './C4'                        => 'PERL_MODULE_DIR',
313   './C4/SIP/t'                  => 'NONE',
314   './C4/SIP/koha_test'          => 'NONE',
315   './C4/tests'                  => 'NONE',
316   './catalogue'                 => 'INTRANET_CGI_DIR',
317   './cataloguing'               => 'INTRANET_CGI_DIR',
318   './changelanguage.pl'         => 'INTRANET_CGI_DIR',
319   './check_sysprefs.pl'         => 'NONE',
320   './circ'                      => 'INTRANET_CGI_DIR',
321   './clubs'                     => 'INTRANET_CGI_DIR',
322   './course_reserves'           => 'INTRANET_CGI_DIR',
323   './cpanfile'                  => 'PERL_MODULE_DIR',
324   './docs/history.txt'          => { target => 'DOC_DIR', trimdir => -1 },
325   './docs/contributors.yaml'    => { target => 'DOC_DIR', trimdir => -1 },
326   './docs/teams.yaml'           => { target => 'DOC_DIR', trimdir => -1 },
327   './offline_circ'              => 'INTRANET_CGI_DIR',
328   './etc'                       => { target => 'KOHA_CONF_DIR', trimdir => -1 },
329   './etc/zebradb'               => { target => 'ZEBRA_CONF_DIR', trimdir => -1 },
330   './etc/z3950'                 => { target => 'Z3950_CONF_DIR', trimdir => -1 },
331   './etc/pazpar2'               => { target => 'PAZPAR2_CONF_DIR', trimdir => -1 },
332   './help.pl'                   => 'INTRANET_CGI_DIR',
333   './ill'                       => 'INTRANET_CGI_DIR',
334   './installer-CPAN.pl'         => 'NONE',
335   './installer'                 => 'INTRANET_CGI_DIR',
336   './errors'                    => {target => 'INTRANET_CGI_DIR'},
337   './Koha'                      => 'PERL_MODULE_DIR',
338   './Koha.pm'                   => 'PERL_MODULE_DIR',
339   './koha-tmpl/intranet-tmpl'   => {target => 'INTRANET_TMPL_DIR', trimdir => -1},
340   './koha-tmpl/opac-tmpl'       => {target => 'OPAC_TMPL_DIR', trimdir => -1},
341   './kohaversion.pl'            => 'INTRANET_CGI_DIR',
342   './labels'                    => 'INTRANET_CGI_DIR',
343   './mainpage.pl'               => 'INTRANET_CGI_DIR',
344   './Makefile.PL'               => 'NONE',
345   './MANIFEST.SKIP'             => 'NONE',
346   './members'                   => 'INTRANET_CGI_DIR',
347   './misc'                      => { target => 'SCRIPT_NONDEV_DIR', trimdir => -1 },
348   './misc/bin'                  => { target => 'SCRIPT_DIR', trimdir => -1 },
349   './misc/release_notes'        => { target => 'DOC_DIR', trimdir => 2 },
350   './misc/translator'           => { target => 'MISC_DIR', trimdir => 2 },
351   './misc/koha-install-log'     => { target => 'MISC_DIR', trimdir => -1 },
352   './misc/installer_devel_notes' => 'NONE',
353   './opac'                      => 'OPAC_CGI_DIR',
354   './OpenILS'                   => 'PERL_MODULE_DIR',
355   './README.txt'                => 'NONE',
356   './patroncards'               => 'INTRANET_CGI_DIR',
357   './patron_lists'              => 'INTRANET_CGI_DIR',
358   './plugins'                   => 'INTRANET_CGI_DIR',
359   './pos'                       => 'INTRANET_CGI_DIR',
360   './reports'                   => 'INTRANET_CGI_DIR',
361   './reserve'                   => 'INTRANET_CGI_DIR',
362   './reviews'                   => 'INTRANET_CGI_DIR',
363   './rewrite-config.PL'         => 'NONE',
364   './reviews'                   => 'INTRANET_CGI_DIR',
365   './rotating_collections'      => 'INTRANET_CGI_DIR',
366   './serials'                   => 'INTRANET_CGI_DIR',
367   './services'                  => 'INTRANET_CGI_DIR',
368   './skel'                      => 'NONE',
369   './skel/var/lock/koha'        => { target => 'LOCK_DIR', trimdir => -1 },
370   './skel/var/log/koha'         => { target => 'LOG_DIR', trimdir => -1 },
371   './skel/var/spool/koha'       => { target => 'BACKUP_DIR', trimdir => -1 },
372   './skel/var/run/koha/zebradb' => { target => 'ZEBRA_RUN_DIR', trimdir => -1 },
373   './skel/var/lock/koha/zebradb/authorities' => { target => 'ZEBRA_LOCK_DIR', trimdir => 6 },
374   './skel/var/lib/koha/zebradb/authorities/key'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
375   './skel/var/lib/koha/zebradb/authorities/register'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
376   './skel/var/lib/koha/zebradb/authorities/shadow'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
377   './skel/var/lib/koha/zebradb/authorities/tmp'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
378   './skel/var/lock/koha/zebradb/biblios' => { target => 'ZEBRA_LOCK_DIR', trimdir => 6 },
379   './skel/var/lib/koha/zebradb/biblios/key'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
380   './skel/var/lib/koha/zebradb/biblios/register'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
381   './skel/var/lib/koha/zebradb/biblios/shadow'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
382   './skel/var/lib/koha/zebradb/biblios/tmp'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
383   './skel/var/lock/koha/zebradb/rebuild' => { target => 'ZEBRA_LOCK_DIR', trimdir => 6 },
384   './skel/var/lib/koha/plugins' => { target => 'PLUGINS_DIR', trimdir => 6 },
385   './sms'                       => 'INTRANET_CGI_DIR',
386   './suggestion'                => 'INTRANET_CGI_DIR',
387   './svc'                       => 'INTRANET_CGI_DIR',
388   './t'                         => 'NONE',
389   './xt'                        => 'NONE',
390   './tags'                      => 'INTRANET_CGI_DIR',
391   './tmp'                       => 'NONE', # FIXME need to determine whether
392                                            # Koha generates any persistent temp files
393                                            # that should go in /var/tmp/koha
394   './tools'                     => 'INTRANET_CGI_DIR',
395   './virtualshelves'            => 'INTRANET_CGI_DIR',
396   # ignore files and directories created by the install itself
397   './pm_to_blib'                => 'NONE',
398   './blib'                      => 'NONE',
399   '.sass-lint.yml'              => 'NONE',
400   '.scss-lint.yml'              => 'NONE',
401   '.eslintrc.json'              => 'NONE',
402   'gulpfile.js'                 => 'NONE',
403   'package.json'                => 'NONE',
404   'yarn.lock'                   => 'NONE',
405 };
406
407 =head1 CONFIGURATION OPTIONS
408
409 The following configuration options are used by the installer.
410
411 =over 4
412
413 =item INSTALL_MODE
414
415 Specifies whether installation will be FHS-compliant (default,
416 assumes user has root), put everything under
417 a single directory (for users installing on a web host
418 that allows CGI scripts and a MySQL database but not root
419 access), or development (for a developer who wants to run
420 Koha from a git clone with no fuss).
421
422 =item INSTALL_BASE
423
424 Directory under which most components will go.  Default
425 value will vary depending on INSTALL_MODE.
426
427 =item DB_TYPE
428
429 Type of DBMS (e.g., mysql or Pg).
430
431 =item DB_HOST
432
433 Name of DBMS server.
434
435 =item DB_PORT
436
437 Port that DBMS server is listening on.
438
439 =item DB_NAME
440
441 Name of the DBMS database for Koha.
442
443 =item DB_USER
444
445 Name of DBMS user account for Koha's database.
446
447 =item DB_PASS
448
449 Pasword of DMBS user account for Koha's database.
450
451 =item ZEBRA_MARC_FORMAT
452
453 Specifies format of MARC records to be indexed by Zebra.
454
455 =item ZEBRA_LANGUAGE
456
457 Specifies primary language of records that will be
458 indexed by Zebra.
459
460 =item ZEBRA_USER
461
462 Internal Zebra user account for the index.
463
464 =item ZEBRA_PASS
465
466 Internal Zebra user account's password.
467
468 =item KOHA_USER
469
470 System user account that will own Koha's files.
471
472 =item KOHA_GROUP
473
474 System group that will own Koha's files.
475
476 =item SMTP_HOST
477
478 SMTP server host name (default: localhost)
479
480 =item SMTP_PORT
481
482 SMTP server port (default: 25)
483
484 =item SMTP_TIMEOUT
485
486 Connection timeour in seconds (default: 120)
487
488 =item SMTP_SSL_MODE
489
490 SSL mode. Options are 'disabled' (default), 'ssl' and 'starttls'
491
492 =item SMTP_USER_NAME
493
494 SMTP authentication user name
495
496 =item SMTP_PASSWORD
497
498 SMTP authentication password
499
500 =item SMTP_DEBUG
501
502 Enable debug mode for SMTP (default: no)
503
504 =back
505
506 =cut
507
508 # default configuration options
509 my %config_defaults = (
510   'DB_TYPE'           => 'mysql',
511   'DB_HOST'           => 'localhost',
512   'DB_NAME'           => 'koha',
513   'DB_USER'           => 'kohaadmin',
514   'DB_PASS'           => 'katikoan',
515   'DB_USE_TLS'        => 'no',
516   'DB_TLS_CA_CERTIFICATE'  => '/etc/mysql-ssl/server-ca.pem',
517   'DB_TLS_CLIENT_CERTIFICATE'  => '/etc/mysql-ssl/client-cert.pem',
518   'DB_TLS_CLIENT_KEY' => '/etc/mysql-ssl/client-key.pem',
519   'INSTALL_SRU'       => 'yes',
520   'INSTALL_PAZPAR2'   => 'no',
521   'ZEBRA_MARC_FORMAT' => 'marc21',
522   'ZEBRA_LANGUAGE'    => 'en',
523   'ZEBRA_TOKENIZER'   => 'chr',
524   'ZEBRA_USER'        => 'kohauser',
525   'ZEBRA_PASS'        => 'zebrastripes',
526   'ZEBRA_SRU_HOST'    => 'localhost',
527   'ZEBRA_SRU_BIBLIOS_PORT'    => '9998',
528   'ZEBRA_SRU_AUTHORITIES_PORT'    => '9999',
529   'KOHA_USER'         => 'koha',
530   'KOHA_GROUP'        => 'koha',
531   'MERGE_SERVER_HOST' => 'localhost',
532   'MERGE_SERVER_PORT' => '11001',
533   'PAZPAR2_HOST' => 'localhost',
534   'PAZPAR2_PORT' => '11002',
535   'RUN_DATABASE_TESTS' => 'no',
536   'PATH_TO_ZEBRA' => '',
537   'USE_MEMCACHED'     => 'yes',
538   'MEMCACHED_SERVERS' => '127.0.0.1:11211',
539   'MEMCACHED_NAMESPACE' => 'KOHA',
540   'TEMPLATE_CACHE_DIR' => '/tmp/koha',
541   'USE_ELASTICSEARCH'     => 'no',
542   'ELASTICSEARCH_SERVERS' => 'localhost:9200',
543   'ELASTICSEARCH_INDEX'  => 'koha',
544   'FONT_DIR'          => '/usr/share/fonts/truetype/dejavu',
545   'SMTP_HOST'      => 'localhost',
546   'SMTP_PORT'      => '25',
547   'SMTP_TIMEOUT'   => '120',
548   'SMTP_SSL_MODE'  => 'disabled',
549   'SMTP_USER_NAME' => '',
550   'SMTP_PASSWORD'  => '',
551   'SMTP_DEBUG'     => 'no',
552 );
553
554 # set some default configuration options based on OS
555 # more conditions need to be added for other OS's
556
557 warn "Your platform appears to be $^O.\n" if $DEBUG;
558
559 if ( $^O eq 'cygwin' ) {
560         # Most Unix2Win32 ports seem to poke everything into the Program Files directory
561         # this could be changed to put some files (ie. libraries) into system32, etc.
562         $config_defaults{'INSTALL_MODE'} = 'single';
563         $config_defaults{'INSTALL_BASE'} = 'c:/progra~1/koha';  # Use 8.3 names to be safe...
564 }
565 else {
566         $config_defaults{'INSTALL_MODE'} = 'standard';
567         $config_defaults{'INSTALL_BASE'} = '/usr/share/koha';
568 }
569
570 # valid values for certain configuration options
571 my %valid_config_values = (
572   'INSTALL_MODE'  => { 'standard' => 1, 'single' => 1, 'dev' => 1 },
573   'DB_TYPE' => { 'mysql' => 1, 'Pg' => 1 },
574   'DB_USE_TLS' => {'yes', 'no'},
575   'INSTALL_SRU' => { 'yes' => 1, 'no' => 1 },
576   'ZEBRA_MARC_FORMAT' => { 'marc21' => 1, 'normarc' => 1, 'unimarc' => 1 }, # FIXME should generate from contents of distributation
577   'ZEBRA_LANGUAGE'    => { 'cs' => 1, 'el' => 1, 'en' => 1, 'es' => 1, 'fr' => 1, 'nb' => 1, 'ru' => 1, 'uk' => 1 }, # FIXME should generate from contents of distribution
578   'ZEBRA_TOKENIZER' => { chr => 1, icu => 1 },
579   'RUN_DATABASE_TESTS' => { 'yes' => 1, 'no' => 1 },
580   'USE_MEMCACHED'      => { 'yes' => 1, 'no' => 1 },
581   'USE_ELASTICSEARCH'  => { 'yes' => 1, 'no' => 1 },
582   'SMTP_SSL_MODE'      => { 'disabled' => 1, 'ssl' => 1, 'starttls' => 1 },
583   'SMTP_DEBUG'         => { 'yes' => 1, 'no' => 1 },
584 );
585
586 # get settings from command-line
587 my $koha_install_log = "";
588 my $cli_koha_install_mode = "";
589 my $cli_koha_db_type = "";
590 my $cli_koha_db_host = "";
591 my $cli_koha_db_port = "";
592 my $cli_koha_db_name = "";
593 my $cli_koha_db_user = "";
594 my $cli_koha_db_pass = "";
595 my $cli_zebra_marc_format = "";
596 my $cli_zebra_language = "",
597 my $cli_zebra_tokenizer = "";
598 my $cli_zebra_user = "";
599 my $cli_zebra_pass = "";
600 my $cli_zebra_sru_host = "";
601 my $cli_zebra_sru_bib_port = "";
602 my $cli_zebra_sru_auth_port = "";
603 my $cli_koha_user = "";
604 my $cli_koha_group = "";
605 my $cli_koha_install_sru = "";
606 my $cli_koha_install_pazpar2 = "";
607 my $cli_koha_use_memcached = "";
608 my $cli_koha_font_dir = "";
609 my $cli_koha_run_database_tests = "";
610 my $cli_koha_install_base = "";
611 my $cli_koha_template_cache_dir = "";
612 my $cli_smtp_host      = "";
613 my $cli_smtp_port      = "";
614 my $cli_smtp_timeout   = "";
615 my $cli_smtp_ssl_mode  = "";
616 my $cli_smtp_user_name = "";
617 my $cli_smtp_password  = "";
618 my $cli_smtp_debug     = "";
619
620 Getopt::Long::Configure('pass_through');
621 my $results = GetOptions(
622     "prev-install-log=s"           => \$koha_install_log,
623     "install_mode=s"               => \$cli_koha_install_mode,
624     "db_type=s"                    => \$cli_koha_db_type,
625     "db_host=s"                    => \$cli_koha_db_host,
626     "db_port=s"                    => \$cli_koha_db_port,
627     "db_name=s"                    => \$cli_koha_db_name,
628     "db_user=s"                    => \$cli_koha_db_user,
629     "db_pass=s"                    => \$cli_koha_db_pass,
630     "zebra_marc_format=s"          => \$cli_zebra_marc_format,
631     "zebra_language=s"             => \$cli_zebra_language,
632     "zebra_tokenizer=s"            => \$cli_zebra_tokenizer,
633     "zebra_user=s"                 => \$cli_zebra_user,
634     "zebra_pass=s"                 => \$cli_zebra_pass,
635     "zebra_sru_host=s"             => \$cli_zebra_sru_host,
636     "zebra_sru_biblios_port=s"     => \$cli_zebra_sru_bib_port,
637     "zebra_sru_authorities_port=s" => \$cli_zebra_sru_auth_port,
638     "koha_user=s"                  => \$cli_koha_user,
639     "koha_group=s"                 => \$cli_koha_group,
640     "install_sru=s"                => \$cli_koha_install_sru,
641     "install_pazpar2=s"            => \$cli_koha_install_pazpar2,
642     "use_memcached=s"              => \$cli_koha_use_memcached,
643     "font_dir=s"                   => \$cli_koha_font_dir,
644     "run_database_tests=s"         => \$cli_koha_run_database_tests,
645     "install_base=s"               => \$cli_koha_install_base,
646     "template-cache-dir=s"         => \$cli_koha_template_cache_dir,
647     "smtp-host=s"                  => \$cli_smtp_host,
648     "smtp-port=s"                  => \$cli_smtp_port,
649     "smtp-timeout=s"               => \$cli_smtp_timeout,
650     "smtp-ssl-mode=s"              => \$cli_smtp_ssl_mode,
651     "smtp-user-name=s"             => \$cli_smtp_user_name,
652     "smtp-password=s"              => \$cli_smtp_password,
653     "smtp-debug"                   => \$cli_smtp_debug,
654     "help"                         => sub { HelpMessage(0) },
655 ) or HelpMessage(1);
656
657 my %install_log_values = ();
658 if ($koha_install_log ne "") {
659     get_install_log_values($koha_install_log, \%install_log_values);
660 } else {
661     # Try to set install_log_values for provided values;
662     get_cli_values(\%install_log_values);
663 }
664
665 my %config = get_configuration(\%config_defaults, \%valid_config_values, \%install_log_values);
666 my ($target_directories, $skip_directories) = get_target_directories(\%config);
667 display_configuration(\%config, $target_directories);
668 my $file_map = {};
669 get_file_map($target_map, $dirtree, $file_map);
670
671 my $pl_files = {
672       'rewrite-config.PL' => [
673          'blib/KOHA_CONF_DIR/koha-conf.xml',
674          'blib/KOHA_CONF_DIR/koha-httpd.conf',
675          'blib/KOHA_CONF_DIR/log4perl.conf',
676          'blib/KOHA_CONF_DIR/koha-worker.service',
677          'blib/ZEBRA_CONF_DIR/etc/default.idx',
678          'blib/MISC_DIR/koha-install-log'
679          ],
680           'fix-perl-path.PL' => [       # this script ensures the correct shebang line for the platform installed on...
681                  'blib'
682                  ],
683 };
684
685 push @{ $pl_files->{'rewrite-config.PL'} }, (
686     'blib/ZEBRA_CONF_DIR/etc/passwd',
687     'blib/ZEBRA_CONF_DIR/zebra-biblios-dom.cfg',
688     'blib/ZEBRA_CONF_DIR/zebra-authorities-dom.cfg',
689     'blib/ZEBRA_CONF_DIR/explain-authorities.xml',
690     'blib/ZEBRA_CONF_DIR/explain-biblios.xml',
691     'blib/ZEBRA_CONF_DIR/retrieval-info-auth-dom.xml',
692     'blib/ZEBRA_CONF_DIR/retrieval-info-bib-dom.xml',
693 );
694 push @{ $pl_files->{'rewrite-config.PL'} }, (
695     'blib/SCRIPT_DIR/koha-zebra-ctl.sh',
696     'blib/SCRIPT_DIR/koha-pazpar2-ctl.sh',
697     'blib/SCRIPT_DIR/koha-index-daemon-ctl.sh',
698 );
699 if ($config{'INSTALL_PAZPAR2'} eq 'yes') {
700     push @{ $pl_files->{'rewrite-config.PL'} }, (
701         'blib/PAZPAR2_CONF_DIR/koha-biblios.xml',
702         'blib/PAZPAR2_CONF_DIR/pazpar2.xml'
703     );
704 }
705 $config{'ZEBRA_AUTH_CFG'} = 'zebra-authorities-dom.cfg';
706 $config{'ZEBRA_BIB_CFG'}  = 'zebra-biblios-dom.cfg';
707 $config{'AUTH_RETRIEVAL_CFG'} = 'retrieval-info-auth-dom.xml';
708 $config{'BIB_RETRIEVAL_CFG'}  = 'retrieval-info-bib-dom.xml';
709
710 if ($config{'INSTALL_MODE'} ne "dev") {
711     push @{ $pl_files->{'rewrite-config.PL'} }, (
712         'blib/PERL_MODULE_DIR/C4/Context.pm',
713         'blib/SCRIPT_NONDEV_DIR/kohalib.pl'
714     );
715 }
716
717 $config{ZEBRA_TOKENIZER_STMT} = $config{ZEBRA_TOKENIZER} eq 'icu'
718     ? 'icuchain words-icu.xml'
719     : 'charmap word-phrase-utf.chr';
720
721 $config{ZEBRA_PTOKENIZER_STMT} = $config{ZEBRA_TOKENIZER} eq 'icu'
722     ? 'icuchain phrases-icu.xml'
723     : 'charmap word-phrase-utf.chr';
724
725 my %test_suite_override_dirs = (
726     KOHA_CONF_DIR  => ['etc'],
727     ZEBRA_CONF_DIR => ['etc', 'zebradb'],
728     Z3950_CONF_DIR => ['etc', 'z3950'],
729     LOCK_DIR       => ['var', 'lock'],
730     LOG_DIR        => ['var', 'log'],
731     BACKUP_DIR     => ['var', 'spool'],
732     SCRIPT_DIR     => ['bin'],
733     ZEBRA_LOCK_DIR => ['var', 'lock', 'zebradb'],
734     ZEBRA_DATA_DIR => ['var', 'lib', 'zebradb'],
735     ZEBRA_RUN_DIR  => ['var', 'run', 'zebradb'],
736 );
737
738 WriteMakefile(
739               NAME         => 'koha',
740               #VERSION     => strftime('2.9.%Y%m%d%H',gmtime),
741               VERSION_FROM => 'kohaversion.pl',
742               ABSTRACT     => 'Award-winning integrated library system (ILS) and Web OPAC',
743               AUTHOR       => 'Koha Contributors <https://koha-community.org/>',
744               NO_META      => 1,
745               PREREQ_PM    => $koha_pm->prereq_pm,
746
747               # File tree mapping
748               PM => $file_map,
749
750               # Man pages generated from POD
751               # ExtUtils::MakeMaker already manage $(DESTDIR)
752               INSTALLMAN1DIR => File::Spec->catdir(_strip_destdir($target_directories->{'MAN_DIR'}), 'man1'),
753               INSTALLMAN3DIR => File::Spec->catdir(_strip_destdir($target_directories->{'MAN_DIR'}), 'man3'),
754
755               PL_FILES => $pl_files,
756 );
757
758 =head1 FUNCTIONS
759
760 =head2 hashdir
761
762 This function recurses through the directory structure and builds
763 a hash of hashes containing the structure with arrays holding filenames.
764 This directory hashing routine was taken from BrowserUK @ http://www.perlmonks.org/?node_id=219919
765
766 =cut
767
768 sub hashdir{
769     my $dir = shift;
770     opendir my $dh, $dir or die $!;
771     my $tree = {}->{$dir} = {};
772     while( my $file = readdir($dh) ) {
773         next if $file =~ m/^\.{1,2}/ and $file !~ /^\.htaccess/; # .htaccess is a special case
774         my $path = $dir .'/' . $file;
775         $tree->{$file} = hashdir($path), next if -d $path;
776         push @{$tree->{'.'}}, $file;
777     }
778     return $tree;
779 }
780
781 =head2 get_file_map
782
783 This function combines the target_map and file hash to
784 map each source file to its destination relative to
785 the set of installation targets.
786
787 Output will be a hash mapping from each source file
788 to its destination value, like this:
789
790 'mainpage.pl' => '$(INTRANET_CGI_DIR)/mainpage.pl'
791
792 =cut
793
794 sub get_file_map {
795     my $target_map = shift;
796     my $dirtree = shift;
797     my $file_map = shift;
798     my $curr_path = @_ ? shift : ['.'];
799
800     # Traverse the directory tree.
801     # For each file or directory, identify the
802     # most specific match in the target_map
803     foreach my $dir (sort keys %{ $dirtree }) {
804         if ($dir eq '.') {
805             # deal with files in directory
806             foreach my $file (sort @{ $dirtree->{$dir} }) {
807                 my $targetdir = undef;
808                 my $matchlevel = undef;
809                 # first, see if there is a match on this specific
810                 # file in the target map
811                 my $filepath = join("/", @$curr_path, $file);
812                 if (exists $target_map->{$filepath}) {
813                     $targetdir = $target_map->{$filepath};
814                     $matchlevel = scalar(@$curr_path) + 1;
815                 } else {
816                     # no match on the specific file; look for
817                     # a directory match
818                     for (my $i = scalar(@$curr_path) - 1; $i >= 0; $i--)  {
819                         my $dirpath = join("/", @$curr_path[0..$i]);
820                         if (exists $target_map->{$dirpath}) {
821                             $targetdir = $target_map->{$dirpath};
822                             $matchlevel = $i + 1;
823                             last;
824                         }
825                     }
826                 }
827                 if (defined $targetdir) {
828                      _add_to_file_map($file_map, $targetdir, $curr_path, $file, $matchlevel);
829                 } else {
830                     my $path = join("/", @$curr_path);
831                     print "failed to map: $path/$file\n" if $DEBUG;
832                 }
833             }
834         } else {
835             # dealing with subdirectory
836             push @$curr_path, $dir;
837             get_file_map($target_map, $dirtree->{$dir}, $file_map, $curr_path);
838             pop @$curr_path;
839         }
840     }
841 }
842
843 sub _add_to_file_map {
844     my $file_map = shift;
845     my $targetdir = shift;
846     my $curr_path = shift;
847     my $file = shift;
848     my $matchlevel = shift;
849     my $dest_path = @_ ? shift : $curr_path;
850
851     # The target can be one of the following:
852     # 1. scalar representing target symbol
853     # 2. hash ref containing target and trimdir keys
854     #
855     # Consequently, this routine traverses this structure,
856     # calling itself recursively, until it deals with
857     # all of the scalar target symbols.
858     if (ref $targetdir eq 'HASH') {
859         my $subtarget = $targetdir->{target};
860         if (exists $targetdir->{trimdir}) {
861             # if we get here, we've specified that
862             # rather than installing the file to
863             # $(TARGET)/matching/dirs/subdirs/file,
864             # we want to install it to
865             # $(TARGET)/subdirs/file
866             #
867             # Note that this the only place where
868             # $matchlevel is used.
869             my @new_dest_path = @$dest_path;
870             if ($targetdir->{trimdir} == -1)  {
871                 splice @new_dest_path, 0, $matchlevel;
872             } else {
873                 splice @new_dest_path, 0, $targetdir->{trimdir};
874             }
875             _add_to_file_map($file_map, $subtarget, $curr_path, $file, $matchlevel, \@new_dest_path);
876         } else {
877             # actually getting here means that the
878             # target was unnecessarily listed
879             # as a hash, but we'll forgive that
880             _add_to_file_map($file_map, $subtarget, $curr_path, $file, $matchlevel);
881         }
882     } elsif ($targetdir ne 'NONE' and $targetdir ne '') {
883         my $source = File::Spec->catfile(@$curr_path, $file);
884         my $destination = File::Spec->catfile('blib', $targetdir, @$dest_path, $file);
885         #print "$source => $destination\n"; # DEBUG
886         # quote spaces in file names
887         # FIXME: this is of questionable portability and
888         # probably depends on user's make recognizing this
889         # quoting syntax -- probably better to remove
890         # spaces and shell metacharacters from all file names
891         $source =~ s/ /\\ /g;
892         $destination =~ s/ /\\ /g;
893
894         $file_map->{$source} = $destination;
895     }
896 }
897
898 =head2 get_cli_values
899
900 Reads values provided on cli for configuration values
901
902 =cut
903
904 sub get_cli_values {
905     my $values = shift;
906     my $map = {
907         INSTALL_MODE               => $cli_koha_install_mode,
908         DB_TYPE                    => $cli_koha_db_type,
909         DB_HOST                    => $cli_koha_db_host,
910         DB_PORT                    => $cli_koha_db_port,
911         DB_NAME                    => $cli_koha_db_name,
912         DB_USER                    => $cli_koha_db_user,
913         DB_PASS                    => $cli_koha_db_pass,
914         ZEBRA_MARC_FORMAT          => $cli_zebra_marc_format,
915         ZEBRA_LANGUAGE             => $cli_zebra_language,
916         ZEBRA_TOKENIZER            => $cli_zebra_tokenizer,
917         ZEBRA_USER                 => $cli_zebra_user,
918         ZEBRA_PASS                 => $cli_zebra_pass,
919         ZEBRA_SRU_HOST             => $cli_zebra_sru_host,
920         ZEBRA_SRU_BIBLIOS_PORT     => $cli_zebra_sru_bib_port,
921         ZEBRA_SRU_AUTHORITIES_PORT => $cli_zebra_sru_auth_port,
922         KOHA_USER                  => $cli_koha_user,
923         KOHA_GROUP                 => $cli_koha_group,
924         INSTALL_SRU                => $cli_koha_install_sru,
925         INSTALL_PAZPAR2            => $cli_koha_install_pazpar2,
926         USE_MEMCACHED              => $cli_koha_use_memcached,
927         FONT_DIR                   => $cli_koha_font_dir,
928         RUN_DATABASE_TESTS         => $cli_koha_run_database_tests,
929         INSTALL_BASE               => $cli_koha_install_base,
930         TEMPLATE_CACHE_DIR         => $cli_koha_template_cache_dir,
931         SMTP_HOST                  => $cli_smtp_host,
932         SMTP_PORT                  => $cli_smtp_port,
933         SMTP_TIMEOUT               => $cli_smtp_timeout,
934         SMTP_SSL_MODE              => $cli_smtp_ssl_mode,
935         SMTP_USER_NAME             => $cli_smtp_user_name,
936         SMTP_PASSWORD              => $cli_smtp_password,
937         SMTP_DEBUG                 => (defined $cli_smtp_debug) ? ( $cli_smtp_debug ? 'yes' : 'no' ) : $values->{SMTP_DEBUG},
938     };
939     foreach my $key (keys %{$map}) {
940         $values->{$key} = $map->{$key} if ($map->{$key});
941     }
942 }
943
944 =head2 get_install_log_values
945
946 Reads value from the Koha install log specified by
947 --prev-install-log
948
949 =cut
950
951 sub get_install_log_values {
952     my $install_log = shift;
953     my $values = shift;
954
955     open my $log, '<', $install_log or die "Cannot open install log $install_log: $!\n";
956     while (<$log>) {
957         chomp;
958         next if /^#/ or /^\s*$/;
959         next if /^=/;
960         next unless m/=/;
961         s/\s+$//g;
962         my ($key, $value) = split /=/, $_, 2;
963         $values->{$key} = $value;
964     }
965     close $log;
966
967     print <<_EXPLAIN_INSTALL_LOG_;
968 Reading values from install log $install_log.  You
969 will be prompted only for settings that have been
970 added since the last time you installed Koha.  To
971 be prompted for all settings, run 'perl Makefile.PL'
972 without the --prev-install-log option.
973 _EXPLAIN_INSTALL_LOG_
974 }
975
976 =head2 get_configuration
977
978 This prompts the user for various configuration options.
979
980 =cut
981
982 sub get_configuration {
983   my $defaults = shift;
984   my $valid_values = shift;
985   my $install_log_values = shift;
986   my %config = ();
987
988   my $msg = q(
989 By default, Koha can be installed in one of three ways:
990
991 standard: Install files in conformance with the Filesystem
992           Hierarchy Standard (FHS).  This is the default mode
993           and should be used when installing a production
994           Koha system.  On Unix systems, root access is
995           needed to complete a standard installation.
996
997 single:   Install files under a single directory.  This option
998           is useful for installing Koha without root access, e.g.,
999           on a web host that allows CGI scripts and MySQL databases
1000           but requires the user to keep all files under the user's
1001           HOME directory.
1002
1003 dev:      Create a set of symbolic links and configuration files to
1004           allow Koha to run directly from the source distribution.
1005           This mode is useful for developers who want to run
1006           Koha from a git clone.
1007
1008 Installation mode);
1009     $msg .= _add_valid_values_disp('INSTALL_MODE', $valid_values);
1010     $config{'INSTALL_MODE'} = _get_value('INSTALL_MODE', $msg, $defaults->{'INSTALL_MODE'}, $valid_values, $install_log_values);
1011
1012     # set message and default value for INSTALL_BASE
1013     # depending on value of INSTALL_MODE
1014     my $install_base_default = $defaults->{'INSTALL_BASE'};
1015     if ($config{'INSTALL_MODE'} eq 'dev') {
1016         $msg = q(
1017 Please specify the directory in which to install Koha's
1018 active configuration files and (if applicable) the
1019 Zebra database.  Koha's CGI scripts and templates will
1020 be run from the current directory.
1021
1022 Configuration directory:);
1023         # FIXME - home directory portability consideration apply
1024         $install_base_default =
1025             $ENV{DESTDIR}
1026             || ( exists $ENV{HOME} ? "$ENV{HOME}/koha-dev" : "$defaults->{'INSTALL_BASE'}-dev" )
1027         ;
1028     } elsif ($config{'INSTALL_MODE'} eq 'single') {
1029         $msg = "\nPlease specify the directory in which to install Koha";
1030         # FIXME -- we're assuming under a 'single' mode install
1031         # that user will likely want to install under the home
1032         # directory.  This is OK in and of itself, but we should
1033         # use File::HomeDir to locate the home directory portably.
1034         # This is deferred for now because File::HomeDir is not yet
1035         # core.
1036                 # --we must also keep this portable to the major OS's -fbcit
1037         $install_base_default = (exists $ENV{'HOME'}) ? "$ENV{'HOME'}/koha" : $defaults->{'INSTALL_BASE'};
1038     } else {
1039         # must be standard
1040         $msg = q(
1041 Please specify the directory under which most Koha files
1042 will be installed.
1043
1044 Note that if you are planning in installing more than
1045 one instance of Koha, you may want to modify the last
1046 component of the directory path, which will be used
1047 as the package name in the FHS layout.
1048
1049 Base installation directory);
1050     }
1051     $config{'INSTALL_BASE'} = _get_value('INSTALL_BASE', $msg, $install_base_default, $valid_values, $install_log_values);
1052
1053     $config{'INSTALL_BASE'} = File::Spec->rel2abs($config{'INSTALL_BASE'});
1054         print "INSTALL_BASE=$config{'INSTALL_BASE'}\r\n" if $DEBUG;
1055     if ($config{'INSTALL_MODE'} eq "standard") {
1056         $msg = q(
1057 Since you are using the 'standard' install
1058 mode, you should run 'make install' as root.
1059 However, it is recommended that a non-root
1060 user (on Unix and Linux platforms) have
1061 ownership of Koha's files, including the
1062 Zebra indexes if applicable.
1063
1064 Please specify a user account.  This
1065 user account does not need to exist
1066 right now, but it needs to exist
1067 before you run 'make install'.  Please
1068 note that for security reasons, this
1069 user should not be the same as the user
1070 account Apache runs under.
1071
1072 User account);
1073         $config{'KOHA_USER'} = _get_value('KOHA_USER', $msg, $defaults->{'KOHA_USER'}, $valid_values, $install_log_values);
1074
1075         $msg = q(
1076 Please specify the group that should own
1077 Koha's files.  As above, this group need
1078 not exist right now, but should be created
1079 before you run 'make install'.
1080
1081 Group);
1082         $config{'KOHA_GROUP'} = _get_value('KOHA_GROUP', $msg, $defaults->{'KOHA_GROUP'}, $valid_values, $install_log_values);
1083     }
1084
1085     $msg = q(
1086 Please specify which database engine you will use
1087 to store data in Koha.  The choices are MySQL and
1088 PostgreSQL; please note that at the moment
1089 PostgreSQL support is highly experimental.
1090
1091 DBMS to use);
1092     $msg .= _add_valid_values_disp('DB_TYPE', $valid_values);
1093     $config{'DB_TYPE'} = _get_value('DB_TYPE', $msg, $defaults->{'DB_TYPE'}, $valid_values, $install_log_values);
1094
1095     $msg = q(
1096 Please specify the name or address of your
1097 database server.  Note that the database
1098 does not have to exist at this point, it
1099 can be created after running 'make install'
1100 and before you try using Koha for the first time.
1101
1102 Database server);
1103     $config{'DB_HOST'} = _get_value('DB_HOST', $msg, $defaults->{'DB_HOST'}, $valid_values, $install_log_values);
1104
1105     $msg = q(
1106 Please specify the port used to connect to the
1107 DMBS);
1108     my $db_port_default = $config{'DB_TYPE'} eq 'mysql' ? '3306' : '5432';
1109     $config{'DB_PORT'} = _get_value('DB_PORT', $msg, $db_port_default, $valid_values, $install_log_values);
1110
1111     $msg = q(
1112 Please specify the name of the database to be
1113 used by Koha);
1114     $config{'DB_NAME'} = _get_value('DB_NAME', $msg, $defaults->{'DB_NAME'}, $valid_values, $install_log_values);
1115     if ($config{'DB_TYPE'} eq 'mysql'){
1116         $msg = q(
1117 Please specify whether the connection to MySQL will use TLS
1118         );
1119         $config{'DB_USE_TLS'} = _get_value('DB_USE_TLS', $msg, $defaults->{'DB_USE_TLS'}, $valid_values, $install_log_values);
1120     }
1121     if ($config{'DB_USE_TLS'} eq 'yes'){
1122         $msg = q(
1123 Please enter the path to the CA certificate for TLS
1124         );
1125         $config{'DB_TLS_CA_CERTIFICATE'} = _get_value('DB_TLS_CA_CERTIFICATE', $msg, $defaults->{'DB_TLS_CA_CERTIFICATE'}, $valid_values, $install_log_values);
1126
1127         $msg = q(
1128 Please enter the path to the client certificate for TLS
1129         );
1130         $config{'DB_TLS_CLIENT_CERTIFICATE'} = _get_value('DB_TLS_CLIENT_CERTIFICATE', $msg, $defaults->{'DB_TLS_CLIENT_CERTIFICATE'}, $valid_values, $install_log_values);
1131         $msg = q(
1132 Please enter the path to the client key for TLS
1133         );
1134         $config{'DB_TLS_CLIENT_KEY'} = _get_value('DB_TLS_CLIENT_KEY', $msg, $defaults->{'DB_TLS_CLIENT_KEY'}, $valid_values, $install_log_values);
1135     }
1136
1137     $msg = q(
1138 Please specify the user that owns the database to be
1139 used by Koha);
1140     $config{'DB_USER'} = _get_value('DB_USER', $msg, $defaults->{'DB_USER'}, $valid_values, $install_log_values);
1141
1142     $msg = q(
1143 Please specify the password of the user that owns the
1144 database to be used by Koha);
1145     $config{'DB_PASS'} = _get_value('DB_PASS', $msg, $defaults->{'DB_PASS'}, $valid_values, $install_log_values);
1146
1147     print q(
1148 Koha uses the Zebra search engine for high-performance
1149 searching of bibliographic and authority records.  Checking
1150 to see if you have installed the Zebra software...
1151 );
1152
1153     if (defined(my $zebra_path = find_zebra())) {
1154         $config{'PATH_TO_ZEBRA'} = $zebra_path;
1155         print qq(
1156 Found 'zebrasrv' and 'zebraidx' in $zebra_path.
1157 );
1158     } else {
1159         print q(
1160 Unable to find the Zebra programs 'zebrasrv' and 'zebraidx'
1161 in your PATH or in some of the usual places.  If you haven't
1162 installed Zebra yet, please do so and run Makefile.PL again.
1163
1164 );
1165     }
1166
1167     $msg = q(
1168 You must specify the primary MARC format of the
1169 records to be indexed by Zebra.
1170
1171 Koha provides Zebra configuration files for MARC21,
1172 NORMARC and UNIMARC.
1173
1174 MARC format for Zebra indexing);
1175     $msg .= _add_valid_values_disp('ZEBRA_MARC_FORMAT', $valid_values);
1176     $config{'ZEBRA_MARC_FORMAT'} = _get_value('ZEBRA_MARC_FORMAT', $msg, $defaults->{'ZEBRA_MARC_FORMAT'}, $valid_values, $install_log_values);
1177     $msg = q(
1178 Koha supplies Zebra configuration files tuned for
1179 searching either English (en) or French (fr) MARC
1180 records.
1181
1182 Primary language for Zebra indexing);
1183     $msg .= _add_valid_values_disp('ZEBRA_LANGUAGE', $valid_values);
1184     $config{'ZEBRA_LANGUAGE'} = _get_value('ZEBRA_LANGUAGE', $msg, $defaults->{'ZEBRA_LANGUAGE'}, $valid_values, $install_log_values);
1185
1186     $msg = q(
1187 Zebra has two methods to perform records tokenization
1188 and characters normalization: CHR and ICU. ICU is
1189 recommended for catalogs containing non-Latin
1190 characters.);
1191
1192     $msg .= _add_valid_values_disp('ZEBRA_TOKENIZER', $valid_values);
1193     $config{'ZEBRA_TOKENIZER'} = _get_value('ZEBRA_TOKENIZER', $msg, $defaults->{'ZEBRA_TOKENIZER'}, $valid_values, $install_log_values);
1194
1195     $msg = q(
1196 Please specify Zebra database user);
1197     $config{'ZEBRA_USER'} = _get_value('ZEBRA_USER', $msg, $defaults->{'ZEBRA_USER'}, $valid_values, $install_log_values);
1198
1199     $msg = q(
1200 Please specify the Zebra database password);
1201     $config{'ZEBRA_PASS'} = _get_value('ZEBRA_PASS', $msg, $defaults->{'ZEBRA_PASS'}, $valid_values, $install_log_values);
1202
1203     $msg = q(
1204 Since you've chosen to use Zebra, you can enable the SRU/
1205 Z39.50 Server if you so choose, but you must specify a
1206 few configuration options for it.
1207
1208 Please note that if you choose *NOT* to configure SRU,
1209 koha-conf.xml will still contain some references to SRU
1210 settings.  Those references will be ignored by Koha.
1211
1212 Install the SRU configuration files?);
1213     $msg .= _add_valid_values_disp('INSTALL_SRU', $valid_values);
1214     $config{'INSTALL_SRU'} = _get_value('INSTALL_SRU', $msg, $defaults->{'INSTALL_SRU'}, $valid_values, $install_log_values);
1215
1216     if ($config{'INSTALL_SRU'} eq 'yes') {
1217         $msg = q(
1218 Since you've chosen to configure SRU, you must
1219 specify the host and port(s) that the SRU
1220 Servers (bibliographic and authority) should run on.
1221 );
1222         $msg = q(
1223 SRU Database host?);
1224         $config{'ZEBRA_SRU_HOST'} = _get_value('ZEBRA_SRU_HOST', $msg, $defaults->{'ZEBRA_SRU_HOST'}, $valid_values, $install_log_values);
1225
1226         $msg = q(
1227 SRU port for bibliographic data?);
1228         $config{'ZEBRA_SRU_BIBLIOS_PORT'} = _get_value('ZEBRA_SRU_BIBLIOS_PORT', $msg, $defaults->{'ZEBRA_SRU_BIBLIOS_PORT'}, $valid_values, $install_log_values);
1229
1230         $msg = q(
1231 SRU port for authority data?);
1232         $config{'ZEBRA_SRU_AUTHORITIES_PORT'} = _get_value('ZEBRA_SRU_AUTHORITIES_PORT', $msg, $defaults->{'ZEBRA_SRU_AUTHORITIES_PORT'}, $valid_values, $install_log_values);
1233
1234     }
1235
1236     $msg = q(
1237 Along with Zebra, you can also choose to install PazPar2,
1238 which is a metasearch tool.  With PazPar2,
1239 Koha can perform on-the-fly merging of bibliographic
1240 records during searching, allowing for FRBRization of
1241 the results list.
1242
1243 Install the PazPar2 configuration files?);
1244     $msg .= _add_valid_values_disp('INSTALL_PAZPAR2', $valid_values);
1245     $config{'INSTALL_PAZPAR2'} = _get_value('INSTALL_PAZPAR2', $msg, $defaults->{'INSTALL_PAZPAR2'}, $valid_values, $install_log_values);
1246
1247     if ($config{'INSTALL_PAZPAR2'} eq 'yes') {
1248         $msg = q(
1249 Since you've chosen to configure PazPar2, you must
1250 specify the host and port(s) that PazPar2
1251 uses:
1252 );
1253         $msg = q(
1254 Zebra bibliographic server host?);
1255         $config{'MERGE_SERVER_HOST'} = _get_value('MERGE_SERVER_HOST', $msg, $defaults->{'MERGE_SERVER_HOST'}, $valid_values, $install_log_values);
1256
1257         $msg = q(
1258 Zebra bibliographic port for PazPar2 to use?);
1259         $config{'MERGE_SERVER_PORT'} = _get_value('MERGE_SERVER_PORT', $msg, $defaults->{'MERGE_SERVER_PORT'}, $valid_values, $install_log_values);
1260
1261         $msg = q(
1262 PazPar2 host?);
1263         $config{'PAZPAR2_HOST'} = _get_value('PAZPAR2_HOST', $msg, $defaults->{'PAZPAR2_HOST'}, $valid_values, $install_log_values);
1264
1265         $msg = q(
1266 PazPar2 port?);
1267         $config{'PAZPAR2_PORT'} = _get_value('PAZPAR2_PORT', $msg, $defaults->{'PAZPAR2_PORT'}, $valid_values, $install_log_values);
1268
1269     }
1270
1271   $msg = q(
1272 Use memcached to cache the results of some function calls?
1273 This provides a significant performance improvement.
1274 You will need a Memcached server running.);
1275   $msg .= _add_valid_values_disp('USE_MEMCACHED', $valid_values);
1276   $config{'USE_MEMCACHED'} = _get_value('USE_MEMCACHED', $msg, $defaults->{'USE_MEMCACHED'}, $valid_values, $install_log_values);
1277   if ($config{'USE_MEMCACHED'} eq 'yes'){
1278       $msg = q(
1279 Since you've chosen to use caching, you must specify the memcached servers and the namespace to use:
1280 );
1281       $msg = q(
1282 Memcached server address?);
1283       $config{'MEMCACHED_SERVERS'} = _get_value('MEMCACHED_SERVERS', $msg, $defaults->{'MEMCACHED_SERVERS'}, $valid_values, $install_log_values);
1284
1285       $msg = q(
1286 Memcached namespace?);
1287       $config{'MEMCACHED_NAMESPACE'} = _get_value('MEMCACHED_NAMESPACE', $msg, $defaults->{'MEMCACHED_NAMESPACE'}, $valid_values, $install_log_values);
1288   }
1289
1290
1291   $msg = q(
1292 Use Elasticsearch to serve search results?
1293 You will need a Elasticsearch server running.);
1294   $msg .= _add_valid_values_disp('USE_ELASTICSEARCH', $valid_values);
1295   $config{'USE_ELASTICSEARCH'} = _get_value('USE_ELASTICSEARCH', $msg, $defaults->{'USE_ELASTICSEARCH'}, $valid_values, $install_log_values);
1296   if ($config{'USE_ELASTICSEARCH'} eq 'yes'){
1297       $msg = q(
1298 Define the ELASTICSEARCH_SERVERS with a comma-separated list. Eg
1299 localhost:9200, 192.168.0.100:9200
1300 );
1301       $msg = q(
1302 Elasticsearch server addresses?);
1303       $config{'ELASTICSEARCH_SERVERS'} = _get_value('ELASTICSEARCH_SERVERS', $msg, $defaults->{'ELASTICSEARCH_SERVERS'}, $valid_values, $install_log_values);
1304
1305       $msg = q(
1306 What index name to use for this Koha-instance? It must be unique for each Koha
1307 sharing the same Elasticsearch-cluster
1308
1309 Elasticsearch index?);
1310       $config{'ELASTICSEARCH_INDEX'} = _get_value('ELASTICSEARCH_INDEX', $msg, $defaults->{'ELASTICSEARCH_INDEX'}, $valid_values, $install_log_values);
1311   }
1312
1313   $msg = q(
1314 Template cache directory?);
1315   $config{'TEMPLATE_CACHE_DIR'} = _get_value('TEMPLATE_CACHE_DIR', $msg, $defaults->{'TEMPLATE_CACHE_DIR'}, $valid_values, $install_log_values);
1316
1317   $msg = q(
1318 Path to DejaVu fonts?);
1319   $config{'FONT_DIR'} = _get_value('FONT_DIR', $msg, $defaults->{'FONT_DIR'}, $valid_values, $install_log_values);
1320
1321   $msg = q(
1322 SMTP settings
1323 You will be able to set your default SMTP configuration.);
1324   $msg .= q{
1325 SMTP host name?};
1326   $config{'SMTP_HOST'} = _get_value('SMTP_HOST', $msg, $defaults->{'SMTP_HOST'}, $valid_values, $install_log_values);
1327
1328   $msg = q{
1329 SMTP port?};
1330   $config{'SMTP_PORT'} = _get_value('SMTP_PORT', $msg, $defaults->{'SMTP_PORT'}, $valid_values, $install_log_values);
1331
1332   $msg = q{
1333 SMTP timeout (in seconds)?};
1334   $config{'SMTP_TIMEOUT'} = _get_value('SMTP_TIMEOUT', $msg, $defaults->{'SMTP_TIMEOUT'}, $valid_values, $install_log_values);
1335
1336   $msg = q{
1337 SMTP SSL mode?};
1338   $msg .= _add_valid_values_disp('SMTP_SSL_MODE', $valid_values);
1339   $config{'SMTP_SSL_MODE'} = _get_value('SMTP_SSL_MODE', $msg, $defaults->{'SMTP_SSL_MODE'}, $valid_values, $install_log_values);
1340
1341   $msg = q{
1342 SMTP user name?};
1343   $config{'SMTP_USER_NAME'} = _get_value('SMTP_USER_NAME', $msg, $defaults->{'SMTP_USER_NAME'}, $valid_values, $install_log_values);
1344
1345   $msg = q{
1346 SMTP password?};
1347   $config{'SMTP_PASSWORD'} = _get_value('SMTP_PASSWORD', $msg, $defaults->{'SMTP_PASSWORD'}, $valid_values, $install_log_values);
1348
1349   $msg = q{
1350 SMTP debug mode?};
1351   $msg .= _add_valid_values_disp('SMTP_DEBUG', $valid_values);
1352   $config{'SMTP_DEBUG'} = _get_value('SMTP_DEBUG', $msg, $defaults->{'SMTP_DEBUG'}, $valid_values, $install_log_values);
1353
1354   $config{'SMTP_DEBUG'} = ( $config{'SMTP_DEBUG'} eq 'yes' ) ? 1 : 0;
1355
1356   $msg = q(
1357 Would you like to run the database-dependent test suite?);
1358   $msg .= _add_valid_values_disp( 'RUN_DATABASE_TESTS', $valid_values );
1359     $config{'RUN_DATABASE_TESTS'} = _get_value( 'RUN_DATABASE_TESTS', $msg, $defaults->{'RUN_DATABASE_TESTS'}, $valid_values, $install_log_values );
1360
1361   if ( $config{'RUN_DATABASE_TESTS'} eq 'yes' ) {
1362       $config{'TEST_DB_TYPE'} = $config{'DB_TYPE'};
1363       $config{'TEST_DB_HOST'} = $config{'DB_HOST'};
1364       $msg = q(TEST DATABASE
1365
1366 THE DATA IN THIS DATABASE WILL BE DESTROYED during the process of
1367 testing. Please don't do this on your production database. It is not
1368 reversible.
1369
1370 YOU WILL SUFFER DATA LOSS if you run this test suite on your test
1371 database. You are better off not running this optional test suite than
1372 doing it in a database that you don't want to lose.
1373
1374 Please specify the name of the test database to be
1375 used by Koha);
1376
1377       $config{'TEST_DB_NAME'} = _get_value('TEST_DB_NAME', $msg, $defaults->{'TEST_DB_NAME'}, $valid_values, $install_log_values);
1378       while ( $config{'TEST_DB_NAME'} eq $config{'DB_NAME'} ) {
1379           $msg = q(Please do not use the same database for testing as you do for production. You run the severe risk of data loss.);
1380           $config{'TEST_DB_NAME'} = _get_value('TEST_DB_NAME', $msg, $defaults->{'TEST_DB_NAME'}, $valid_values, $install_log_values);
1381       }
1382
1383       $msg = q(
1384 Please specify the user that owns the database to be
1385 used by Koha);
1386     $config{'TEST_DB_USER'} = _get_value('TEST_DB_USER', $msg, $defaults->{'TEST_DB_USER'}, $valid_values, $install_log_values);
1387
1388       $msg = q(
1389 Please specify the password of the user that owns the
1390 database to be used by Koha);
1391       $config{'TEST_DB_PASS'} = _get_value('TEST_DB_PASS', $msg, $defaults->{'TEST_DB_PASS'}, $valid_values, $install_log_values);
1392   }
1393
1394     print "\n\n";
1395
1396     # add version number
1397     my $version = "no_version_found";
1398     eval {
1399         require 'kohaversion.pl';
1400         $version = kohaversion();
1401     };
1402     $config{'KOHA_INSTALLED_VERSION'} = $version;
1403
1404     return %config;
1405 }
1406
1407 sub _add_valid_values_disp {
1408     my $key = shift;
1409     my $valid_values = shift;
1410
1411     my $disp = "";
1412     if (exists $valid_values->{$key}) {
1413         $disp = " (" . join(", ", sort keys %{ $valid_values->{$key} }) . ")";
1414     }
1415     return $disp;
1416 }
1417
1418 sub _get_value {
1419     my $key = shift;
1420     my $msg = shift;
1421     my $default = shift;
1422     my $valid_values = shift;
1423     my $install_log_values = shift;
1424
1425     # take value from install log if present
1426     if (exists $install_log_values{$key}) {
1427         $install_log_values{$key} =~ s/\$/\$\$/g;
1428         return $install_log_values{$key};
1429     }
1430
1431     # override default value from environment
1432     if (exists $ENV{$key}) {
1433         $default = $ENV{$key};
1434         $msg .= " (default from environment)";
1435     }
1436
1437     my $val = prompt($msg, $default);
1438
1439     while (exists $valid_values->{$key} and
1440            $val ne $default and
1441            not exists $valid_values->{$key}->{$val}) {
1442         my $retry_msg = "Value '$val' is not a valid option.\n";
1443         $retry_msg .= "Please enter a value";
1444         $retry_msg .= _add_valid_values_disp($key, $valid_values);
1445         $val = prompt($retry_msg, $default);
1446     }
1447     $val =~ s/\$/\$\$/g;
1448     return $val;
1449 }
1450
1451 =head2 get_target_directories
1452
1453 Creates a hash mapping from symbols for installation target
1454 directories to actual directory paths.
1455
1456 Also returns a hash indicating targets for which
1457 files need not be copied -- this is used for the 'dev'
1458 mode installation, where some files are installed in place.
1459
1460 =cut
1461
1462 sub get_target_directories {
1463     my $config = shift;
1464
1465     my $base = $config->{'INSTALL_BASE'};
1466     my $mode = $config->{'INSTALL_MODE'};
1467
1468     # get last component of install base directory
1469     # to treat as package name
1470     my ($volume, $directories, $file) = File::Spec->splitpath($base, 1);
1471
1472     my @basedir = File::Spec->splitdir($directories);
1473
1474     if ( $^O eq 'cygwin' ) { shift @basedir; unshift @basedir, 'c:'; }   # in a cygwin environment, $volume is returned empty
1475
1476     my $package = pop @basedir;
1477
1478
1479     my %dirmap = ();
1480     my %skipdirs = ();
1481     if ($mode eq 'single') {
1482         $dirmap{'API_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'api');
1483         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'cgi-bin');
1484         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs', 'intranet-tmpl');
1485         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs');
1486         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'cgi-bin');
1487         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs', 'opac-tmpl');
1488         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs');
1489         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir(@basedir, $package, 'lib');
1490         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc');
1491         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'zebradb');
1492         $dirmap{'Z3950_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'z3950');
1493         $dirmap{'PAZPAR2_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'pazpar2');
1494         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
1495         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
1496         $dirmap{'SCRIPT_NONDEV_DIR'} = $dirmap{'SCRIPT_DIR'};
1497         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
1498         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
1499         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb');
1500         $dirmap{'LOCK_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lock');
1501         $dirmap{'LOG_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'log');
1502         $dirmap{'BACKUP_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'spool');
1503         $dirmap{'PLUGINS_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'koha', 'plugins');
1504         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'zebradb');
1505         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'run', 'zebradb');
1506     } elsif ($mode eq 'dev') {
1507         my $curdir = File::Spec->rel2abs(File::Spec->curdir());
1508         $dirmap{'API_CGI_DIR'} = File::Spec->catdir($curdir, 'api');
1509         $skipdirs{'API_CGI_DIR'} = 1;
1510         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir($curdir);
1511         $skipdirs{'INTRANET_CGI_DIR'} = 1;
1512         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'intranet-tmpl');
1513         $skipdirs{'INTRANET_TMPL_DIR'} = 1;
1514         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl');
1515         $skipdirs{'INTRANET_WWW_DIR'} = 1;
1516         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir($curdir);
1517         $skipdirs{'OPAC_CGI_DIR'} = 1;
1518         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'opac-tmpl');
1519         $skipdirs{'OPAC_TMPL_DIR'} = 1;
1520         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl');
1521         $skipdirs{'OPAC_WWW_DIR'} = 1;
1522         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir($curdir);
1523         $skipdirs{'PERL_MODULE_DIR'} = 1;
1524         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc');
1525         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'zebradb');
1526         $dirmap{'Z3950_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'z3950');
1527         $dirmap{'PAZPAR2_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'pazpar2');
1528         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
1529         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
1530         #For dev install, point NONDEV_DIR to misc in current dir (not base)
1531         #Used by supportdir in koha-conf.xml (BZ 12031)
1532         $dirmap{'SCRIPT_NONDEV_DIR'} = File::Spec->catdir($curdir, 'misc');
1533         $skipdirs{'SCRIPT_NONDEV_DIR'} = 1;
1534         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
1535         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
1536         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb');
1537         $dirmap{'LOCK_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lock');
1538         $dirmap{'LOG_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'log');
1539         $dirmap{'BACKUP_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'spool');
1540         $dirmap{'PLUGINS_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'plugins');
1541         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'zebradb');
1542         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'run', 'zebradb');
1543     } else {
1544         # mode is standard, i.e., 'fhs'
1545         $dirmap{'API_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'api');
1546         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'cgi-bin');
1547         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs', 'intranet-tmpl');
1548         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs');
1549         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'cgi-bin');
1550         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs', 'opac-tmpl');
1551         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs');
1552         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir(@basedir, $package, 'lib');
1553         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package);
1554         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package, 'zebradb');
1555         $dirmap{'Z3950_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package, 'z3950');
1556         $dirmap{'PAZPAR2_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package, 'pazpar2');
1557         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
1558         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
1559         $dirmap{'SCRIPT_NONDEV_DIR'} = $dirmap{'SCRIPT_DIR'};
1560         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
1561         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
1562         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'var', 'lock', $package, 'zebradb');
1563         $dirmap{'LOCK_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'lock', $package);
1564         $dirmap{'LOG_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'log', $package);
1565         $dirmap{'BACKUP_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'spool', $package);
1566         $dirmap{'PLUGINS_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'lib', $package, 'plugins');
1567         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'lib', $package, 'zebradb');
1568         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'run', $package, 'zebradb');
1569     }
1570
1571     _get_env_overrides(\%dirmap);
1572     _get_argv_overrides(\%dirmap);
1573     _add_destdir(\%dirmap);
1574
1575     return \%dirmap, \%skipdirs;
1576 }
1577
1578 =head2 get_test_dir
1579
1580 Map a directory target to the corresponding path for
1581 the test suite.
1582
1583 =cut
1584
1585 sub get_test_dir {
1586     my ($dirname) = @_;
1587
1588     my @basedir = (File::Spec->rel2abs(File::Spec->curdir()), 't', 'run');
1589     if (exists $test_suite_override_dirs{$dirname}) {
1590         return File::Spec->catdir(@basedir, @{ $test_suite_override_dirs{$dirname} });
1591     } else {
1592         return;
1593     }
1594
1595 }
1596
1597 sub _get_env_overrides {
1598     my $dirmap = shift;
1599
1600     foreach my $key (keys %$dirmap) {
1601         if (exists $ENV{$key}) {
1602             $dirmap->{$key} = $ENV{$key};
1603             print "Setting $key from environment\n";
1604         }
1605     }
1606 }
1607
1608 sub _get_argv_overrides {
1609     my $dirmap = shift;
1610
1611     my @new_argv = ();
1612     for (my $i = 0; $i <= $#ARGV; $i++) {
1613         if ($ARGV[$i] =~ /^([^=]+)=([^=]+)$/ and exists $dirmap->{$1}) {
1614             $dirmap->{$1} = $2;
1615         } else {
1616             push @new_argv, $ARGV[$i];
1617         }
1618     }
1619     @ARGV = @new_argv;
1620 }
1621
1622 sub _strip_destdir {
1623     my $dir = shift;
1624     $dir =~ s/^\$\(DESTDIR\)//;
1625     return $dir;
1626 }
1627
1628 sub _add_destdir {
1629     my $dirmap = shift;
1630
1631     foreach my $key (keys %$dirmap) {
1632         $dirmap->{$key} = '$(DESTDIR)'.$dirmap->{$key};
1633     }
1634 }
1635
1636 sub display_configuration {
1637     my $config = shift;
1638     my $dirmap = shift;
1639     my @version = grep /\/usr\/share\/perl\//, @INC;
1640     push @version, ('/usr/share/perl/5.10') if !$version[0];
1641     print "\n\nKoha will be installed with the following configuration parameters:\n\n";
1642     foreach my $key (sort keys %$config) {
1643         print sprintf("%-25.25s%s\n", $key, $config->{$key});
1644     }
1645
1646     print "\nand in the following directories:\n\n";
1647     foreach my $key (sort keys %$dirmap) {
1648         print sprintf("%-25.25s%s\n", $key, $dirmap->{$key});
1649     }
1650     print "\n\nTo change any configuration setting, please run\n";
1651     print "perl Makefile.PL again.  To override one of the target\n";
1652     print "directories, you can do so on the command line like this:\n";
1653     print "\nperl Makefile.PL PERL_MODULE_DIR=$version[0]\n\n";
1654     print "You can also set different default values for parameters\n";
1655     print "or override directory locations by using environment variables.\n";
1656     print "\nFor example:\n\n";
1657     print "export DB_USER=my_koha\n";
1658     print "perl Makefile.PL\n";
1659     print "\nor\n\n";
1660     print "DB_USER=my_koha DOC_DIR=/usr/local/info perl Makefile.PL\n\n";
1661 }
1662
1663 =head2 find_zebra
1664
1665 Attempt to find Zebra - check user's PATH and
1666 a few other directories for zebrasrv and zebraidx.
1667
1668 =cut
1669
1670 sub find_zebra {
1671     my @search_dirs = map {
1672                             my $abs = File::Spec->rel2abs($_);
1673                             my ($toss, $directories);
1674                             ($toss, $directories, $toss) = File::Spec->splitpath($abs, 1);
1675                             $directories;
1676                         }  split /:/, $ENV{PATH};
1677     push @search_dirs, qw(/usr/local/bin /opt/local/bin /usr/bin);
1678     my @zebrasrv_dirs = grep { -x File::Spec->catpath('', $_, 'zebrasrv') } @search_dirs;
1679     return unless @zebrasrv_dirs;
1680     # verify that directory that contains zebrasrv also contains zebraidx
1681     foreach my $dir (@zebrasrv_dirs) {
1682         return $dir if -x File::Spec->catpath('', $dir, 'zebraidx');
1683     }
1684     return;
1685 }
1686
1687 package MY;
1688
1689 sub test {
1690     my $self = shift;
1691     my $test = $self->SUPER::test(@_);
1692     $test =~ s!\$\(INST_LIB\)!blib/PERL_MODULE_DIR!g;
1693
1694     # set KOHA_CONF
1695     $test =~ s!\$\(FULLPERLRUN\)!KOHA_CONF=blib/KOHA_CONF_DIR/koha-conf.xml \$(FULLPERLRUN)!g;
1696     return $test;
1697 }
1698
1699 sub install {
1700     my $self = shift;
1701     my $install = "";
1702     # NOTE: we're *not* doing this: my $install = $self->SUPER::install(@_);
1703     # This means that we're completely overriding EU::MM's default
1704     # installation and uninstallation targets.
1705
1706 # If installation is on Win32, we need to do permissions different from *nix
1707     if ( $^O =~ /darwin|linux|cygwin|freebsd|solaris/ ) { # this value needs to be verified for each platform and modified accordingly
1708             foreach my $key (sort keys %$target_directories) {
1709                     $install .= qq(
1710 KOHA_INST_$key = blib/$key
1711 KOHA_DEST_$key = $target_directories->{$key}
1712 ) unless exists $skip_directories->{$key};
1713                 }
1714                 $install .= qq(
1715 install :: all install_koha set_koha_ownership set_koha_permissions warn_koha_env_vars
1716 \t\$(NOECHO) \$(NOOP)
1717 );
1718                         $install .= "install_koha ::\n";
1719                         $install .= "\t\$(NOECHO) umask 022; \$(MOD_INSTALL) \\\n";
1720                         foreach my $key (sort keys %$target_directories) {
1721                                 $install .= "\t\t\$(KOHA_INST_$key) \$(KOHA_DEST_$key) \\\n"
1722           unless exists $skip_directories->{$key};
1723                         }
1724                         $install .= "\t\t\$(INST_MAN1DIR) \$(DESTINSTALLMAN1DIR) \\\n";
1725                         $install .= "\t\t\$(INST_MAN3DIR) \$(DESTINSTALLMAN3DIR)\n";
1726
1727                         $install .= "\n";
1728                         $install .= "set_koha_ownership ::\n";
1729 # Do not try to change ownership if DESTDIR is set
1730                         if ($config{'INSTALL_MODE'} eq 'standard' and $config{'KOHA_USER'} ne "root") {
1731                                 foreach my $key (sort keys %$target_directories) {
1732             $install .= "\t\$(NOECHO) if test -z \"\$(DESTDIR)\"; then chown -R $config{'KOHA_USER'}:$config{'KOHA_GROUP'} \$(KOHA_DEST_$key); fi\n"
1733             unless exists $skip_directories->{$key};
1734                                 }
1735                         } else {
1736                                 $install .= "\t\t\$(NOECHO) \$(NOOP)\n\n";
1737                         }
1738
1739                         $install .= "\n";
1740                         $install .= "set_koha_permissions ::\n";
1741                         # This is necessary because EU::MM installs files
1742                         # as either 0444 or 0555, and we want the owner
1743                         # of Koha's files to have write permission by default.
1744                         foreach my $key (sort keys %$target_directories) {
1745                                 $install .= "\t\$(NOECHO) chmod -R u+w \$(KOHA_DEST_$key)\n"
1746           unless exists $skip_directories->{$key};
1747                         }
1748         }
1749
1750         $install .= "\n";
1751
1752     $install .= "warn_koha_env_vars ::\n";
1753     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1754     $install .= "\t\$(NOECHO) \$(ECHO) Koha\\'s files have now been installed. \n";
1755     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1756     $install .= "\t\$(NOECHO) \$(ECHO) In order to use Koha\\'s command-line batch jobs,\n";
1757     $install .= "\t\$(NOECHO) \$(ECHO) you should set the following environment variables:\n";
1758     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1759     $install .= "\t\$(NOECHO) \$(ECHO) export KOHA_CONF=\$(KOHA_DEST_KOHA_CONF_DIR)/koha-conf.xml\n";
1760     $install .= "\t\$(NOECHO) \$(ECHO) export PERL5LIB=$target_directories->{'PERL_MODULE_DIR'}\n";
1761     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1762     $install .= "\t\$(NOECHO) \$(ECHO) For other post-installation tasks, please consult the README.\n";
1763     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1764
1765     $install .= _update_zebra_conf_target();
1766
1767     $install .= upgrade();
1768
1769     return $install;
1770 }
1771
1772 =head2 _update_zebra_conf_target
1773
1774 Add an installation target for updating
1775 Zebra's configuration files.
1776
1777 =cut
1778
1779 sub _update_zebra_conf_target {
1780
1781     my $target = "\nupdate_zebra_conf ::\n";
1782     $target .= "\tumask 022; \$(MOD_INSTALL) \\\n";
1783     $target .= "\t\t\$(KOHA_INST_ZEBRA_CONF_DIR) \$(KOHA_DEST_ZEBRA_CONF_DIR) \n";
1784     $target .= "\t\$(NOECHO) chmod -R u+w \$(KOHA_DEST_ZEBRA_CONF_DIR)\n";
1785     $target .= "\tumask 022; \$(MOD_INSTALL) \\\n";
1786     $target .= "\t\t\$(KOHA_INST_PAZPAR2_CONF_DIR) \$(KOHA_DEST_PAZPAR2_CONF_DIR) \n";
1787     $target .= "\t\$(NOECHO) chmod -R u+w \$(KOHA_DEST_PAZPAR2_CONF_DIR)\n";
1788
1789     return $target;
1790 }
1791
1792 sub upgrade {
1793     my $upgrade = "";
1794
1795     my $backup_suffix;
1796     if (exists $install_log_values{'KOHA_INSTALLED_VERSION'}) {
1797         my $version = $install_log_values{'KOHA_INSTALLED_VERSION'};
1798         $version =~ s/\./_/g;
1799         $backup_suffix = "_koha_$version";
1800     } else {
1801         $backup_suffix = "_upgrade_backup";
1802     }
1803
1804     $upgrade .= qq/
1805 MOD_BACKUP = \$(ABSPERLRUN) -MC4::Installer::UpgradeBackup -e 'backup_changed_files({\@ARGV}, '$backup_suffix', '\''\$(VERBINST)'\'', '\''\$(UNINST)'\'');' --
1806
1807 upgrade :: make_upgrade_backup install
1808 \t\$(NOECHO) \$(NOOP)
1809 make_upgrade_backup ::
1810 \t\$(NOECHO) umask 022; \$(MOD_BACKUP) \\
1811 /;
1812     foreach my $key (qw/KOHA_CONF_DIR INTRANET_TMPL_DIR INTRANET_WWW_DIR OPAC_TMPL_DIR OPAC_WWW_DIR
1813                      PAZPAR2_CONF_DIR ZEBRA_CONF_DIR PLUGINS_DIR/) {
1814         $upgrade .= "\t\t\$(KOHA_INST_$key) \$(KOHA_DEST_$key) \\\n"
1815             unless exists $skip_directories->{$key} or
1816                    not exists $target_directories->{$key};
1817     }
1818     $upgrade =~ s/\\\n$/\n/;
1819
1820     return $upgrade;
1821 }
1822 sub postamble {
1823     # put directory mappings into Makefile
1824     # so that Make will export as environment
1825     # variables -- this is for the use of
1826     # rewrite-confg.PL
1827
1828     my $env;
1829         # Hereagain, we must alter syntax per platform...
1830         {
1831                 $env = join("\n", map { "export __${_}__ := $target_directories->{$_}" } keys %$target_directories);
1832                 $env .= "\n\n";
1833                 $env .= join("\n", map { "export __${_}__ := $config{$_}" } keys %config);
1834         }
1835
1836     return "$env\n";
1837 }
1838
1839
1840 __END__
1841
1842
1843 =head1 SEE ALSO
1844
1845 ExtUtils::MakeMaker(3)
1846
1847 =head1 AUTHORS
1848
1849 MJ Ray mjr at phonecoop.coop
1850 Galen Charlton galen.charlton at liblime.com
1851
1852 =cut
1853
1854 FIXME: deal with .htaccess