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