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