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