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