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