installer: command-line scripts improve finding C4 modules
[koha.git] / Makefile.PL
blob9898c846e5719b9501b2a00ee19d0184654aed93
1 # Copyright 2007 MJ Ray
3 # This file is part of Koha.
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.
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.
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
18 # Current maintainer MJR http://mjr.towers.org.uk/
19 # See http://www.koha.org/wiki/?page=KohaInstaller
22 use strict;
23 use warnings;
24 use ExtUtils::MakeMaker;
25 use POSIX;
26 use File::Spec;
28 my $DEBUG = 0;
29 die "perl 5.6.1 or later required" unless ($] >= 5.006001);
31 # Hash up directory structure & files beginning with the directory we were called from (should be the base of koha)...
33 my $dirtree = hashdir('.');
34 my %result = ();
36 =head1 NAME
38 Makefile.PL - Koha packager and installer
40 =head1 SYNOPSIS
42 =head2 BASIC INSTALLATION
44         perl Makefile.PL
45         make
46         sudo make install
48 =head2 PACKAGING RELEASE TARBALLS
50         make manifest tardist
51         make manifest zipdist
53 =head2 CLEANING UP
55         make clean
57 =head1 DESCRIPTION
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.
67 =head1 VARIABLES
69 =head2 NAME, VERSION_FROM, ABSTRACT, AUTHOR
71 Basic metadata about this software.
73 =head2 NO_META
75 Suppress generation of META.yml file.
77 =head2 PREREQ_PM
79 Hash of perl modules and versions required.
81 =head2 PM
83 Hash of file mappings
85 =head2 PL_FILES
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.
92 =cut
94 =head2 target_map
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:
100 =over 4
102 =item If a directory or file is specified, it and its
103 contents will be copied to the installation target directory.
105 =item If a subdirectory of a mapped directory is specified,
106 its target overrides the parent's target for that subdirectory.
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.
112 =item Any files at the top level of the source tree that are
113 not included in the map will not be installed.
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.
124 =back
126 The permitted installation targets are:
128 =over 4
130 =item INTRANET_CGI_DIR 
132 CGI scripts for intranet (staff) interface.
134 =item INTRANET_TMPL_DIR
136 HTML templates for the intranet interface.
138 =item INTRANET_WWW_DIR
140 HTML files, images, etc. for DocumentRoot for the intranet interface.
142 =item OPAC_CGI_DIR
144 CGI scripts for OPAC (public) interface.
146 =item OPAC_TMPL_DIR
148 HTML templates for the OPAC interface.
150 =item OPAC_WWW_DIR
152 HTML files, images, etc. for DocumentRoot for the OPAC interface.
154 =item PERL_MODULE_DIR
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.
161 =item KOHA_CONF_DIR
163 Directory for Koha configuration files.
165 =item ZEBRA_CONF_DIR
167 Directory for Zebra configuration files.
169 =item ZEBRA_LOCK_DIR
171 Directory for Zebra's lock files.
173 =item ZEBRA_DATA_DIR
175 Directory for Zebra's data files.
177 =item ZEBRA_RUN_DIR
179 Directory for Zebra's UNIX-domain sockets.
181 =item MISC_DIR
183 Directory for for miscellaenous scripts, among other
184 things the translation toolkit and RSS feed tools.
186 =item SCRIPT_DIR
188 Directory for command-line scripts and daemons.
190 =item MAN_DIR
192 Directory for man pages created from POD -- will mostly
193 contain information of interest to Koha developers.
195 =item DOC_DIR
197 Directory for Koha documentation accessed from the 
198 command-line, e.g., READMEs.
200 =item LOG_DIR
202 Directory for Apache and Zebra logs produced by Koha.
204 =item NONE
206 This is a dummy target used to explicitly state
207 that a given file or directory is not to be installed.
208 This is used either for parts of the installer itself
209 or for development tools that are not applicable to a
210 production installation.
212 =back
214 =cut
216 my $target_map = {
217   './about.pl'                  => 'INTRANET_CGI_DIR',
218   './acqui'                     => 'INTRANET_CGI_DIR',
219   './admin'                     => 'INTRANET_CGI_DIR',
220   './authorities'               => 'INTRANET_CGI_DIR',
221   './C4'                        => 'PERL_MODULE_DIR',
222   './C4/SIP/t'                  => 'NONE',
223   './C4/SIP/koha_test'          => 'NONE',
224   './C4/tests'                  => 'NONE',
225   './catalogue'                 => 'INTRANET_CGI_DIR',
226   './cataloguing'               => 'INTRANET_CGI_DIR',
227   './changelanguage.pl'         => 'INTRANET_CGI_DIR',
228   './check_sysprefs.pl'         => 'NONE',
229   './circ'                      => 'INTRANET_CGI_DIR',
230   './edithelp.pl'               => 'INTRANET_CGI_DIR',
231   './etc'                       => { target => 'KOHA_CONF_DIR', trimdir => -1 },
232   './etc/zebradb'               => { target => 'ZEBRA_CONF_DIR', trimdir => -1 },
233   './help.pl'                   => 'INTRANET_CGI_DIR', 
234   './installer-CPAN.pl'         => 'NONE',
235   './installer'                 => 'INTRANET_CGI_DIR',
236   './koha-tmpl/errors'          => {target => 'INTRANET_CGI_DIR', trimdir => 2},
237   './koha-tmpl/intranet-tmpl'   => {target => 'INTRANET_TMPL_DIR', trimdir => -1},
238   './koha-tmpl/opac-tmpl'       => {target => 'OPAC_TMPL_DIR', trimdir => -1},
239   './kohaversion.pl'            => 'INTRANET_CGI_DIR', 
240   './labels'                    => 'INTRANET_CGI_DIR',
241   './mainpage.pl'               => 'INTRANET_CGI_DIR',
242   './Makefile.PL'               => 'NONE',
243   './MANIFEST.SKIP'             => 'NONE',
244   './members'                   => 'INTRANET_CGI_DIR',
245   './misc'                      => { target => 'SCRIPT_DIR', trimdir => -1 }, 
246   './misc/info'                 => { target => 'DOC_DIR', trimdir => 2 },
247   './misc/release notes'        => { target => 'DOC_DIR', trimdir => 2 },
248   './misc/translator'           => { target => 'MISC_DIR', trimdir => 2 }, 
249   './misc/installer_devel_notes' => 'NONE',
250   './opac'                      => 'OPAC_CGI_DIR',
251   './README.txt'                => 'NONE',
252   './reports'                   => 'INTRANET_CGI_DIR',
253   './reserve'                   => 'INTRANET_CGI_DIR',
254   './reviews'                   => 'INTRANET_CGI_DIR',
255   './rewrite-config.PL'         => 'NONE',
256   './reviews'                   => 'INTRANET_CGI_DIR',
257   './rss'                       => 'MISC_DIR', 
258   './serials'                   => 'INTRANET_CGI_DIR',
259   './skel'                      => 'NONE',
260   './skel/var/log/koha'         => { target => 'LOG_DIR', trimdir => -1 },
261   './skel/var/run/koha/zebradb' => { target => 'ZEBRA_RUN_DIR', trimdir => -1 },
262   './skel/var/lock/koha/zebradb/authorities' => { target => 'ZEBRA_LOCK_DIR', trimdir => 6 },
263   './skel/var/lib/koha/zebradb/authorities/key'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
264   './skel/var/lib/koha/zebradb/authorities/register'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
265   './skel/var/lib/koha/zebradb/authorities/shadow'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
266   './skel/var/lib/koha/zebradb/authorities/tmp'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
267   './skel/var/lock/koha/zebradb/biblios' => { target => 'ZEBRA_LOCK_DIR', trimdir => 6 },
268   './skel/var/lib/koha/zebradb/biblios/key'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
269   './skel/var/lib/koha/zebradb/biblios/register'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
270   './skel/var/lib/koha/zebradb/biblios/shadow'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
271   './skel/var/lib/koha/zebradb/biblios/tmp'  => { target => 'ZEBRA_DATA_DIR', trimdir => 6 },
272   './sms'                       => 'INTRANET_CGI_DIR',
273   './suggestion'                => 'INTRANET_CGI_DIR',
274   './svc'                       => 'INTRANET_CGI_DIR',
275   './t'                         => 'NONE',
276   './tmp'                       => 'NONE', # FIXME need to determine whether 
277                                            # Koha generates any persistent temp files
278                                            # that should go in /var/tmp/koha
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',
286 =head1 CONFIGURATION OPTIONS
288 The following configuration options are used by the installer.
290 =over 4
292 =item INSTALL_MODE
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).
301 =item INSTALL_BASE
303 Directory under which most components will go.  Default
304 value will vary depending on INSTALL_MODE.
306 =item DB_TYPE
308 Type of DBMS (e.g., mysql or Pg).
310 =item DB_HOST
312 Name of DBMS server.
314 =item DB_PORT
316 Port that DBMS server is listening on.
318 =item DB_NAME
320 Name of the DBMS database for Koha.
322 =item DB_USER
324 Name of DBMS user account for Koha's database.
326 =item DB_PASS
328 Pasword of DMBS user account for Koha's database.
330 =item INSTALL_ZEBRA
332 Whether to install Zebra configuration files and data
333 directories.
335 =item ZEBRA_MARC_FORMAT
337 Specifies format of MARC records to be indexed by Zebra.
339 =item ZEBRA_LANGUAGE
341 Specifies primary language of records that will be 
342 indexed by Zebra.
344 =item ZEBRA_USER
346 Internal Zebra user account for the index.
348 =item ZEBRA_PASS
350 Internal Zebra user account's password.
352 =item KOHA_USER
354 System user account that will own Koha's files.
356 =item KOHA_GROUP
358 System group that will own Koha's files.
360 =back
362 =cut
364 # default configuration options
365 my %config_defaults = (
366   'INSTALL_MODE'      => 'standard',
367   'INSTALL_BASE'      => '/usr/share/koha',
368   'DB_TYPE'           => 'mysql',
369   'DB_HOST'           => 'localhost',
370   'DB_NAME'           => 'koha',    
371   'DB_USER'           => 'kohaadmin',
372   'DB_PASS'           => 'katikoan',
373   'INSTALL_ZEBRA'     => 'yes',
374   'ZEBRA_MARC_FORMAT' => 'marc21',
375   'ZEBRA_LANGUAGE'    => 'en',
376   'ZEBRA_USER'        => 'kohauser',
377   'ZEBRA_PASS'        => 'zebrastripes',
378   'KOHA_USER'         => 'koha',
379   'KOHA_GROUP'        => 'koha',
382 # valid values for certain configuration options
383 my %valid_config_values = (
384   'INSTALL_MODE'  => { 'standard' => 1, 'single' => 1, 'dev' => 1 },
385   'DB_TYPE' => { 'mysql' => 1, 'Pg' => 1 },
386   'INSTALL_ZEBRA' => { 'yes' => 1, 'no' => 1 },
387   'ZEBRA_MARC_FORMAT' => { 'marc21' => 1, 'unimarc' => 1 }, # FIXME should generate from contents of distributation
388   'ZEBRA_LANGUAGE'    => { 'en' => 1, 'fr' => 1 }, # FIXME should generate from contents of distribution
391 my %config = get_configuration(\%config_defaults, \%valid_config_values);
392 my ($target_directories, $skip_directories) = get_target_directories(\%config);
393 display_configuration(\%config, $target_directories);
394 my $file_map = {};
395 get_file_map($target_map, $dirtree, $file_map, $config{'INSTALL_ZEBRA'} eq "yes" ? 1: 0);
397 my $pl_files = {
398       'rewrite-config.PL' => [
399          'blib/KOHA_CONF_DIR/koha-conf.xml',
400          'blib/KOHA_CONF_DIR/koha-httpd.conf',
401          ]
404 if ($config{'INSTALL_ZEBRA'} eq "yes") {
405     push @{ $pl_files->{'rewrite-config.PL'} }, (
406         'blib/ZEBRA_CONF_DIR/etc/passwd',
407         'blib/ZEBRA_CONF_DIR/zebra-biblios.cfg',
408         'blib/ZEBRA_CONF_DIR/zebra-authorities.cfg'
409     );
412 if ($config{'INSTALL_MODE'} ne "dev") {
413     push @{ $pl_files->{'rewrite-config.PL'} }, (
414         'blib/PERL_MODULE_DIR/C4/Context.pm',
415         'blib/SCRIPT_DIR/kohalib.pl'
416     );
419 WriteMakefile(
420     NAME => 'koha',
421     #VERSION => strftime('2.9.%Y%m%d%H',gmtime),
422     VERSION_FROM => 'kohaversion.pl',
423     ABSTRACT => 'Award-winning integrated library system (ILS) and Web OPAC',
424     AUTHOR => 'Koha Developers <koha-devel@nongnu.org>',
425     NO_META => 1,
426     PREREQ_PM => {
427 'CGI' => 3.15,
428 'CGI::Carp' => 1.29,
429 'CGI::Session' => '4.10',
430 'Class::Factory::Util' => 1.7,
431 'Class::Accessor' => 0.30,
432 'DBD::mysql' => 3.0008,
433 'DBI' => 1.53,
434 'Data::Dumper' => 2.121_08,
435 'Date::Calc' => 5.4,
436 'Date::Manip' => 5.44,
437 'Digest::MD5' => 2.36,
438 'File::Temp' => 0.16,
439 'GD::Barcode::UPCE' => 1.1,
440 'Getopt::Long' => 2.35,
441 'Getopt::Std' => 1.05,
442 'HTML::Template::Pro' => 0.65,
443 'HTTP::Cookies' => 1.39,
444 'HTTP::Request::Common' => 1.26,
445 'LWP::Simple' => 1.41,
446 'LWP::UserAgent' => 2.033,
447 'Lingua::Stem' => 0.82,
448 'List::Util' => 1.18,
449 'Locale::Language' => 2.07,
450 'MARC::Charset' => 0.98,
451 'MARC::Crosswalk::DublinCore' => 0.02,
452 'MARC::File::XML' => 0.88,
453 'MARC::Record' => 2.00,
454 'MIME::Base64' => 3.07,
455 'MIME::QuotedPrint' => 3.07,
456 'Mail::Sendmail' => 0.79,
457 'Net::Z3950::ZOOM' => 1.16,
458 'PDF::API2' => 2.000,
459 'PDF::API2::Page' => 2.000,
460 'PDF::API2::Util' => 2.000,
461 'PDF::Reuse' => 0.33,
462 'PDF::Reuse::Barcode' => 0.05,
463 'POSIX' => 1.09,
464 'Schedule::At' => 1.06,
465 'Term::ANSIColor' => 1.10,
466 'Test' => 1.25,
467 'Test::Harness' => 2.56,
468 'Test::More' => 0.62,
469 'Text::CSV' => 0.01,
470 'Text::CSV_XS' => 0.32,
471 'Text::Wrap' => 2005.082401,
472 'Time::HiRes' => 1.86,
473 'Time::localtime' => 1.02,
474 'Unicode::Normalize' => 0.32,
475 'XML::Dumper' => 0.81,
476 'XML::LibXML' => 1.59,
477 'XML::SAX::ParserFactory' => 1.01,
478 'XML::Simple' => 2.14,
479 'XML::RSS' => 1.31,
480         },
482         # File tree mapping
483         PM => $file_map,
485     # Man pages generated from POD
486     INSTALLMAN1DIR => File::Spec->catdir($target_directories->{'MAN_DIR'}, 'man1'),
487     INSTALLMAN3DIR => File::Spec->catdir($target_directories->{'MAN_DIR'}, 'man3'),
489    PL_FILES => $pl_files,
493 =head1 FUNCTIONS
495 =head2 hashdir
497 This function recurses through the directory structure and builds
498 a hash of hashes containing the structure with arrays holding filenames.
499 This directory hashing routine was taken from BrowserUK @ http://www.perlmonks.org/?node_id=219919
501 =cut
503 sub hashdir{
504     my $dir = shift;
505     opendir my $dh, $dir or die $!;
506     my $tree = {}->{$dir} = {};
507     while( my $file = readdir($dh) ) {
508         next if $file =~ m/^\.{1,2}/ and $file !~ /^\.htaccess/; # .htaccess is a special case
509         my $path = $dir .'/' . $file;
510         $tree->{$file} = hashdir($path), next if -d $path;
511         push @{$tree->{'.'}}, $file;
512     }
513     return $tree;
516 =head2 get_file_map 
518 This function combines the target_map and file hash to
519 map each source file to its destination relative to
520 the set of installation targets.
522 Output will be a hash mapping from each source file
523 to its destination value, like this:
525 'mainpage.pl' => '$(INTRANET_CGI_DIR)/mainpage.pl'
527 =cut
529 sub get_file_map {
530     my $target_map = shift;
531     my $dirtree = shift;
532     my $file_map = shift;
533     my $install_zebra = shift;
534     my $curr_path = @_ ? shift : ['.'];
536     # Traverse the directory tree.
537     # For each file or directory, identify the
538     # most specific match in the target_map
539     foreach my $dir (sort keys %{ $dirtree }) {
540         if ($dir eq '.') {
541             # deal with files in directory
542             foreach my $file (sort @{ $dirtree->{$dir} }) {
543                 my $targetdir = undef;
544                 my $matchlevel = undef;
545                 # first, see if there is a match on this specific
546                 # file in the target map
547                 my $filepath = join("/", @$curr_path, $file);
548                 if (exists $target_map->{$filepath}) {
549                     $targetdir = $target_map->{$filepath};
550                     $matchlevel = scalar(@$curr_path) + 1;
551                 } else {
552                     # no match on the specific file; look for
553                     # a directory match
554                     for (my $i = scalar(@$curr_path) - 1; $i >= 0; $i--)  {
555                         my $dirpath = join("/", @$curr_path[0..$i]);
556                         if (exists $target_map->{$dirpath}) {
557                             $targetdir = $target_map->{$dirpath};
558                             $matchlevel = $i + 1;
559                             last;
560                         }
561                     }
562                 }
563                 if (defined $targetdir) {
564                      _add_to_file_map($file_map, $targetdir, $curr_path, $file, $matchlevel, $install_zebra);
565                 } else {
566                     my $path = join("/", @$curr_path);
567                     print "failed to map: $path/$file\n" if $DEBUG;
568                 }
569             }
570         } else {
571             # dealing with subdirectory
572             push @$curr_path, $dir;
573             get_file_map($target_map, $dirtree->{$dir}, $file_map, $install_zebra, $curr_path);
574             pop @$curr_path;
575         }
576     }
579 sub _add_to_file_map {
580     my $file_map = shift;
581     my $targetdir = shift;
582     my $curr_path = shift;
583     my $file = shift;
584     my $matchlevel = shift;
585     my $install_zebra = shift;
586     my $dest_path = @_ ? shift : $curr_path;
588     # The target can be one of the following:
589     # 1. scalar representing target symbol
590     # 2. hash ref containing target and trimdir keys
591     #
592     # Consequently, this routine traverses this structure,
593     # calling itself recursively, until it deals with
594     # all of the scalar target symbols.
595     if (ref $targetdir eq 'HASH') {
596         my $subtarget = $targetdir->{target};
597         if (exists $targetdir->{trimdir}) {
598             # if we get here, we've specified that
599             # rather than installing the file to
600             # $(TARGET)/matching/dirs/subdirs/file,
601             # we want to install it to
602             # $(TARGET)/subdirs/file
603             #
604             # Note that this the only place where
605             # $matchlevel is used.
606             my @new_dest_path = @$dest_path;
607             if ($targetdir->{trimdir} == -1)  {
608                 splice @new_dest_path, 0, $matchlevel;
609             } else {
610                 splice @new_dest_path, 0, $targetdir->{trimdir};
611             }
612             _add_to_file_map($file_map, $subtarget, $curr_path, $file, $matchlevel, $install_zebra, \@new_dest_path);
613         } else {
614             # actually getting here means that the
615             # target was unnecessarily listed
616             # as a hash, but we'll forgive that
617             _add_to_file_map($file_map, $subtarget, $curr_path, $file, $matchlevel, $install_zebra);
618         }
619     } elsif ($targetdir ne 'NONE' and $targetdir ne '') {
620         my $source = File::Spec->catfile(@$curr_path, $file);
621         my $destination = File::Spec->catfile('blib', $targetdir, @$dest_path, $file);
622         #print "$source => $destination\n"; # DEBUG
623         # quote spaces in file names
624         # FIXME: this is of questionable portability and
625         # probably depends on user's make recognizing this
626         # quoting syntax -- probably better to remove
627         # spaces and shell metacharacters from all file names
628         $source =~ s/ /\\ /g;
629         $destination =~ s/ /\\ /g;
630       
631         $file_map->{$source} = $destination unless (!$install_zebra and $targetdir =~ /ZEBRA/);
632     }
635 =head2 get_configuration_options
637 This prompts the user for various configuration options.
639 =cut
641 sub get_configuration {
642   my $defaults = shift;
643   my $valid_values = shift;
644   my %config = ();
646   my $msg = q(
647 By default, Koha can be installed in one of three ways:
649 standard: Install files in conformance with the Filesystem
650           Hierarchy Standard (FHS).  This is the default mode
651           and should be used when installing a production
652           Koha system.  On Unix systems, root access is 
653           needed to complete a standard installation.
655 single:   Install files under a single directory.  This option
656           is useful for installing Koha without root access, e.g.,
657           on a web host that allows CGI scripts and MySQL databases
658           but requires the user to keep all files under the user's
659           HOME directory.
661 dev:      Create a set of symbolic links and configuration files to
662           allow Koha to run directly from the source distribution.
663           This mode is useful for developers who want to run
664           Koha from a git clone.
666 Installation mode);
667     $msg .= _add_valid_values_disp('INSTALL_MODE', $valid_values);
668     $config{'INSTALL_MODE'} = _get_value('INSTALL_MODE', $msg, $defaults->{'INSTALL_MODE'}, $valid_values);
670     # set message and default value for INSTALL_BASE
671     # depending on value of INSTALL_MODE
672     my $install_base_default = $defaults->{'INSTALL_BASE'};
673     if ($config{'INSTALL_MODE'} eq 'dev') {
674         $msg = q(
675 Please specify the directory in which to install Koha's
676 active configuration files and (if applicable) the
677 Zebra database.  Koha's CGI scripts and templates will
678 be run from the current directory.
680 Configuration directory:);
681         # FIXME - home directory portability consideration apply
682         $install_base_default = (exists $ENV{'HOME'}) ? "$ENV{'HOME'}/koha-dev" : "/usr/share/koha-dev";
683     } elsif ($config{'INSTALL_MODE'} eq 'single') {
684         $msg = "\nPlease specify the directory in which to install Koha";
685         # FIXME -- we're assuming under a 'single' mode install
686         # that user will likely want to install under the home
687         # directory.  This is OK in and of itself, but we should
688         # use File::HomeDir to locate the home directory portably.  
689         # This is deferred for now because File::HomeDir is not yet
690         # core.
691         $install_base_default = (exists $ENV{'HOME'}) ? "$ENV{'HOME'}/koha" : "/usr/share/koha";
692     } else {
693         # must be standard
694         $msg = q(
695 Please specify the directory under which most Koha files 
696 will be installed.
698 Note that if you are planning in installing more than 
699 one instance of Koha, you may want to modify the last
700 component of the directory path, which will be used
701 as the package name in the FHS layout.
703 Base installation directory);
704     }
705     $config{'INSTALL_BASE'} = _get_value('INSTALL_BASE', $msg, $install_base_default, $valid_values);
706     $config{'INSTALL_BASE'} = File::Spec->rel2abs($config{'INSTALL_BASE'});
708     if ($config{'INSTALL_MODE'} eq "standard") {
709         $msg = q(
710 Since you are using the 'standard' install
711 mode, you should run 'make install' as root.
712 However, it is recommended that a non-root
713 user (on Unix and Linux platforms) have 
714 ownership of Koha's files, including the
715 Zebra indexes if applicable.
717 Please specify a user account.  This
718 user account does not need to exist
719 right now, but it needs to exist
720 before you run 'make install'.  Please
721 note that for security reasons, this
722 user should not be the same as the user
723 account Apache runs under.
725 User account);
726         $config{'KOHA_USER'} = _get_value('KOHA_USER', $msg, $defaults->{'KOHA_USER'}, $valid_values);
728         $msg = q(
729 Please specify the group that should own
730 Koha's files.  As above, this group need
731 not exist right now, but should be created
732 before you run 'make install'.
734 Group);
735         $config{'KOHA_GROUP'} = _get_value('KOHA_GROUP', $msg, $defaults->{'KOHA_GROUP'}, $valid_values);
736     }
738     $msg = q(
739 Please specify which database engine you will use
740 to store data in Koha.  The choices are MySQL and
741 PostgreSQL; please note that at the moment
742 PostgreSQL support is highly experimental.
744 DBMS to use);
745     $msg .= _add_valid_values_disp('DB_TYPE', $valid_values);
746     $config{'DB_TYPE'} = _get_value('DB_TYPE', $msg, $defaults->{'DB_TYPE'}, $valid_values);
748     $msg = q(
749 Please specify the name or address of your 
750 database server.  Note that the database 
751 does not have to exist at this point, it
752 can be created after running 'make install'
753 and before you try using Koha for the first time.
755 Database server);
756     $config{'DB_HOST'} = _get_value('DB_HOST', $msg, $defaults->{'DB_HOST'}, $valid_values);
758     $msg = q(
759 Please specify the port used to connect to the
760 DMBS);
761     my $db_port_default = $config{'DB_TYPE'} eq 'mysql' ? '3306' : '5432';
762     $config{'DB_PORT'} = _get_value('DB_PORT', $msg, $db_port_default, $valid_values);
764     $msg = q(
765 Please specify the name of the database to be
766 used by Koha);
767     $config{'DB_NAME'} = _get_value('DB_NAME', $msg, $defaults->{'DB_NAME'}, $valid_values);
769     $msg = q(
770 Please specify the user that owns the database to be
771 used by Koha);
772     $config{'DB_USER'} = _get_value('DB_USER', $msg, $defaults->{'DB_USER'}, $valid_values);
774     $msg = q(
775 Please specify the password of the user that owns the 
776 database to be used by Koha);
777     $config{'DB_PASS'} = _get_value('DB_PASS', $msg, $defaults->{'DB_PASS'}, $valid_values);
779     $msg = q(
780 Koha can use the Zebra search engine for high-performance
781 searching of bibliographic and authority records.  If you
782 have installed the Zebra software and would like to use it,
783 please answer 'yes' to the following question.  Otherwise, 
784 Koha will default to using its internal search engine.
786 Please note that if you choose *NOT* to install Zebra,
787 koha-conf.xml will still contain some references to Zebra
788 settings.  Those references will be ignored by Koha.
790 Install the Zebra configuration files?);
791     $msg .= _add_valid_values_disp('INSTALL_ZEBRA', $valid_values);
792     $config{'INSTALL_ZEBRA'} = _get_value('INSTALL_ZEBRA', $msg, $defaults->{'INSTALL_ZEBRA'}, $valid_values);
794     if ($config{'INSTALL_ZEBRA'} eq 'yes') {
795         $msg = q(
796 Since you've chosen to use Zebra with Koha,
797 you must specify the primary MARC format of the
798 records to be indexed by Zebra.
800 Koha provides Zebra configuration files for MARC 21
801 and UNIMARC.
803 MARC format for Zebra indexing);
804         $msg .= _add_valid_values_disp('ZEBRA_MARC_FORMAT', $valid_values);
805         $config{'ZEBRA_MARC_FORMAT'} = _get_value('ZEBRA_MARC_FORMAT', $msg, $defaults->{'ZEBRA_MARC_FORMAT'}, $valid_values);
806         $msg = q(
807 Koha supplies Zebra configuration files tuned for
808 searching either English (en) or French (fr) MARC
809 records.
811 Primary language for Zebra indexing);
812         $msg .= _add_valid_values_disp('ZEBRA_LANGUAGE', $valid_values);
813         $config{'ZEBRA_LANGUAGE'} = _get_value('ZEBRA_LANGUAGE', $msg, $defaults->{'ZEBRA_LANGUAGE'}, $valid_values);
815         $msg = q(
816 Please specify Zebra database user);
817         $config{'ZEBRA_USER'} = _get_value('ZEBRA_USER', $msg, $defaults->{'ZEBRA_USER'}, $valid_values);
819         $msg = q(
820 Please specify the Zebra database password);
821         $config{'ZEBRA_PASS'} = _get_value('ZEBRA_PASS', $msg, $defaults->{'ZEBRA_PASS'}, $valid_values);
823     }
825     print "\n\n";
826     return %config;
829 sub _add_valid_values_disp {
830     my $key = shift;
831     my $valid_values = shift;
832    
833     my $disp = "";
834     if (exists $valid_values->{$key}) {
835         $disp = " (" . join(", ", sort keys %{ $valid_values->{$key} }) . ")";
836     }
837     return $disp;
840 sub _get_value {
841     my $key = shift;
842     my $msg = shift;
843     my $default = shift;
844     my $valid_values = shift;
846     # override default value from environment
847     if (exists $ENV{$key}) {
848         $default = $ENV{$key};
849         $msg .= " (default from environment)";
850     }
852     my $val = prompt($msg, $default);
853     while (exists $valid_values->{$key} and not exists $valid_values->{$key}->{$val}) {
854         my $retry_msg = "Value '$val' is not a valid option.\n";
855         $retry_msg .= "Please enter a value";
856         $retry_msg .= _add_valid_values_disp($key, $valid_values);
857         $val = prompt($retry_msg, $default);
858     }
859     return $val;
862 =head2 get_target_directories 
864 Creates a hash mapping from symbols for installation target
865 directories to actual directory paths.
867 Also returns a hash indicating targets for which 
868 files need not be copied -- this is used for the 'dev'
869 mode installation, where some files are installed in place.
871 =cut
873 sub get_target_directories {
874     my $config = shift;
876     my $base = $config->{'INSTALL_BASE'};
877     my $mode = $config->{'INSTALL_MODE'};
879     # get last component of install base directory
880     # to treat as package name
881     my ($volume, $directories, $file) = File::Spec->splitpath($base, 1);
882     my @basedir = File::Spec->splitdir($directories);
883     my $package = pop @basedir;
885     my %dirmap = ();
886     my %skipdirs = ();
887     if ($mode eq 'single') {
888         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'cgi-bin');
889         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs', 'intranet-tmpl');
890         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs');
891         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'cgi-bin');
892         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs', 'opac-tmpl');
893         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs');
894         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir(@basedir, $package, 'lib');
895         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc');
896         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'zebradb');
897         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
898         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
899         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
900         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
901         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb');
902         $dirmap{'LOG_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'log');
903         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'zebradb');
904         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'run', 'zebradb');
905     } elsif ($mode eq 'dev') {
906         my $curdir = File::Spec->rel2abs(File::Spec->curdir());
907         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir($curdir);
908         $skipdirs{'INTRANET_CGI_DIR'} = 1;
909         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'intranet-tmpl');
910         $skipdirs{'INTRANET_TMPL_DIR'} = 1;
911         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl');
912         $skipdirs{'INTRANET_WWW_DIR'} = 1;
913         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir($curdir, 'opac');
914         $skipdirs{'OPAC_CGI_DIR'} = 1;
915         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'opac-tmpl');
916         $skipdirs{'OPAC_TMPL_DIR'} = 1;
917         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl');
918         $skipdirs{'OPAC_WWW_DIR'} = 1;
919         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir($curdir);
920         $skipdirs{'PERL_MODULE_DIR'} = 1;
921         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc');
922         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'zebradb');
923         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
924         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
925         $skipdirs{'SCRIPT_DIR'} = 1;
926         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
927         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
928         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb');
929         $dirmap{'LOG_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'log');
930         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'zebradb');
931         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'run', 'zebradb');
932     } else {
933         # mode is standard, i.e., 'fhs'
934         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'cgi-bin');
935         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs', 'intranet-tmpl');
936         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs');
937         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'cgi-bin');
938         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs', 'opac-tmpl');
939         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs');
940         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir(@basedir, $package, 'lib');
941         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package);
942         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package, 'zebradb');
943         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
944         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
945         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
946         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
947         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'var', 'lock', $package, 'zebradb');
948         $dirmap{'LOG_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'log', $package);
949         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'lib', $package, 'zebradb');
950         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'run', $package, 'zebradb');
951     }
953     _get_env_overrides(\%dirmap);
954     _get_argv_overrides(\%dirmap);
956     return \%dirmap, \%skipdirs;
959 sub _get_env_overrides {
960     my $dirmap = shift;
962     foreach my $key (keys %$dirmap) {
963         if (exists $ENV{$key}) {
964             $dirmap->{$key} = $ENV{$key};
965             print "Setting $key from environment\n";
966         }
967     }
970 sub _get_argv_overrides {
971     my $dirmap = shift;
972     
973     my @new_argv = ();
974     for (my $i = 0; $i <= $#ARGV; $i++) {
975         if ($ARGV[$i] =~ /^([^=]+)=([^=]+)$/ and exists $dirmap->{$1}) {
976             $dirmap->{$1} = $2;
977         } else {
978             push @new_argv, $ARGV[$i];
979         }
980     }
981     @ARGV = @new_argv;
984 sub display_configuration {
985     my $config = shift;
986     my $dirmap = shift;
987     print "\n\nKoha will be installed with the following configuration parameters:\n\n";
988     foreach my $key (sort keys %$config) {
989         print sprintf("%-25.25s%s\n", $key, $config->{$key});
990     }
992     print "\nand in the following directories:\n\n";
993     foreach my $key (sort keys %$dirmap) {
994         print sprintf("%-25.25s%s\n", $key, $dirmap->{$key});
995     }
996     print "\n\nTo change any configuration setting, please run\n";
997     print "perl Makefile.PL again.  To override one of the target\n";
998     print "directories, you can do so on the command line like this:\n";
999     print "\nperl Makefile.PL PERL_MODULE_DIR=/usr/share/perl/5.8\n\n";
1000     print "You can also set different default values for parameters\n";
1001     print "or override directory locations by using environment variables.\n";
1002     print "\nFor example:\n\n";
1003     print "export DB_USER=my_koha\n";
1004     print "perl Makefile.PL\n";
1005     print "\nor\n\n";
1006     print "DB_USER=my_koha DOC_DIR=/usr/local/info perl Makefile.PL\n\n";
1009 package MY;
1011 sub test {
1012     my $self = shift;
1013     my $test = $self->SUPER::test(@_);
1014     $test =~ s!\$\(INST_LIB\)!blib/PERL_MODULE_DIR!g;
1015     return $test;
1018 sub install {
1019     my $self = shift;
1020     my $install = ""; 
1021     # NOTE: we're *not* doing this: my $install = $self->SUPER::install(@_);
1022     # This means that we're completely overriding EU::MM's default
1023     # installation and uninstallation targets.
1024     foreach my $key (sort keys %$target_directories) {
1025         $install .= qq(
1026 KOHA_INST_$key = blib/$key
1027 KOHA_DEST_$key = $target_directories->{$key}
1028 ) unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1029     }
1031     $install .= qq(
1032 install :: all install_koha set_koha_ownership set_koha_permissions warn_koha_env_vars
1033 \t\$(NOECHO) \$(NOOP)
1035     $install .= "install_koha ::\n";      
1036     $install .= "\t\$(NOECHO) umask 022; \$(MOD_INSTALL) \\\n";
1037     foreach my $key (sort keys %$target_directories) {
1038         $install .= "\t\t\$(KOHA_INST_$key) \$(KOHA_DEST_$key) \\\n" 
1039             unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1040     }
1041     $install .= "\t\t\$(INST_MAN1DIR) \$(DESTINSTALLMAN1DIR) \\\n";
1042     $install .= "\t\t\$(INST_MAN3DIR) \$(DESTINSTALLMAN3DIR)\n";
1044     $install .= "\n";
1045     $install .= "set_koha_ownership ::\n";
1046     if ($config{'INSTALL_MODE'} eq 'standard' and $config{'KOHA_USER'} ne "root") {
1047         foreach my $key (sort keys %$target_directories) {
1048             $install .= "\t\$(NOECHO) chown -R $config{'KOHA_USER'}:$config{'KOHA_GROUP'} \$(KOHA_DEST_$key)\n"
1049                 unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1050         }
1051     } else {
1052         $install .= "\t\t\$(NOECHO) \$(NOOP)\n\n";
1053     }
1055     $install .= "\n";
1056     $install .= "set_koha_permissions ::\n";
1057     # This is necessary because EU::MM installs files
1058     # as either 0444 or 0555, and we want the owner
1059     # of Koha's files to have write permission by default.
1060     foreach my $key (sort keys %$target_directories) {
1061         $install .= "\t\$(NOECHO) chmod -R u+w \$(KOHA_DEST_$key)\n"
1062             unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1063     }
1064     $install .= "\n";
1066     $install .= "warn_koha_env_vars ::\n";
1067     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1068     $install .= "\t\$(NOECHO) \$(ECHO) Koha\\'s files have now been installed. \n";
1069     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1070     $install .= "\t\$(NOECHO) \$(ECHO) In order to use Koha\\'s command-line batch jobs,\n";
1071     $install .= "\t\$(NOECHO) \$(ECHO) you should set the following environment variables:\n";
1072     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1073     $install .= "\t\$(NOECHO) \$(ECHO) export KOHA_CONF=\$(KOHA_DEST_KOHA_CONF_DIR)/koha-conf.xml\n";
1074     $install .= "\t\$(NOECHO) \$(ECHO) export PERL5LIB=$target_directories->{'PERL_MODULE_DIR'}\n";
1075     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1076     $install .= "\t\$(NOECHO) \$(ECHO) For other post-installation tasks, please consult the README.\n";
1077     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1079     return $install;
1082 sub postamble {
1083     # put directory mappings into Makefile
1084     # so that Make will export as environment
1085     # variables -- this is for the use of
1086     # rewrite-confg.PL
1087     my $env = join("\n", map { "export __${_}__ = $target_directories->{$_}" } keys %$target_directories); 
1088     $env .= "\n\n";
1089     $env .= join("\n", map { "export __${_}__ = $config{$_}" } keys %config);
1090     return "$env\n";
1093 __END__
1096 =head1 SEE ALSO
1098 ExtUtils::MakeMaker(3)
1100 =head1 AUTHORS
1102 MJ Ray mjr at phonecoop.coop
1103 Galen Charlton galen.charlton at liblime.com
1105 =cut
1106 FIXME: deal with .htaccess