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