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