installer: started on Zebra paths
[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 EXAMPLE_DIR
182
183 Directory for example configuration files.  This directory
184 exists primarily to make it easier to change the
185 MARC format or language of the active Zebra
186 indexes.
187
188 =item SCRIPT_DIR
189
190 Directory for command-line scripts and daemons.
191
192 =item MAN_DIR
193
194 Directory for man pages created from POD -- will mostly
195 contain information of interest to Koha developers.
196
197 =item DOC_DIR
198
199 Directory for Koha documentation accessed from the 
200 command-line, e.g., READMEs.
201
202 =item LOG_DIR
203
204 Directory for Apache and Zebra logs produced by Koha.
205
206 =item NONE
207
208 This is a dummy target used to explicitly state
209 that a given file or directory is not to be installed.
210 This is used either for parts of the installer itself
211 or for development tools that are not applicable to a
212 production installation.
213
214 =back
215
216 =cut
217
218 my $target_map = {
219   './about.pl'                  => 'INTRANET_CGI_DIR',
220   './acqui'                     => 'INTRANET_CGI_DIR',
221   './admin'                     => 'INTRANET_CGI_DIR',
222   './authorities'               => 'INTRANET_CGI_DIR',
223   './C4'                        => 'PERL_MODULE_DIR',
224   './C4/SIP/t'                  => 'NONE',
225   './C4/SIP/koha_test'          => 'NONE',
226   './C4/tests'                  => 'NONE',
227   './catalogue'                 => 'INTRANET_CGI_DIR',
228   './cataloguing'               => 'INTRANET_CGI_DIR',
229   './changelanguage.pl'         => 'INTRANET_CGI_DIR',
230   './check_sysprefs.pl'         => 'NONE',
231   './circ'                      => 'INTRANET_CGI_DIR',
232   './edithelp.pl'               => 'INTRANET_CGI_DIR',
233   './etc'                       => { target => 'KOHA_CONF_DIR', trimdir => -1 },
234   './etc/zebradb'               => { target => 'ZEBRA_CONF_DIR', trimdir => -1 },
235   './etc/zebradb/authorities/lock' => { target => 'ZEBRA_LOCK_DIR', trimdir => 3 },
236   './etc/zebradb/authorities/key'  => { target => 'ZEBRA_DATA_DIR', trimdir => 3 },
237   './etc/zebradb/authorities/register'  => { target => 'ZEBRA_DATA_DIR', trimdir => 3 },
238   './etc/zebradb/authorities/shadow'  => { target => 'ZEBRA_DATA_DIR', trimdir => 3 },
239   './etc/zebradb/authorities/tmp'  => { target => 'ZEBRA_DATA_DIR', trimdir => 3 },
240   './etc/zebradb/biblios/lock' => { target => 'ZEBRA_LOCK_DIR', trimdir => 3 },
241   './etc/zebradb/biblios/key'  => { target => 'ZEBRA_DATA_DIR', trimdir => 3 },
242   './etc/zebradb/biblios/register'  => { target => 'ZEBRA_DATA_DIR', trimdir => 3 },
243   './etc/zebradb/biblios/shadow'  => { target => 'ZEBRA_DATA_DIR', trimdir => 3 },
244   './etc/zebradb/biblios/tmp'  => { target => 'ZEBRA_DATA_DIR', trimdir => 3 },
245   './help.pl'                   => 'NONE', # FIXME
246   './installer-CPAN.pl'         => 'NONE',
247   './installer'                 => 'INTRANET_CGI_DIR',
248   './koha-tmpl/errors'          => {target => 'INTRANET_CGI_DIR', trimdir => 2},
249   './koha-tmpl/intranet-tmpl'   => {target => 'INTRANET_TMPL_DIR', trimdir => -1},
250   './koha-tmpl/opac-tmpl'       => {target => 'OPAC_TMPL_DIR', trimdir => -1},
251   './kohaversion.pl'            => 'INTRANET_CGI_DIR', # FIXME this may need to be in OPAC_CGI_DIR as well, with an update to C4::Context
252   './labels'                    => 'INTRANET_CGI_DIR',
253   './mainpage.pl'               => 'INTRANET_CGI_DIR',
254   './Makefile.PL'               => 'NONE',
255   './MANIFEST.SKIP'             => 'NONE',
256   './members'                   => 'INTRANET_CGI_DIR',
257   './misc'                      => { target => 'SCRIPT_DIR', trimdir => -1 }, 
258   './misc/info'                 => { target => 'DOC_DIR', trimdir => 2 },
259   './misc/release notes'        => { target => 'DOC_DIR', trimdir => 2 },
260   './misc/translator'           => { target => 'EXAMPLE_DIR', trimdir => 2 }, 
261   './misc/installer_devel_notes' => 'NONE',
262   './opac'                      => 'OPAC_CGI_DIR',
263   './README.txt'                => 'NONE',
264   './reports'                   => 'INTRANET_CGI_DIR',
265   './reserve'                   => 'INTRANET_CGI_DIR',
266   './reviews'                   => 'INTRANET_CGI_DIR',
267   './rewrite-config.PL'         => 'NONE',
268   './reviews'                   => 'INTRANET_CGI_DIR',
269   './rss'                       => 'NONE', # FIXME deal with a little later
270   './serials'                   => 'INTRANET_CGI_DIR',
271   './sms'                       => 'INTRANET_CGI_DIR',
272   './suggestion'                => 'INTRANET_CGI_DIR',
273   './svc'                       => 'INTRANET_CGI_DIR',
274   './t'                         => 'NONE',
275   './tmp'                       => 'NONE', # FIXME deal with later
276   './tools'                     => 'INTRANET_CGI_DIR',
277   './virtualshelves'            => 'INTRANET_CGI_DIR',
278   # ignore files and directories created by the install itself
279   './pm_to_blib'                => 'NONE',
280   './blib'                      => 'NONE',
281 };
282
283 =head1 CONFIGURATION OPTIONS
284
285 The following configuration options are used by the installer.
286
287 =over 4
288
289 =item INSTALL_MODE
290
291 Specifies whether installation will be FHS-compliant (default,
292 assumes user has root), put everything under
293 a single directory (for users installing on a web host
294 that allows CGI scripts and a MySQL database but not root 
295 access), or development (for a developer who wants to run
296 Koha from a git clone with no fuss).
297
298 =item INSTALL_BASE
299
300 Directory under which most components will go.  Default
301 value will vary depending on INSTALL_MODE.
302
303 =item INSTALL_ZEBRA
304
305 Whether to install Zebra configuration files and data
306 directories.
307
308 =item ZEBRA_MARC_FORMAT
309
310 Specifies format of MARC records to be indexed by Zebra.
311
312 =item ZEBRA_LANGUAGE
313
314 Specifies primary language of records that will be 
315 indexed by Zebra.
316
317 =back
318
319 =cut
320
321 # default configuration options
322 my %config_defaults = (
323   'INSTALL_MODE'      => 'standard',
324   'INSTALL_BASE'      => '/usr/share/koha',
325   'DB_TYPE'           => 'mysql',
326   'DB_HOST'           => 'localhost',
327   'DB_NAME'           => 'koha',    
328   'DB_USER'           => 'kohaadmin',
329   'DB_PASS'           => 'katikoan',
330   'INSTALL_ZEBRA'     => 'yes',
331   'ZEBRA_MARC_FORMAT' => 'marc21',
332   'ZEBRA_LANGUAGE'    => 'en',
333   'ZEBRA_USER'        => 'kohauser',
334   'ZEBRA_PASS'        => 'zebrastripes',
335 );
336
337 # valid values for certain configuration options
338 my %valid_config_values = (
339   'INSTALL_MODE'  => { 'standard' => 1, 'single' => 1, 'dev' => 1 },
340   'DB_TYPE' => { 'mysql' => 1, 'Pg' => 1 },
341   'INSTALL_ZEBRA' => { 'yes' => 1, 'no' => 1 },
342   'ZEBRA_MARC_FORMAT' => { 'marc21' => 1, 'unimarc' => 1 }, # FIXME should generate from contents of distributation
343   'ZEBRA_LANGUAGE'    => { 'en' => 1, 'fr' => 1 }, # FIXME should generate from contents of distribution
344 );
345
346 my %config = get_configuration(\%config_defaults, \%valid_config_values);
347 my %target_directories = get_target_directories(\%config);
348 display_configuration(\%config, \%target_directories);
349 my $file_map = {};
350 get_file_map($target_map, $dirtree, $file_map);
351
352 WriteMakefile(
353     NAME => 'koha',
354     #VERSION => strftime('2.9.%Y%m%d%H',gmtime),
355     VERSION_FROM => 'C4/Context.pm',
356     ABSTRACT => 'Award-winning integrated library system (ILS) and Web OPAC',
357     AUTHOR => 'Koha Developers <koha-devel@nongnu.org>',
358     NO_META => 1,
359     PREREQ_PM => {
360 'CGI' => 3.15,
361 'CGI::Carp' => 1.29,
362 'CGI::Session' => '4.10',
363 'Class::Factory::Util' => 1.7,
364 'Class::Accessor' => 0.30,
365 'DBD::mysql' => 3.0008,
366 'DBI' => 1.53,
367 'Data::Dumper' => 2.121_08,
368 'Date::Calc' => 5.4,
369 'Date::Manip' => 5.44,
370 'Digest::MD5' => 2.36,
371 'File::Temp' => 0.16,
372 'GD::Barcode::UPCE' => 1.1,
373 'Getopt::Long' => 2.35,
374 'Getopt::Std' => 1.05,
375 'HTML::Template::Pro' => 0.65,
376 'HTTP::Cookies' => 1.39,
377 'HTTP::Request::Common' => 1.26,
378 'LWP::Simple' => 1.41,
379 'LWP::UserAgent' => 2.033,
380 'Lingua::Stem' => 0.82,
381 'List::Util' => 1.18,
382 'Locale::Language' => 2.07,
383 'MARC::Charset' => 0.98,
384 'MARC::Crosswalk::DublinCore' => 0.03,
385 'MARC::File::XML' => 0.88,
386 'MARC::Record' => 2.00,
387 'MIME::Base64' => 3.07,
388 'MIME::QuotedPrint' => 3.07,
389 'Mail::Sendmail' => 0.79,
390 'PDF::API2' => 2.000,
391 'PDF::API2::Page' => 2.000,
392 'PDF::API2::Util' => 2.000,
393 'PDF::Reuse' => 0.33,
394 'PDF::Reuse::Barcode' => 0.05,
395 'POSIX' => 1.09,
396 'Schedule::At' => 1.06,
397 'Term::ANSIColor' => 1.10,
398 'Test' => 1.25,
399 'Test::Harness' => 2.56,
400 'Test::More' => 0.62,
401 'Text::CSV' => 0.01,
402 'Text::CSV_XS' => 0.32,
403 'Text::Wrap' => 2005.082401,
404 'Time::HiRes' => 1.86,
405 'Time::localtime' => 1.02,
406 'Unicode::Normalize' => 0.32,
407 'XML::Dumper' => 0.81,
408 'XML::LibXML' => 1.59,
409 'XML::SAX::ParserFactory' => 1.01,
410 'XML::Simple' => 2.14,
411 'XML::RSS' => 1.31,
412 'ZOOM' => 1.16,
413         },
414
415         # File tree mapping
416         PM => $file_map,
417
418     # Man pages generated from POD
419     INSTALLMAN1DIR => File::Spec->catdir($target_directories{'MAN_DIR'}, 'man1'),
420     INSTALLMAN3DIR => File::Spec->catdir($target_directories{'MAN_DIR'}, 'man3'),
421
422 #   CONFIGURE => sub {
423 #     # Ask for options with prompt($question,$default) calls here?
424 #     return { macro => { 'export TEST' => '755' } }
425 #     },
426
427    PL_FILES => { # generator => target(s)
428       'rewrite-config.PL' => [
429          'blib/KOHA_CONF_DIR/koha-conf.xml',
430          'blib/KOHA_CONF_DIR/koha-httpd.conf',
431          'blib/ZEBRA_CONF_DIR/etc/passwd',
432          'blib/ZEBRA_CONF_DIR/zebra-biblios.cfg',
433          'blib/ZEBRA_CONF_DIR/zebra-authorities.cfg'
434          ]
435    }
436 #     'opac/getfromintranet.PL' => ['$(INST_LIBDIR)/opac/cgi-bin/detail.pl','$(INST_LIBDIR)/opac/cgi-bin/moredetail.pl','$(INST_LIBDIR)/opac/cgi-bin/search.pl','$(INST_LIBDIR)/opac/cgi-bin/subjectsearch.pl','$(INST_LIBDIR)/opac/cgi-bin/logout.pl'],
437 #     'misc/koha.conf.PL' => '$(INST_LIBDIR)/../etc/koha.conf',
438 #     'misc/apache-koha.conf.PL' => '$(INST_LIBDIR)/../etc/apache-koha.conf',
439 #     'misc/koha.sql.PL' => '$(INST_LIBDIR)/intranet/scripts/koha.sql',
440 #     'z3950/z3950-daemon-options.PL' => '$(INST_LIBDIR)/intranet/scripts/z3950daemon/z3950-daemon-options',
441 #     # fake target to check permissions
442 #     'misc/chmod.PL' => '$(INST_LIBDIR)/fake-target'
443 #     }
444    # need to set ownerships
445    # need to load koha.sql
446    # need to link koha-httpd.conf
447    # need to start z3950-daemon
448 );
449
450 =head1 FUNCTIONS
451
452 =head2 hashdir
453
454 This function recurses through the directory structure and builds
455 a hash of hashes containing the structure with arrays holding filenames.
456 This directory hashing routine was taken from BrowserUK @ http://www.perlmonks.org/?node_id=219919
457
458 =cut
459
460 sub hashdir{
461     my $dir = shift;
462     opendir my $dh, $dir or die $!;
463     my $tree = {}->{$dir} = {};
464     while( my $file = readdir($dh) ) {
465         next if $file =~ m/^\.{1,2}/ and $file !~ /^\.htaccess/; # .htaccess is a special case
466         my $path = $dir .'/' . $file;
467         $tree->{$file} = hashdir($path), next if -d $path;
468         push @{$tree->{'.'}}, $file;
469     }
470     return $tree;
471 }
472
473 =head2 get_file_map 
474
475 This function combines the target_map and file hash to
476 map each source file to its destination relative to
477 the set of installation targets.
478
479 Output will be a hash mapping from each source file
480 to its destination value, like this:
481
482 'mainpage.pl' => '$(INTRANET_CGI_DIR)/mainpage.pl'
483
484 =cut
485
486 sub get_file_map {
487     my $target_map = shift;
488     my $dirtree = shift;
489     my $file_map = shift;
490     my $curr_path = @_ ? shift : ['.'];
491
492     # Traverse the directory tree.
493     # For each file or directory, identify the
494     # most specific match in the target_map
495     foreach my $dir (sort keys %{ $dirtree }) {
496         if ($dir eq '.') {
497             # deal with files in directory
498             foreach my $file (sort @{ $dirtree->{$dir} }) {
499                 my $targetdir = undef;
500                 my $matchlevel = undef;
501                 # first, see if there is a match on this specific
502                 # file in the target map
503                 my $filepath = join("/", @$curr_path, $file);
504                 if (exists $target_map->{$filepath}) {
505                     $targetdir = $target_map->{$filepath};
506                     $matchlevel = scalar(@$curr_path) + 1;
507                 } else {
508                     # no match on the specific file; look for
509                     # a directory match
510                     for (my $i = scalar(@$curr_path) - 1; $i >= 0; $i--)  {
511                         my $dirpath = join("/", @$curr_path[0..$i]);
512                         if (exists $target_map->{$dirpath}) {
513                             $targetdir = $target_map->{$dirpath};
514                             $matchlevel = $i + 1;
515                             last;
516                         }
517                     }
518                 }
519                 if (defined $targetdir) {
520                      _add_to_file_map($file_map, $targetdir, $curr_path, $file, $matchlevel);
521                 } else {
522                     my $path = join("/", @$curr_path);
523                     print "failed to map: $path/$file\n" if $DEBUG;
524                 }
525             }
526         } else {
527             # dealing with subdirectory
528             push @$curr_path, $dir;
529             get_file_map($target_map, $dirtree->{$dir}, $file_map, $curr_path);
530             pop @$curr_path;
531         }
532     }
533 }
534
535 sub _add_to_file_map {
536     my $file_map = shift;
537     my $targetdir = shift;
538     my $curr_path = shift;
539     my $file = shift;
540     my $matchlevel = shift;
541     my $dest_path = @_ ? shift : $curr_path;
542
543     # The target can be one of the following:
544     # 1. scalar representing target symbol
545     # 2. hash ref containing target and trimdir keys
546     #
547     # Consequently, this routine traverses this structure,
548     # calling itself recursively, until it deals with
549     # all of the scalar target symbols.
550     if (ref $targetdir eq 'HASH') {
551         my $subtarget = $targetdir->{target};
552         if (exists $targetdir->{trimdir}) {
553             # if we get here, we've specified that
554             # rather than installing the file to
555             # $(TARGET)/matching/dirs/subdirs/file,
556             # we want to install it to
557             # $(TARGET)/subdirs/file
558             #
559             # Note that this the only place where
560             # $matchlevel is used.
561             my @new_dest_path = @$dest_path;
562             if ($targetdir->{trimdir} == -1)  {
563                 splice @new_dest_path, 0, $matchlevel;
564             } else {
565                 splice @new_dest_path, 0, $targetdir->{trimdir};
566             }
567             _add_to_file_map($file_map, $subtarget, $curr_path, $file, $matchlevel, \@new_dest_path);
568         } else {
569             # actually getting here means that the
570             # target was unnecessarily listed
571             # as a hash, but we'll forgive that
572             _add_to_file_map($file_map, $subtarget, $curr_path, $file, $matchlevel);
573         }
574     } elsif ($targetdir ne 'NONE' and $targetdir ne '') {
575         my $source = File::Spec->catfile(@$curr_path, $file);
576         my $destination = File::Spec->catfile('blib', $targetdir, @$dest_path, $file);
577         #print "$source => $destination\n"; # DEBUG
578         # quote spaces in file names
579         # FIXME: this is of questionable portability and
580         # probably depends on user's make recognizing this
581         # quoting syntax -- probably better to remove
582         # spaces and shell metacharacters from all file names
583         $source =~ s/ /\\ /g;
584         $destination =~ s/ /\\ /g;
585        
586         $file_map->{$source} = $destination;
587     }
588 }
589
590 =head2 get_configuration_options
591
592 This prompts the user for various configuration options.
593
594 =cut
595
596 sub get_configuration {
597   my $defaults = shift;
598   my $valid_values = shift;
599   my %config = ();
600
601   my $msg = q(
602 By default, Koha can be installed in one of three ways:
603
604 standard: Install files in conformance with the Filesystem
605           Hierarchy Standard (FHS).  This is the default mode
606           and should be used when installing a production
607           Koha system.  On Unix systems, root access is 
608           needed to complete a standard installation.
609
610 single:   Install files under a single directory.  This option
611           is useful for installing Koha without root access, e.g.,
612           on a web host that allows CGI scripts and MySQL databases
613           but requires the user to keep all files under the user's
614           HOME directory.
615
616 dev:      Create a set of symbolic links and configuration files to
617           allow Koha to run directly from the source distribution.
618           This mode is useful for developers who want to run
619           Koha from a git clone.
620
621 Please choose the installation mode);
622     $msg .= _add_valid_values_disp('INSTALL_MODE', $valid_values);
623     $config{'INSTALL_MODE'} = _get_value('INSTALL_MODE', $msg, $defaults->{'INSTALL_MODE'}, $valid_values);
624
625     # set message and default value for INSTALL_BASE
626     # depending on value of INSTALL_MODE
627     my $install_base_default = $defaults->{'INSTALL_BASE'};
628     if ($config{'INSTALL_MODE'} eq 'dev') {
629         $msg = q(
630 Please specify the directory in which to install Koha's
631 active configuration files and (if applicable) the
632 Zebra database.  Koha's CGI scripts and templates will
633 be run from the current directory.);
634         # FIXME - home directory portability consideration apply
635         $install_base_default = (exists $ENV{'HOME'}) ? "$ENV{'HOME'}/koha-dev" : "/usr/share/koha-dev";
636     } elsif ($config{'INSTALL_MODE'} eq 'single') {
637         $msg = "\nPlease specify the directory in which to install Koha";
638         # FIXME -- we're assuming under a 'single' mode install
639         # that user will likely want to install under the home
640         # directory.  This is OK in and of itself, but we should
641         # use File::HomeDir to locate the home directory portably.  
642         # This is deferred for now because File::HomeDir is not yet
643         # core.
644         $install_base_default = (exists $ENV{'HOME'}) ? "$ENV{'HOME'}/koha" : "/usr/share/koha";
645     } else {
646         # must be standard
647         $msg = q(
648 Please specify the directory under which most Koha files 
649 will be installed.
650
651 Note that if you are planning in installing more than 
652 one instance of Koha, you may want to modify the last
653 component of the directory path, which will be used
654 as the package name in the FHS layout.);
655     }
656     $config{'INSTALL_BASE'} = _get_value('INSTALL_BASE', $msg, $install_base_default, $valid_values);
657     $config{'INSTALL_BASE'} = File::Spec->rel2abs($config{'INSTALL_BASE'});
658
659     $msg = q(
660 Please specify which database engine you will use
661 to store data in Koha.  The choices are MySQL and
662 PostgreSQL; please note that at the moment
663 PostgreSQL support is highly experimental.
664
665 DBMS to use);
666     $msg .= _add_valid_values_disp('DB_TYPE', $valid_values);
667     $config{'DB_TYPE'} = _get_value('DB_TYPE', $msg, $defaults->{'DB_TYPE'}, $valid_values);
668
669     $msg = q(
670 Please specify the name or address of your 
671 database server.  Note that the database 
672 does not have to exist at this point, it
673 can be created after running 'make install'
674 and before you try using Koha for the first time.
675
676 Database server);
677     $config{'DB_HOST'} = _get_value('DB_HOST', $msg, $defaults->{'DB_HOST'}, $valid_values);
678
679     $msg = q(
680 Please specify the port used to connect to the
681 DMBS);
682     my $db_port_default = $config{'DB_TYPE'} eq 'mysql' ? '3306' : '5432';
683     $config{'DB_PORT'} = _get_value('DB_PORT', $msg, $db_port_default, $valid_values);
684
685     $msg = q(
686 Please specify the name of the database to be
687 used by Koha);
688     $config{'DB_NAME'} = _get_value('DB_NAME', $msg, $defaults->{'DB_NAME'}, $valid_values);
689
690     $msg = q(
691 Please specify the user that owns the database to be
692 used by Koha);
693     $config{'DB_USER'} = _get_value('DB_USER', $msg, $defaults->{'DB_USER'}, $valid_values);
694
695     $msg = q(
696 Please specify the password of the user that owns the 
697 database to be used by Koha);
698     $config{'DB_PASS'} = _get_value('DB_PASS', $msg, $defaults->{'DB_PASS'}, $valid_values);
699
700     $msg = q(
701 Koha can use the Zebra search engine for high-performance
702 searching of bibliographic and authority records.  If you
703 have installed the Zebra software and would like to use it,
704 please answer 'yes' to the following question.  Otherwise, 
705 Koha will default to using its internal search engine.
706
707 Please specify whether to install the Zebra configuration files);
708     $msg .= _add_valid_values_disp('INSTALL_ZEBRA', $valid_values);
709     $config{'INSTALL_ZEBRA'} = _get_value('INSTALL_ZEBRA', $msg, $defaults->{'INSTALL_ZEBRA'}, $valid_values);
710
711     if ($config{'INSTALL_ZEBRA'} eq 'yes') {
712         $msg = q(
713 Since you've chosen to use Zebra with Koha,
714 you must specify the primary MARC format of the
715 records to be indexed by Zebra.
716
717 Koha provides Zebra configuration files for MARC 21
718 and UNIMARC.
719
720 Please specify the MARC format);
721         $msg .= _add_valid_values_disp('ZEBRA_MARC_FORMAT', $valid_values);
722         $config{'ZEBRA_MARC_FORMAT'} = _get_value('ZEBRA_MARC_FORMAT', $msg, $defaults->{'ZEBRA_MARC_FORMAT'}, $valid_values);
723         $msg = q(
724 Koha supplies Zebra configuration files tuned for
725 searching either English (en) or French (fr) MARC
726 records.
727
728 Please specify the primary language of the MARC records);
729         $msg .= _add_valid_values_disp('ZEBRA_LANGUAGE', $valid_values);
730         $config{'ZEBRA_LANGUAGE'} = _get_value('ZEBRA_LANGUAGE', $msg, $defaults->{'ZEBRA_LANGUAGE'}, $valid_values);
731
732         $msg = q(
733 Please specify Zebra database user);
734         $config{'ZEBRA_USER'} = _get_value('ZEBRA_USER', $msg, $defaults->{'ZEBRA_USER'}, $valid_values);
735
736         $msg = q(
737 Please specify the Zebra database password);
738         $config{'ZEBRA_PASS'} = _get_value('ZEBRA_PASS', $msg, $defaults->{'ZEBRA_PASS'}, $valid_values);
739
740     }
741     return %config;
742 }
743
744 sub _add_valid_values_disp {
745     my $key = shift;
746     my $valid_values = shift;
747    
748     my $disp = "";
749     if (exists $valid_values->{$key}) {
750         $disp = " (" . join(", ", sort keys %{ $valid_values->{$key} }) . ")";
751     }
752     return $disp;
753 }
754
755 sub _get_value {
756     my $key = shift;
757     my $msg = shift;
758     my $default = shift;
759     my $valid_values = shift;
760
761     my $val = prompt($msg, $default);
762     while (exists $valid_values->{$key} and not exists $valid_values->{$key}->{$val}) {
763         my $retry_msg = "Value '$val' is not a valid option.\n";
764         $retry_msg .= "Please enter a value";
765         $retry_msg .= _add_valid_values_disp($key, $valid_values);
766         $val = prompt($retry_msg, $default);
767     }
768     return $val;
769 }
770
771 =head2 get_target_directories 
772
773 Creates a hash mapping from symbols for installation target
774 directories to actual directory paths.
775
776 =cut
777
778 sub get_target_directories {
779     my $config = shift;
780
781     my $base = $config->{'INSTALL_BASE'};
782     my $mode = $config->{'INSTALL_MODE'};
783
784     # get last component of install base directory
785     # to treat as package name
786     my ($volume, $directories, $file) = File::Spec->splitpath($base, 1);
787     my @basedir = File::Spec->splitdir($directories);
788     my $package = pop @basedir;
789
790     my %dirmap = ();
791     if ($mode eq 'single') {
792         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'cgi-bin');
793         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs', 'intranet-tmpl');
794         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs');
795         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'cgi-bin');
796         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs', 'opac-tmpl');
797         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs');
798         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir(@basedir, $package, 'lib');
799         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc');
800         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'zebradb');
801         $dirmap{'EXAMPLE_DIR'} = File::Spec->catdir(@basedir, $package, 'example');
802         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
803         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
804         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
805         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb');
806         $dirmap{'LOG_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'log');
807         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'zebradb');
808         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'run', 'zebradb');
809     } elsif ($mode eq 'dev') {
810         my $curdir = File::Spec->rel2abs(File::Spec->curdir());
811         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir($curdir);
812         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'intranet-tmpl');
813         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'intranet-tmpl');
814         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir($curdir, 'opac');
815         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'opac-tmpl');
816         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'opac-tmpl');
817         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir($curdir);
818         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc');
819         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'zebradb');
820         $dirmap{'EXAMPLE_DIR'} = File::Spec->catdir(@basedir, $package, 'example');
821         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
822         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
823         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
824         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb');
825         $dirmap{'LOG_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'log');
826         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'zebradb');
827         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'run', 'zebradb');
828     } else {
829         # mode is standard, i.e., 'fhs'
830         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'cgi-bin');
831         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs', 'intranet-tmpl');
832         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs');
833         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'cgi-bin');
834         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs', 'opac-tmpl');
835         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs');
836         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir(@basedir, $package, 'lib');
837         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package);
838         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package, 'zebradb');
839         $dirmap{'EXAMPLE_DIR'} = File::Spec->catdir(@basedir, $package, 'example');
840         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
841         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
842         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
843         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'var', 'lock', $package, 'zebradb');
844         $dirmap{'LOG_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'log', $package);
845         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'lib', $package, 'zebradb');
846         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'run', $package, 'zebradb');
847     }
848
849     _get_argv_overrides(\%dirmap);
850
851     return %dirmap;
852 }
853
854 sub _get_argv_overrides {
855     my $dirmap = shift;
856     
857     my @new_argv = ();
858     for (my $i = 0; $i <= $#ARGV; $i++) {
859         if ($ARGV[$i] =~ /^([^=]+)=([^=]+)$/ and exists $dirmap->{$1}) {
860             $dirmap->{$1} = $2;
861         } else {
862             push @new_argv, $ARGV[$i];
863         }
864     }
865     @ARGV = @new_argv;
866 }
867
868 sub display_configuration {
869     my $config = shift;
870     my $dirmap = shift;
871     print "\n\nKoha will be installed with the following configuration parameters:\n\n";
872     foreach my $key (sort keys %$config) {
873         print sprintf("%-25.25s%s\n", $key, $config->{$key});
874     }
875
876     print "\nand in the following directories:\n\n";
877     foreach my $key (sort keys %$dirmap) {
878         print sprintf("%-25.25s%s\n", $key, $dirmap->{$key});
879     }
880     print "\n\nTo change any configuration setting, please run\n";
881     print "perl Makefile.PL again.  To override one of the target\n";
882     print "directories, you can do so on the command line like this:\n";
883     print "\nperl Makefile.PL PERL_MODULE_DIR=/usr/share/perl/5.8\n\n";
884 }
885
886 package MY;
887
888 sub test {
889     my $self = shift;
890     my $test = $self->SUPER::test(@_);
891     $test =~ s!\$\(INST_LIB\)!blib/PERL_MODULE_DIR!g;
892     return $test;
893 }
894
895 sub install {
896     my $self = shift;
897     my $install = ""; 
898     # NOTE: we're *not* doing this: my $install = $self->SUPER::install(@_);
899     # This means that we're completely overriding EU::MM's default
900     # installation and uninstallation targets.
901     foreach my $key (sort keys %target_directories) {
902         $install .= qq(
903 KOHA_INST_$key = blib/$key
904 KOHA_DEST_$key = $target_directories{$key}
905 );
906     }
907
908     $install .= qq(
909 install :: all install_koha
910 \t\$(NOECHO) \$(NOOP)
911 );
912     $install .= "install_koha ::\n";      
913     $install .= "\t\$(NOECHO) umask 022; \$(MOD_INSTALL) \\\n";
914     foreach my $key (sort keys %target_directories) {
915         $install .= "\t\t\$(KOHA_INST_$key) \$(KOHA_DEST_$key) \\\n";
916     }
917     $install .= "\t\t\$(INST_MAN1DIR) \$(DESTINSTALLMAN1DIR) \\\n";
918     $install .= "\t\t\$(INST_MAN3DIR) \$(DESTINSTALLMAN3DIR)\n";
919     return $install;
920 }
921
922 sub postamble {
923     # put directory mappings into Makefile
924     # so that Make will export as environment
925     # variables -- this is for the use of
926     # rewrite-confg.PL
927     my $env = join("\n", map { "export __${_}__ = $target_directories{$_}" } keys %target_directories); 
928     $env .= "\n\n";
929     $env .= join("\n", map { "export __${_}__ = $config{$_}" } keys %config);
930     return "$env\n";
931 }
932
933 __END__
934
935
936 =head1 SEE ALSO
937
938 ExtUtils::MakeMaker(3)
939
940 =head1 AUTHORS
941
942 MJ Ray mjr at phonecoop.coop
943
944 =cut
945 FIXME: Zebra lang/MARC mapping
946 FIXME: deal with .htaccess