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