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