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