Initial work on adding Win32 support to installer.
[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 use Cwd;
28
29 my $DEBUG = 0;
30 die "perl 5.6.1 or later required" unless ($] >= 5.006001);
31
32 # Hash up directory structure & files beginning with the directory we were called from (should be the base of koha)...
33
34 my $dirtree = hashdir('.');
35 my %result = ();
36
37 =head1 NAME
38
39 Makefile.PL - Koha packager and installer
40
41 =head1 SYNOPSIS
42
43 =head2 BASIC INSTALLATION
44
45         perl Makefile.PL
46         make
47         sudo make install
48
49 =head2 PACKAGING RELEASE TARBALLS
50
51         make manifest tardist
52         make manifest zipdist
53
54 =head2 CLEANING UP
55
56         make clean
57
58 =head1 DESCRIPTION
59
60 This is a packager and installer that uses
61 ExtUtils::MakeMaker, which is fairly common
62 on perl systems.
63 As well as building tar or zip files
64 and installing with the above commands,
65 it allows us to check pre-requisites
66 and generate configuration files.
67
68 =head1 VARIABLES
69
70 =head2 NAME, VERSION_FROM, ABSTRACT, AUTHOR
71
72 Basic metadata about this software.
73
74 =head2 NO_META
75
76 Suppress generation of META.yml file.
77
78 =head2 PREREQ_PM
79
80 Hash of perl modules and versions required.
81
82 =head2 PM
83
84 Hash of file mappings
85
86 =head2 PL_FILES
87
88 This is a hash of PL scripts to run after installation and
89 the files to ask them to generate.
90 Maybe use the values from CONFIGURE
91 to generate initial configuration files in future.
92
93 =cut
94
95 =head2 target_map
96
97 This is a hash mapping directories and files in the
98 source tree to installation target directories.  The rules
99 for this mapping are:
100
101 =over 4
102
103 =item If a directory or file is specified, it and its
104 contents will be copied to the installation target directory.
105
106 =item If a subdirectory of a mapped directory is specified,
107 its target overrides the parent's target for that subdirectory.
108
109 =item The value of each map entry may either be a scalar containing 
110 one target or a reference to a hash containing 'target' and 'trimdir'
111 keys.
112
113 =item Any files at the top level of the source tree that are
114 not included in the map will not be installed.
115
116 =item Any directories at the top level of the source tree
117 that are not included in the map will be installed in
118 INTRANET_CGI_DIR.  This is a sensible default given the
119 current organization of the source tree, but (FIXME) it
120 would be better to reorganize the source tree to better
121 match the installation system, to allow adding new directories
122 without having to adjust Makefile.PL each time.  The idea
123 is to make the C<$target_map> hash as minimal as possible.
124
125 =back
126
127 The permitted installation targets are:
128
129 =over 4
130
131 =item INTRANET_CGI_DIR 
132
133 CGI scripts for intranet (staff) interface.
134
135 =item INTRANET_TMPL_DIR
136
137 HTML templates for the intranet interface.
138
139 =item INTRANET_WWW_DIR
140
141 HTML files, images, etc. for DocumentRoot for the intranet interface.
142
143 =item OPAC_CGI_DIR
144
145 CGI scripts for OPAC (public) interface.
146
147 =item OPAC_TMPL_DIR
148
149 HTML templates for the OPAC interface.
150
151 =item OPAC_WWW_DIR
152
153 HTML files, images, etc. for DocumentRoot for the OPAC interface.
154
155 =item PERL_MODULE_DIR
156
157 Perl modules (at present just the C4 modules) that are intimately
158 tied to Koha.  Depending on the installation options, these
159 may or may not be installed one of the standard directories
160 in Perl's default @LIB.
161
162 =item KOHA_CONF_DIR
163
164 Directory for Koha configuration files.
165
166 =item ZEBRA_CONF_DIR
167
168 Directory for Zebra configuration files.
169
170 =item ZEBRA_LOCK_DIR
171
172 Directory for Zebra's lock files.
173
174 =item ZEBRA_DATA_DIR
175
176 Directory for Zebra's data files.
177
178 =item ZEBRA_RUN_DIR
179
180 Directory for Zebra's UNIX-domain sockets.
181
182 =item MISC_DIR
183
184 Directory for for miscellaenous scripts, among other
185 things the translation toolkit and RSS feed tools.
186
187 =item SCRIPT_DIR
188
189 Directory for command-line scripts and daemons.
190
191 =item MAN_DIR
192
193 Directory for man pages created from POD -- will mostly
194 contain information of interest to Koha developers.
195
196 =item DOC_DIR
197
198 Directory for Koha documentation accessed from the 
199 command-line, e.g., READMEs.
200
201 =item LOG_DIR
202
203 Directory for Apache and Zebra logs produced by Koha.
204
205 =item NONE
206
207 This is a dummy target used to explicitly state
208 that a given file or directory is not to be installed.
209 This is used either for parts of the installer itself
210 or for development tools that are not applicable to a
211 production installation.
212
213 =back
214
215 =cut
216
217 my $target_map = {
218   './about.pl'                  => 'INTRANET_CGI_DIR',
219   './acqui'                     => 'INTRANET_CGI_DIR',
220   './admin'                     => 'INTRANET_CGI_DIR',
221   './authorities'               => 'INTRANET_CGI_DIR',
222   './C4'                        => 'PERL_MODULE_DIR',
223   './C4/SIP/t'                  => 'NONE',
224   './C4/SIP/koha_test'          => 'NONE',
225   './C4/tests'                  => 'NONE',
226   './catalogue'                 => 'INTRANET_CGI_DIR',
227   './cataloguing'               => 'INTRANET_CGI_DIR',
228   './changelanguage.pl'         => 'INTRANET_CGI_DIR',
229   './check_sysprefs.pl'         => 'NONE',
230   './circ'                      => 'INTRANET_CGI_DIR',
231   './edithelp.pl'               => 'INTRANET_CGI_DIR',
232   './etc'                       => { target => 'KOHA_CONF_DIR', trimdir => -1 },
233   './etc/zebradb'               => { target => 'ZEBRA_CONF_DIR', trimdir => -1 },
234   './help.pl'                   => 'INTRANET_CGI_DIR', 
235   './installer-CPAN.pl'         => 'NONE',
236   './installer'                 => 'INTRANET_CGI_DIR',
237   './koha-tmpl/errors'          => {target => 'INTRANET_CGI_DIR', trimdir => 2},
238   './koha-tmpl/intranet-tmpl'   => {target => 'INTRANET_TMPL_DIR', trimdir => -1},
239   './koha-tmpl/opac-tmpl'       => {target => 'OPAC_TMPL_DIR', trimdir => -1},
240   './kohaversion.pl'            => 'INTRANET_CGI_DIR', 
241   './labels'                    => 'INTRANET_CGI_DIR',
242   './mainpage.pl'               => 'INTRANET_CGI_DIR',
243   './Makefile.PL'               => 'NONE',
244   './MANIFEST.SKIP'             => 'NONE',
245   './members'                   => 'INTRANET_CGI_DIR',
246   './misc'                      => { target => 'SCRIPT_DIR', trimdir => -1 }, 
247   './misc/info'                 => { target => 'DOC_DIR', trimdir => 2 },
248   './misc/release notes'        => { target => 'DOC_DIR', trimdir => 2 },
249   './misc/translator'           => { target => 'MISC_DIR', trimdir => 2 }, 
250   './misc/installer_devel_notes' => 'NONE',
251   './opac'                      => 'OPAC_CGI_DIR',
252   './README.txt'                => 'NONE',
253   './reports'                   => 'INTRANET_CGI_DIR',
254   './reserve'                   => 'INTRANET_CGI_DIR',
255   './reviews'                   => 'INTRANET_CGI_DIR',
256   './rewrite-config.PL'         => 'NONE',
257   './reviews'                   => 'INTRANET_CGI_DIR',
258   './rss'                       => 'MISC_DIR', 
259   './serials'                   => 'INTRANET_CGI_DIR',
260   './skel'                      => 'NONE',
261   './skel/var/log/koha'         => { target => 'LOG_DIR', trimdir => -1 },
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 need to determine whether 
278                                            # Koha generates any persistent temp files
279                                            # that should go in /var/tmp/koha
280   './tools'                     => 'INTRANET_CGI_DIR',
281   './virtualshelves'            => 'INTRANET_CGI_DIR',
282   # ignore files and directories created by the install itself
283   './pm_to_blib'                => 'NONE',
284   './blib'                      => 'NONE',
285 };
286
287 =head1 CONFIGURATION OPTIONS
288
289 The following configuration options are used by the installer.
290
291 =over 4
292
293 =item INSTALL_MODE
294
295 Specifies whether installation will be FHS-compliant (default,
296 assumes user has root), put everything under
297 a single directory (for users installing on a web host
298 that allows CGI scripts and a MySQL database but not root 
299 access), or development (for a developer who wants to run
300 Koha from a git clone with no fuss).
301
302 =item INSTALL_BASE
303
304 Directory under which most components will go.  Default
305 value will vary depending on INSTALL_MODE.
306
307 =item DB_TYPE
308
309 Type of DBMS (e.g., mysql or Pg).
310
311 =item DB_HOST
312
313 Name of DBMS server.
314
315 =item DB_PORT
316
317 Port that DBMS server is listening on.
318
319 =item DB_NAME
320
321 Name of the DBMS database for Koha.
322
323 =item DB_USER
324
325 Name of DBMS user account for Koha's database.
326
327 =item DB_PASS
328
329 Pasword of DMBS user account for Koha's database.
330
331 =item INSTALL_ZEBRA
332
333 Whether to install Zebra configuration files and data
334 directories.
335
336 =item ZEBRA_MARC_FORMAT
337
338 Specifies format of MARC records to be indexed by Zebra.
339
340 =item ZEBRA_LANGUAGE
341
342 Specifies primary language of records that will be 
343 indexed by Zebra.
344
345 =item ZEBRA_USER
346
347 Internal Zebra user account for the index.
348
349 =item ZEBRA_PASS
350
351 Internal Zebra user account's password.
352
353 =item KOHA_USER
354
355 System user account that will own Koha's files.
356
357 =item KOHA_GROUP
358
359 System group that will own Koha's files.
360
361 =back
362
363 =cut
364
365 # default configuration options
366 my %config_defaults = (
367   'DB_TYPE'           => 'mysql',
368   'DB_HOST'           => 'localhost',
369   'DB_NAME'           => 'koha',    
370   'DB_USER'           => 'kohaadmin',
371   'DB_PASS'           => 'katikoan',
372   'INSTALL_ZEBRA'     => 'yes',
373   'ZEBRA_MARC_FORMAT' => 'marc21',
374   'ZEBRA_LANGUAGE'    => 'en',
375   'ZEBRA_USER'        => 'kohauser',
376   'ZEBRA_PASS'        => 'zebrastripes',
377   'KOHA_USER'         => 'koha',
378   'KOHA_GROUP'        => 'koha',
379 );
380
381 # set some default configuratio options based on OS
382 # more conditions need to be added for other OS's
383 # this should probably also incorporate usage of Win32::GetOSName() and/or Win32::GetOSVersion()
384 # to allow for more granular decisions based on which Win32 platform
385
386 warn "Your platform appears to be $^O.\n" if $DEBUG;
387
388 if ( $^O eq 'MSWin32' ) {
389         # Most Unix2Win32 ports seem to poke everything into the Program Files directory
390         # this could be changed to put some files (ie. libraries) into system32, etc.
391         $config_defaults{'INSTALL_MODE'} = 'single';
392         $config_defaults{'INSTALL_BASE'} = 'c:/progra~1/koha';  # Use 8.3 names to be safe...
393 }
394 elsif ( $^O eq 'cygwin' ) {
395         # Most Unix2Win32 ports seem to poke everything into the Program Files directory
396         # this could be changed to put some files (ie. libraries) into system32, etc.
397         $config_defaults{'INSTALL_MODE'} = 'single';
398         $config_defaults{'INSTALL_BASE'} = 'c:/progra~1/koha';  # Use 8.3 names to be safe...
399 }
400 else {
401         $config_defaults{'INSTALL_MODE'} = 'standard';
402         $config_defaults{'INSTALL_BASE'} = '/usr/share/koha';
403 }
404
405 # valid values for certain configuration options
406 my %valid_config_values = (
407   'INSTALL_MODE'  => { 'standard' => 1, 'single' => 1, 'dev' => 1 },
408   'DB_TYPE' => { 'mysql' => 1, 'Pg' => 1 },
409   'INSTALL_ZEBRA' => { 'yes' => 1, 'no' => 1 },
410   'ZEBRA_MARC_FORMAT' => { 'marc21' => 1, 'unimarc' => 1 }, # FIXME should generate from contents of distributation
411   'ZEBRA_LANGUAGE'    => { 'en' => 1, 'fr' => 1 }, # FIXME should generate from contents of distribution
412 );
413
414 my %config = get_configuration(\%config_defaults, \%valid_config_values);
415 my ($target_directories, $skip_directories) = get_target_directories(\%config);
416 display_configuration(\%config, $target_directories);
417 my $file_map = {};
418 get_file_map($target_map, $dirtree, $file_map, $config{'INSTALL_ZEBRA'} eq "yes" ? 1: 0);
419
420 my $pl_files = {
421       'rewrite-config.PL' => [
422          'blib/KOHA_CONF_DIR/koha-conf.xml',
423          'blib/KOHA_CONF_DIR/koha-httpd.conf',
424          ]
425 };
426
427 if ($config{'INSTALL_ZEBRA'} eq "yes") {
428     push @{ $pl_files->{'rewrite-config.PL'} }, (
429         'blib/ZEBRA_CONF_DIR/etc/passwd',
430         'blib/ZEBRA_CONF_DIR/zebra-biblios.cfg',
431         'blib/ZEBRA_CONF_DIR/zebra-authorities.cfg'
432     );
433 }
434
435 if ($config{'INSTALL_MODE'} ne "dev") {
436     push @{ $pl_files->{'rewrite-config.PL'} }, (
437         'blib/PERL_MODULE_DIR/C4/Context.pm',
438         'blib/SCRIPT_DIR/kohalib.pl'
439     );
440 }
441
442 WriteMakefile(
443     NAME => 'koha',
444     #VERSION => strftime('2.9.%Y%m%d%H',gmtime),
445     VERSION_FROM => 'kohaversion.pl',
446     ABSTRACT => 'Award-winning integrated library system (ILS) and Web OPAC',
447     AUTHOR => 'Koha Developers <koha-devel@nongnu.org>',
448     NO_META => 1,
449     PREREQ_PM => {
450 'Algorithm::CheckDigits' => 0.48,
451 'Biblio::EndnoteStyle' => 0.05,
452 'CGI' => 3.15,
453 'CGI::Carp' => 1.29,
454 'CGI::Session' => '4.10',
455 'Class::Factory::Util' => 1.7,
456 'Class::Accessor' => 0.30,
457 'DBD::mysql' => 3.0008,
458 'DBI' => 1.53,
459 'Data::ICal' => 0.13,
460 'Data::Dumper' => 2.121_08,
461 'Date::Calc' => 5.4,
462 'Date::ICal' => 1.72,
463 'Date::Manip' => 5.44,
464 'Digest::MD5' => 2.36,
465 'File::Temp' => 0.16,
466 'GD::Barcode::UPCE' => 1.1,
467 'Getopt::Long' => 2.35,
468 'Getopt::Std' => 1.05,
469 'HTML::Template::Pro' => 0.65,
470 'HTTP::Cookies' => 1.39,
471 'HTTP::Request::Common' => 1.26,
472 'LWP::Simple' => 1.41,
473 'LWP::UserAgent' => 2.033,
474 'Lingua::Stem' => 0.82,
475 'List::Util' => 1.18,
476 'List::MoreUtils' => 0.22,
477 'Locale::Language' => 2.07,
478 'MARC::Charset' => 0.98,
479 'MARC::Crosswalk::DublinCore' => 0.02,
480 'MARC::File::XML' => 0.88,
481 'MARC::Record' => 2.00,
482 'MIME::Base64' => 3.07,
483 'MIME::QuotedPrint' => 3.07,
484 'Mail::Sendmail' => 0.79,
485 'Net::LDAP' => 0.34,
486 'Net::LDAP::Filter' => 0.34,
487 'Net::Z3950::ZOOM' => 1.16,
488 'PDF::API2' => 2.000,
489 'PDF::API2::Page' => 2.000,
490 'PDF::API2::Util' => 2.000,
491 'PDF::Reuse' => 0.33,
492 'PDF::Reuse::Barcode' => 0.05,
493 'POE' => 0.9999,
494 'POSIX' => 1.09,
495 'Schedule::At' => 1.06,
496 'Term::ANSIColor' => 1.10,
497 'Test' => 1.25,
498 'Test::Harness' => 2.56,
499 'Test::More' => 0.62,
500 'Text::CSV' => 0.01,
501 'Text::CSV_XS' => 0.32,
502 'Text::Wrap' => 2005.082401,
503 'Time::HiRes' => 1.86,
504 'Time::localtime' => 1.02,
505 'Unicode::Normalize' => 0.32,
506 'XML::Dumper' => 0.81,
507 'XML::LibXML' => 1.59,
508 'XML::LibXSLT' => 1.63,
509 'XML::SAX::ParserFactory' => 1.01,
510 'XML::Simple' => 2.14,
511 'XML::RSS' => 1.31,
512         },
513
514         # File tree mapping
515         PM => $file_map,
516
517     # Man pages generated from POD
518     INSTALLMAN1DIR => File::Spec->catdir($target_directories->{'MAN_DIR'}, 'man1'),
519     INSTALLMAN3DIR => File::Spec->catdir($target_directories->{'MAN_DIR'}, 'man3'),
520
521    PL_FILES => $pl_files,
522
523 );
524
525 =head1 FUNCTIONS
526
527 =head2 hashdir
528
529 This function recurses through the directory structure and builds
530 a hash of hashes containing the structure with arrays holding filenames.
531 This directory hashing routine was taken from BrowserUK @ http://www.perlmonks.org/?node_id=219919
532
533 =cut
534
535 sub hashdir{
536     my $dir = shift;
537     opendir my $dh, $dir or die $!;
538     my $tree = {}->{$dir} = {};
539     while( my $file = readdir($dh) ) {
540         next if $file =~ m/^\.{1,2}/ and $file !~ /^\.htaccess/; # .htaccess is a special case
541         my $path = $dir .'/' . $file;
542         $tree->{$file} = hashdir($path), next if -d $path;
543         push @{$tree->{'.'}}, $file;
544     }
545     return $tree;
546 }
547
548 =head2 get_file_map 
549
550 This function combines the target_map and file hash to
551 map each source file to its destination relative to
552 the set of installation targets.
553
554 Output will be a hash mapping from each source file
555 to its destination value, like this:
556
557 'mainpage.pl' => '$(INTRANET_CGI_DIR)/mainpage.pl'
558
559 =cut
560
561 sub get_file_map {
562     my $target_map = shift;
563     my $dirtree = shift;
564     my $file_map = shift;
565     my $install_zebra = shift;
566     my $curr_path = @_ ? shift : ['.'];
567
568     # Traverse the directory tree.
569     # For each file or directory, identify the
570     # most specific match in the target_map
571     foreach my $dir (sort keys %{ $dirtree }) {
572         if ($dir eq '.') {
573             # deal with files in directory
574             foreach my $file (sort @{ $dirtree->{$dir} }) {
575                 my $targetdir = undef;
576                 my $matchlevel = undef;
577                 # first, see if there is a match on this specific
578                 # file in the target map
579                 my $filepath = join("/", @$curr_path, $file);
580                 if (exists $target_map->{$filepath}) {
581                     $targetdir = $target_map->{$filepath};
582                     $matchlevel = scalar(@$curr_path) + 1;
583                 } else {
584                     # no match on the specific file; look for
585                     # a directory match
586                     for (my $i = scalar(@$curr_path) - 1; $i >= 0; $i--)  {
587                         my $dirpath = join("/", @$curr_path[0..$i]);
588                         if (exists $target_map->{$dirpath}) {
589                             $targetdir = $target_map->{$dirpath};
590                             $matchlevel = $i + 1;
591                             last;
592                         }
593                     }
594                 }
595                 if (defined $targetdir) {
596                      _add_to_file_map($file_map, $targetdir, $curr_path, $file, $matchlevel, $install_zebra);
597                 } else {
598                     my $path = join("/", @$curr_path);
599                     print "failed to map: $path/$file\n" if $DEBUG;
600                 }
601             }
602         } else {
603             # dealing with subdirectory
604             push @$curr_path, $dir;
605             get_file_map($target_map, $dirtree->{$dir}, $file_map, $install_zebra, $curr_path);
606             pop @$curr_path;
607         }
608     }
609 }
610
611 sub _add_to_file_map {
612     my $file_map = shift;
613     my $targetdir = shift;
614     my $curr_path = shift;
615     my $file = shift;
616     my $matchlevel = shift;
617     my $install_zebra = shift;
618     my $dest_path = @_ ? shift : $curr_path;
619
620     # The target can be one of the following:
621     # 1. scalar representing target symbol
622     # 2. hash ref containing target and trimdir keys
623     #
624     # Consequently, this routine traverses this structure,
625     # calling itself recursively, until it deals with
626     # all of the scalar target symbols.
627     if (ref $targetdir eq 'HASH') {
628         my $subtarget = $targetdir->{target};
629         if (exists $targetdir->{trimdir}) {
630             # if we get here, we've specified that
631             # rather than installing the file to
632             # $(TARGET)/matching/dirs/subdirs/file,
633             # we want to install it to
634             # $(TARGET)/subdirs/file
635             #
636             # Note that this the only place where
637             # $matchlevel is used.
638             my @new_dest_path = @$dest_path;
639             if ($targetdir->{trimdir} == -1)  {
640                 splice @new_dest_path, 0, $matchlevel;
641             } else {
642                 splice @new_dest_path, 0, $targetdir->{trimdir};
643             }
644             _add_to_file_map($file_map, $subtarget, $curr_path, $file, $matchlevel, $install_zebra, \@new_dest_path);
645         } else {
646             # actually getting here means that the
647             # target was unnecessarily listed
648             # as a hash, but we'll forgive that
649             _add_to_file_map($file_map, $subtarget, $curr_path, $file, $matchlevel, $install_zebra);
650         }
651     } elsif ($targetdir ne 'NONE' and $targetdir ne '') {
652         my $source = File::Spec->catfile(@$curr_path, $file);
653         my $destination = File::Spec->catfile('blib', $targetdir, @$dest_path, $file);
654         #print "$source => $destination\n"; # DEBUG
655         # quote spaces in file names
656         # FIXME: this is of questionable portability and
657         # probably depends on user's make recognizing this
658         # quoting syntax -- probably better to remove
659         # spaces and shell metacharacters from all file names
660         $source =~ s/ /\\ /g;
661         $destination =~ s/ /\\ /g;
662       
663         $file_map->{$source} = $destination unless (!$install_zebra and $targetdir =~ /ZEBRA/);
664     }
665 }
666
667 =head2 get_configuration_options
668
669 This prompts the user for various configuration options.
670
671 =cut
672
673 sub get_configuration {
674   my $defaults = shift;
675   my $valid_values = shift;
676   my %config = ();
677
678   my $msg = q(
679 By default, Koha can be installed in one of three ways:
680
681 standard: Install files in conformance with the Filesystem
682           Hierarchy Standard (FHS).  This is the default mode
683           and should be used when installing a production
684           Koha system.  On Unix systems, root access is 
685           needed to complete a standard installation.
686
687 single:   Install files under a single directory.  This option
688           is useful for installing Koha without root access, e.g.,
689           on a web host that allows CGI scripts and MySQL databases
690           but requires the user to keep all files under the user's
691           HOME directory.
692
693 dev:      Create a set of symbolic links and configuration files to
694           allow Koha to run directly from the source distribution.
695           This mode is useful for developers who want to run
696           Koha from a git clone.
697
698 Installation mode);
699     $msg .= _add_valid_values_disp('INSTALL_MODE', $valid_values);
700     $config{'INSTALL_MODE'} = _get_value('INSTALL_MODE', $msg, $defaults->{'INSTALL_MODE'}, $valid_values);
701
702     # set message and default value for INSTALL_BASE
703     # depending on value of INSTALL_MODE
704     my $install_base_default = $defaults->{'INSTALL_BASE'};
705     if ($config{'INSTALL_MODE'} eq 'dev') {
706         $msg = q(
707 Please specify the directory in which to install Koha's
708 active configuration files and (if applicable) the
709 Zebra database.  Koha's CGI scripts and templates will
710 be run from the current directory.
711
712 Configuration directory:);
713         # FIXME - home directory portability consideration apply
714         $install_base_default = (exists $ENV{'HOME'}) ? "$ENV{'HOME'}/koha-dev" : "$defaults->{'INSTALL_BASE'}-dev";
715     } elsif ($config{'INSTALL_MODE'} eq 'single') {
716         $msg = "\nPlease specify the directory in which to install Koha";
717         # FIXME -- we're assuming under a 'single' mode install
718         # that user will likely want to install under the home
719         # directory.  This is OK in and of itself, but we should
720         # use File::HomeDir to locate the home directory portably.  
721         # This is deferred for now because File::HomeDir is not yet
722         # core.
723                 # --we must also keep this portable to the major OS's -fbcit
724         $install_base_default = (exists $ENV{'HOME'}) ? "$ENV{'HOME'}/koha" : $defaults->{'INSTALL_BASE'};
725     } else {
726         # must be standard
727         $msg = q(
728 Please specify the directory under which most Koha files 
729 will be installed.
730
731 Note that if you are planning in installing more than 
732 one instance of Koha, you may want to modify the last
733 component of the directory path, which will be used
734 as the package name in the FHS layout.
735
736 Base installation directory);
737     }
738     $config{'INSTALL_BASE'} = _get_value('INSTALL_BASE', $msg, $install_base_default, $valid_values);
739
740     $config{'INSTALL_BASE'} = File::Spec->rel2abs($config{'INSTALL_BASE'});
741         print "INSTALL_BASE=$config{'INSTALL_BASE'}\r\n" if $DEBUG;
742     if ($config{'INSTALL_MODE'} eq "standard") {
743         $msg = q(
744 Since you are using the 'standard' install
745 mode, you should run 'make install' as root.
746 However, it is recommended that a non-root
747 user (on Unix and Linux platforms) have 
748 ownership of Koha's files, including the
749 Zebra indexes if applicable.
750
751 Please specify a user account.  This
752 user account does not need to exist
753 right now, but it needs to exist
754 before you run 'make install'.  Please
755 note that for security reasons, this
756 user should not be the same as the user
757 account Apache runs under.
758
759 User account);
760         $config{'KOHA_USER'} = _get_value('KOHA_USER', $msg, $defaults->{'KOHA_USER'}, $valid_values);
761
762         $msg = q(
763 Please specify the group that should own
764 Koha's files.  As above, this group need
765 not exist right now, but should be created
766 before you run 'make install'.
767
768 Group);
769         $config{'KOHA_GROUP'} = _get_value('KOHA_GROUP', $msg, $defaults->{'KOHA_GROUP'}, $valid_values);
770     }
771
772     $msg = q(
773 Please specify which database engine you will use
774 to store data in Koha.  The choices are MySQL and
775 PostgreSQL; please note that at the moment
776 PostgreSQL support is highly experimental.
777
778 DBMS to use);
779     $msg .= _add_valid_values_disp('DB_TYPE', $valid_values);
780     $config{'DB_TYPE'} = _get_value('DB_TYPE', $msg, $defaults->{'DB_TYPE'}, $valid_values);
781
782     $msg = q(
783 Please specify the name or address of your 
784 database server.  Note that the database 
785 does not have to exist at this point, it
786 can be created after running 'make install'
787 and before you try using Koha for the first time.
788
789 Database server);
790     $config{'DB_HOST'} = _get_value('DB_HOST', $msg, $defaults->{'DB_HOST'}, $valid_values);
791
792     $msg = q(
793 Please specify the port used to connect to the
794 DMBS);
795     my $db_port_default = $config{'DB_TYPE'} eq 'mysql' ? '3306' : '5432';
796     $config{'DB_PORT'} = _get_value('DB_PORT', $msg, $db_port_default, $valid_values);
797
798     $msg = q(
799 Please specify the name of the database to be
800 used by Koha);
801     $config{'DB_NAME'} = _get_value('DB_NAME', $msg, $defaults->{'DB_NAME'}, $valid_values);
802
803     $msg = q(
804 Please specify the user that owns the database to be
805 used by Koha);
806     $config{'DB_USER'} = _get_value('DB_USER', $msg, $defaults->{'DB_USER'}, $valid_values);
807
808     $msg = q(
809 Please specify the password of the user that owns the 
810 database to be used by Koha);
811     $config{'DB_PASS'} = _get_value('DB_PASS', $msg, $defaults->{'DB_PASS'}, $valid_values);
812
813     $msg = q(
814 Koha can use the Zebra search engine for high-performance
815 searching of bibliographic and authority records.  If you
816 have installed the Zebra software and would like to use it,
817 please answer 'yes' to the following question.  Otherwise, 
818 Koha will default to using its internal search engine.
819
820 Please note that if you choose *NOT* to install Zebra,
821 koha-conf.xml will still contain some references to Zebra
822 settings.  Those references will be ignored by Koha.
823
824 Install the Zebra configuration files?);
825     $msg .= _add_valid_values_disp('INSTALL_ZEBRA', $valid_values);
826     $config{'INSTALL_ZEBRA'} = _get_value('INSTALL_ZEBRA', $msg, $defaults->{'INSTALL_ZEBRA'}, $valid_values);
827
828     if ($config{'INSTALL_ZEBRA'} eq 'yes') {
829         $msg = q(
830 Since you've chosen to use Zebra with Koha,
831 you must specify the primary MARC format of the
832 records to be indexed by Zebra.
833
834 Koha provides Zebra configuration files for MARC 21
835 and UNIMARC.
836
837 MARC format for Zebra indexing);
838         $msg .= _add_valid_values_disp('ZEBRA_MARC_FORMAT', $valid_values);
839         $config{'ZEBRA_MARC_FORMAT'} = _get_value('ZEBRA_MARC_FORMAT', $msg, $defaults->{'ZEBRA_MARC_FORMAT'}, $valid_values);
840         $msg = q(
841 Koha supplies Zebra configuration files tuned for
842 searching either English (en) or French (fr) MARC
843 records.
844
845 Primary language for Zebra indexing);
846         $msg .= _add_valid_values_disp('ZEBRA_LANGUAGE', $valid_values);
847         $config{'ZEBRA_LANGUAGE'} = _get_value('ZEBRA_LANGUAGE', $msg, $defaults->{'ZEBRA_LANGUAGE'}, $valid_values);
848
849         $msg = q(
850 Please specify Zebra database user);
851         $config{'ZEBRA_USER'} = _get_value('ZEBRA_USER', $msg, $defaults->{'ZEBRA_USER'}, $valid_values);
852
853         $msg = q(
854 Please specify the Zebra database password);
855         $config{'ZEBRA_PASS'} = _get_value('ZEBRA_PASS', $msg, $defaults->{'ZEBRA_PASS'}, $valid_values);
856
857     }
858
859     print "\n\n";
860     return %config;
861 }
862
863 sub _add_valid_values_disp {
864     my $key = shift;
865     my $valid_values = shift;
866    
867     my $disp = "";
868     if (exists $valid_values->{$key}) {
869         $disp = " (" . join(", ", sort keys %{ $valid_values->{$key} }) . ")";
870     }
871     return $disp;
872 }
873
874 sub _get_value {
875     my $key = shift;
876     my $msg = shift;
877     my $default = shift;
878     my $valid_values = shift;
879
880     # override default value from environment
881     if (exists $ENV{$key}) {
882         $default = $ENV{$key};
883         $msg .= " (default from environment)";
884     }
885
886     my $val = prompt($msg, $default);
887
888     while (exists $valid_values->{$key} and 
889            $val ne $default and
890            not exists $valid_values->{$key}->{$val}) {
891         my $retry_msg = "Value '$val' is not a valid option.\n";
892         $retry_msg .= "Please enter a value";
893         $retry_msg .= _add_valid_values_disp($key, $valid_values);
894         $val = prompt($retry_msg, $default);
895     }
896     return $val;
897 }
898
899 =head2 get_target_directories 
900
901 Creates a hash mapping from symbols for installation target
902 directories to actual directory paths.
903
904 Also returns a hash indicating targets for which 
905 files need not be copied -- this is used for the 'dev'
906 mode installation, where some files are installed in place.
907
908 =cut
909
910 sub get_target_directories {
911     my $config = shift;
912
913     my $base = $config->{'INSTALL_BASE'};
914     my $mode = $config->{'INSTALL_MODE'};
915
916     # get last component of install base directory
917     # to treat as package name
918     my ($volume, $directories, $file) = File::Spec->splitpath($base, 1);
919
920     my @basedir = File::Spec->splitdir($directories);
921
922         # for Win32 we need to prepend the volume to the directory path
923         if ( $^O eq 'MSWin32' ) { shift @basedir; unshift @basedir, $volume; }
924         elsif ( $^O eq 'cygwin' ) { shift @basedir; unshift @basedir, 'c:'; }   # in a cygwin environment, $volume is returned empty
925
926     my $package = pop @basedir;
927
928
929     my %dirmap = ();
930     my %skipdirs = ();
931     if ($mode eq 'single') {
932         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'cgi-bin');
933         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs', 'intranet-tmpl');
934         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs');
935         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'cgi-bin');
936         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs', 'opac-tmpl');
937         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs');
938         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir(@basedir, $package, 'lib');
939         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc');
940         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'zebradb');
941         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
942         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
943         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
944         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
945         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb');
946         $dirmap{'LOG_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'log');
947         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'zebradb');
948         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'run', 'zebradb');
949     } elsif ($mode eq 'dev') {
950         my $curdir = File::Spec->rel2abs(File::Spec->curdir());
951         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir($curdir);
952         $skipdirs{'INTRANET_CGI_DIR'} = 1;
953         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'intranet-tmpl');
954         $skipdirs{'INTRANET_TMPL_DIR'} = 1;
955         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl');
956         $skipdirs{'INTRANET_WWW_DIR'} = 1;
957         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir($curdir, 'opac');
958         $skipdirs{'OPAC_CGI_DIR'} = 1;
959         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl', 'opac-tmpl');
960         $skipdirs{'OPAC_TMPL_DIR'} = 1;
961         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir($curdir, 'koha-tmpl');
962         $skipdirs{'OPAC_WWW_DIR'} = 1;
963         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir($curdir);
964         $skipdirs{'PERL_MODULE_DIR'} = 1;
965         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc');
966         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(@basedir, $package, 'etc', 'zebradb');
967         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
968         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
969         $skipdirs{'SCRIPT_DIR'} = 1;
970         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
971         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
972         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(@basedir, $package, 'var', 'lock', 'zebradb');
973         $dirmap{'LOG_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'log');
974         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'lib', 'zebradb');
975         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(@basedir, $package, 'var', 'run', 'zebradb');
976     } else {
977         # mode is standard, i.e., 'fhs'
978         $dirmap{'INTRANET_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'cgi-bin');
979         $dirmap{'INTRANET_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs', 'intranet-tmpl');
980         $dirmap{'INTRANET_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'intranet', 'htdocs');
981         $dirmap{'OPAC_CGI_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'cgi-bin');
982         $dirmap{'OPAC_TMPL_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs', 'opac-tmpl');
983         $dirmap{'OPAC_WWW_DIR'} = File::Spec->catdir(@basedir, $package, 'opac', 'htdocs');
984         $dirmap{'PERL_MODULE_DIR'} = File::Spec->catdir(@basedir, $package, 'lib');
985         $dirmap{'KOHA_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package);
986         $dirmap{'ZEBRA_CONF_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'etc', $package, 'zebradb');
987         $dirmap{'MISC_DIR'} = File::Spec->catdir(@basedir, $package, 'misc');
988         $dirmap{'SCRIPT_DIR'} = File::Spec->catdir(@basedir, $package, 'bin');
989         $dirmap{'MAN_DIR'} = File::Spec->catdir(@basedir, $package, 'man');
990         $dirmap{'DOC_DIR'} = File::Spec->catdir(@basedir, $package, 'doc');
991         $dirmap{'ZEBRA_LOCK_DIR'} = File::Spec->catdir(File::Spec->rootdir(), 'var', 'lock', $package, 'zebradb');
992         $dirmap{'LOG_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'log', $package);
993         $dirmap{'ZEBRA_DATA_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'lib', $package, 'zebradb');
994         $dirmap{'ZEBRA_RUN_DIR'} =  File::Spec->catdir(File::Spec->rootdir(), 'var', 'run', $package, 'zebradb');
995     }
996
997     _get_env_overrides(\%dirmap);
998     _get_argv_overrides(\%dirmap);
999
1000     return \%dirmap, \%skipdirs;
1001 }
1002
1003 sub _get_env_overrides {
1004     my $dirmap = shift;
1005
1006     foreach my $key (keys %$dirmap) {
1007         if (exists $ENV{$key}) {
1008             $dirmap->{$key} = $ENV{$key};
1009             print "Setting $key from environment\n";
1010         }
1011     }
1012 }
1013
1014 sub _get_argv_overrides {
1015     my $dirmap = shift;
1016     
1017     my @new_argv = ();
1018     for (my $i = 0; $i <= $#ARGV; $i++) {
1019         if ($ARGV[$i] =~ /^([^=]+)=([^=]+)$/ and exists $dirmap->{$1}) {
1020             $dirmap->{$1} = $2;
1021         } else {
1022             push @new_argv, $ARGV[$i];
1023         }
1024     }
1025     @ARGV = @new_argv;
1026 }
1027
1028 sub display_configuration {
1029     my $config = shift;
1030     my $dirmap = shift;
1031     print "\n\nKoha will be installed with the following configuration parameters:\n\n";
1032     foreach my $key (sort keys %$config) {
1033         print sprintf("%-25.25s%s\n", $key, $config->{$key});
1034     }
1035
1036     print "\nand in the following directories:\n\n";
1037     foreach my $key (sort keys %$dirmap) {
1038         print sprintf("%-25.25s%s\n", $key, $dirmap->{$key});
1039     }
1040     print "\n\nTo change any configuration setting, please run\n";
1041     print "perl Makefile.PL again.  To override one of the target\n";
1042     print "directories, you can do so on the command line like this:\n";
1043     print "\nperl Makefile.PL PERL_MODULE_DIR=/usr/share/perl/5.8\n\n";
1044     print "You can also set different default values for parameters\n";
1045     print "or override directory locations by using environment variables.\n";
1046     print "\nFor example:\n\n";
1047     print "export DB_USER=my_koha\n";
1048     print "perl Makefile.PL\n";
1049     print "\nor\n\n";
1050     print "DB_USER=my_koha DOC_DIR=/usr/local/info perl Makefile.PL\n\n";
1051 }
1052
1053 =head2 fixshebang
1054
1055 This sub will recurse through a given directory and its subdirectories checking for the existence of a shebang
1056 line in .pl files and replacing it with the correct line for the current OS if needed. It should be called
1057 in a manner similar to 'fixshebang (getcwd())' but may be supplied with any directory in absolute path form.
1058
1059 =cut
1060
1061 sub fixshebang{
1062         # NOTE: this might be dressed up a bit with File::Spec since we're using it here already.
1063         my $dir = shift;
1064     opendir my $dh, $dir or die $!;
1065     while( my $file = readdir($dh) ) {
1066                 print "Reading $dir contents.\r\n" if $DEBUG;
1067                 print "Current item = $file\r\n" if $DEBUG;
1068                 # this may be used to exclude any desired files from the scan
1069                 if ( $file =~ /shebang|wixgen/ ) { next; }
1070                 # handle files... other extensions could be substituted/added if needed
1071                 if ( $file =~ /\.pl$/ ) {
1072                         my $pathfile =$dir . '/' . $file;
1073                         print "Found a perl script named $pathfile\r\n" if $DEBUG;
1074                         open FH, '+<', "$pathfile";
1075                         my @file = <FH>;
1076                         seek( FH, 0, 0 );
1077                         truncate( FH, 0 );
1078                         my $line = shift @file;
1079                         print "Found shebang line: $line\r\n" if $DEBUG;
1080                         # FIXME: these conditionals need to be modified to select shebang line based on OS ($^O)
1081                         if ( $line =~ /#!c:\\strawberry-perl\\perl\\bin\\perl -w/ ) {
1082                                 # FIXME: This can probably be done with less of a hack. -fbcit
1083                                 print FH $line;
1084                                 print FH @file;
1085                                 close (FH);
1086                                 next;
1087                         }
1088                         elsif ( $line =~ /#!\/usr\/bin\/perl/ ) {
1089                                 print "Re-writing shebang line for $pathfile\r\n";
1090                                 print FH "#!c:\\strawberry-perl\\perl\\bin\\perl -w\n";
1091                                 print FH @file;
1092                                 close(FH);
1093                         }
1094                         else {
1095                                 # FIXME: This can probably be done with less of a hack. -fbcit
1096                                 print FH $line;
1097                                 print FH @file;
1098                                 close (FH);
1099                                 next;
1100                         }
1101                 }
1102                 # handle directories
1103                 elsif ( -d ($dir . '/' . $file) && $file !~ /^\.{1,2}/ ) {
1104                         my $pathfile = $dir . '/' . $file;
1105                         print "Found a subdir named $pathfile\r\n" if $DEBUG;
1106                         &fixshebang ($pathfile);
1107 #                       closedir $dh;   # I'm not really sure if this is necessary
1108                 }
1109         }
1110         closedir $dh;
1111 }
1112
1113 package MY;
1114
1115 # This will have to be reworked in order to accommodate Win32...
1116
1117 sub test {
1118     my $self = shift;
1119     my $test = $self->SUPER::test(@_);
1120     $test =~ s!\$\(INST_LIB\)!blib/PERL_MODULE_DIR!g;
1121     return $test;
1122 }
1123
1124 sub install {
1125     my $self = shift;
1126     my $install = ""; 
1127     # NOTE: we're *not* doing this: my $install = $self->SUPER::install(@_);
1128     # This means that we're completely overriding EU::MM's default
1129     # installation and uninstallation targets.
1130
1131 # If installation is on Win32, we need to do permissions different from *nix
1132     if ( $^O =~ /linux|cygwin/ ) { # this value needs to be verified for each platform and modified accordingly
1133             foreach my $key (sort keys %$target_directories) {
1134                     $install .= qq(
1135 KOHA_INST_$key = blib/$key
1136 KOHA_DEST_$key = $target_directories->{$key}
1137 )                       unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1138                 }
1139                 $install .= qq(
1140 install :: all install_koha set_koha_ownership set_koha_permissions warn_koha_env_vars
1141 \t\$(NOECHO) \$(NOOP)
1142 );
1143                         $install .= "install_koha ::\n";      
1144                         $install .= "\t\$(NOECHO) umask 022; \$(MOD_INSTALL) \\\n";
1145                         foreach my $key (sort keys %$target_directories) {
1146                                 $install .= "\t\t\$(KOHA_INST_$key) \$(KOHA_DEST_$key) \\\n" 
1147                                         unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1148                         }
1149                         $install .= "\t\t\$(INST_MAN1DIR) \$(DESTINSTALLMAN1DIR) \\\n";
1150                         $install .= "\t\t\$(INST_MAN3DIR) \$(DESTINSTALLMAN3DIR)\n";
1151
1152                         $install .= "\n";
1153                         $install .= "set_koha_ownership ::\n";
1154                         if ($config{'INSTALL_MODE'} eq 'standard' and $config{'KOHA_USER'} ne "root") {
1155                                 foreach my $key (sort keys %$target_directories) {
1156                                         $install .= "\t\$(NOECHO) chown -R $config{'KOHA_USER'}:$config{'KOHA_GROUP'} \$(KOHA_DEST_$key)\n"
1157                                                 unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1158                                 }
1159                         } else {
1160                                 $install .= "\t\t\$(NOECHO) \$(NOOP)\n\n";
1161                         }
1162
1163                         $install .= "\n";
1164                         $install .= "set_koha_permissions ::\n";
1165                         # This is necessary because EU::MM installs files
1166                         # as either 0444 or 0555, and we want the owner
1167                         # of Koha's files to have write permission by default.
1168                         foreach my $key (sort keys %$target_directories) {
1169                                 $install .= "\t\$(NOECHO) chmod -R u+w \$(KOHA_DEST_$key)\n"
1170                                         unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1171                         }
1172         }
1173         elsif ($^O eq 'MSWin32' ) {     # On Win32, the install probably needs to be done under the user account koha will be running as...
1174                                                                 # We can attempt some creative things with command line utils such as CACLS which allows permission
1175                                                                 # management from Win32 cmd.exe, but permissions really only apply to NTFS.
1176             foreach my $key (sort keys %$target_directories) {
1177                     $install .= qq(
1178 KOHA_INST_$key = blib/$key
1179 KOHA_DEST_$key = $target_directories->{$key}
1180 )                       unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1181                 }
1182                 $install .= qq(
1183 install :: all install_koha warn_koha_env_vars
1184 \t\$(NOECHO) \$(NOOP)
1185 );
1186                 $install .= "install_koha ::\n";
1187                 $install .= "\t\$(MOD_INSTALL) \\\n";
1188                 foreach my $key (sort keys %$target_directories) {
1189                         $install .= "\t\t\$(KOHA_INST_$key) \$(KOHA_DEST_$key) \\\n" 
1190                                 unless ($config{'INSTALL_ZEBRA'} ne "yes" and $key =~ /ZEBRA/) or exists $skip_directories->{$key};
1191                 }
1192         }
1193         $install .= "\t\t\$(INST_MAN1DIR) \$(DESTINSTALLMAN1DIR) \\\n";
1194         $install .= "\t\t\$(INST_MAN3DIR) \$(DESTINSTALLMAN3DIR)\n";
1195
1196         $install .= "\n";
1197
1198     $install .= "warn_koha_env_vars ::\n";
1199     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1200     $install .= "\t\$(NOECHO) \$(ECHO) Koha\\'s files have now been installed. \n";
1201     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1202     $install .= "\t\$(NOECHO) \$(ECHO) In order to use Koha\\'s command-line batch jobs,\n";
1203     $install .= "\t\$(NOECHO) \$(ECHO) you should set the following environment variables:\n";
1204     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1205     $install .= "\t\$(NOECHO) \$(ECHO) export KOHA_CONF=\$(KOHA_DEST_KOHA_CONF_DIR)/koha-conf.xml\n";
1206     $install .= "\t\$(NOECHO) \$(ECHO) export PERL5LIB=$target_directories->{'PERL_MODULE_DIR'}\n";
1207     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1208     $install .= "\t\$(NOECHO) \$(ECHO) If installing on a Win32 platform, be sure to use:\n";
1209     $install .= "\t\$(NOECHO) \$(ECHO) 'dmake -x MAXLINELENGTH=300000'\n";
1210     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1211     $install .= "\t\$(NOECHO) \$(ECHO) For other post-installation tasks, please consult the README.\n";
1212     $install .= "\t\$(NOECHO) \$(ECHO)\n";
1213
1214     return $install;
1215 }
1216
1217 sub postamble {
1218     # put directory mappings into Makefile
1219     # so that Make will export as environment
1220     # variables -- this is for the use of
1221     # rewrite-confg.PL
1222
1223     # quote '$' in the two password parameters
1224     my %config = %config;
1225     $config{'DB_PASS'} =~ s/\$/\$\$/g;
1226     $config{'ZEBRA_PASS'} =~ s/\$/\$\$/g;
1227
1228         # Hereagain, we must alter syntax per platform...
1229         if ( $^O =~ /linux|cygwin/ ) {
1230                 my $env = join("\n", map { "export __${_}__ := $target_directories->{$_}" } keys %$target_directories); 
1231                 $env .= "\n\n";
1232                 $env .= join("\n", map { "export __${_}__ := $config{$_}" } keys %config);
1233                 return "$env\n";
1234         }
1235         elsif ( $^O eq 'MSWin32' ) {
1236                 # NOTE: it is imperative that there be no whitespaces in ENV=value...
1237                 my $env = join("\n", map { "__${_}__=$target_directories->{$_}" } keys %$target_directories); 
1238                 $env .= "\n\n";
1239                 $env .= join("\n", map { "__${_}__=$config{$_}" } keys %config);
1240                 return "$env\n";
1241         }
1242 }
1243
1244 __END__
1245
1246
1247 =head1 SEE ALSO
1248
1249 ExtUtils::MakeMaker(3)
1250
1251 =head1 AUTHORS
1252
1253 MJ Ray mjr at phonecoop.coop
1254 Galen Charlton galen.charlton at liblime.com
1255
1256 =cut
1257 FIXME: deal with .htaccess