now checks for HTML::Template
[koha.git] / Install.pm
1 package Install; #assumes Install.pm
2
3 use strict;
4 require Exporter;
5
6 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
7
8 # set the version for version checking
9 $VERSION = 0.01;
10
11 @ISA = qw(Exporter);
12 @EXPORT = qw(   &checkperlmodules
13                 &getmessage
14                 &showmessage
15                 &releasecandidatewarning
16                 &getinstallationdirectories
17                 &getdatabaseinfo
18                 &getapacheinfo
19                 &getapachevhostinfo
20                 &updateapacheconf
21                 &basicauthentication
22                 &installfiles
23                 &databasesetup
24                 &restartapache
25                 &loadconfigfile
26                 );
27
28
29 my $messages;
30 $messages->{'continuing'}->{en}="Great!  Continuing setup.\n\n";
31 $messages->{'WelcomeToKohaInstaller'}->{en}=qq|
32 =================================
33 = Welcome to the Koha Installer =
34 =================================
35
36 Welcome to the Koha install script!  This script will prompt you for some
37 basic information about your desired setup, then install Koha according to
38 your specifications.  To accept the default value for any question, simply hit
39 Enter at the prompt.
40
41 Please be sure to read the documentation, or visit the Koha website at 
42 http://www.koha.org for more information.
43
44 Are you ready to begin the installation? (Y/[N]): |;
45 $messages->{'ReleaseCandidateWarning'}->{en}=qq|
46 =====================
47 = RELEASE CANDIDATE =
48 =====================
49
50 WARNING WARNING WARNING WARNING WARNING
51
52 You are about to install Koha version %s.  This version of Koha is a
53 release candidate.  It is not intended to be installed on production systems.
54 It is being released so that users can test it before we release a final
55 version.
56
57 Are you sure you want to install Koha %s? (Y/[N]): |;
58 $messages->{'WatchForReleaseAnnouncements'}->{en}=qq|
59
60 Watch for announcements of Koha releases on the Koha mailing list or the Koha
61 web site (http://www.koha.org/).
62
63 |;
64
65 $messages->{'NETZ3950Missing'}->{en}=qq|
66
67 The Net::Z3950 module is missing.  This module is necessary if you want to use
68 Koha's Z39.50 client to download bibliographic records from other libraries.
69 To install this module, you will need the yaz client installed from
70 http://www.indexdata.dk/yaz/ and then you can install the perl module with the
71 command:
72
73 perl -MCPAN -e 'install Net::Z3950'
74
75 Press the <ENTER> key to continue: |;
76
77 $messages->{'CheckingPerlModules'}->{en}=qq|
78
79 ==================
80 = PERL & MODULES =
81 ==================
82
83 Checking perl modules ...
84 |;
85
86 $messages->{'PerlVersionFailure'}->{en}="Sorry, you need at least Perl %s\n";
87
88 $messages->{'MissingPerlModules'}->{en}=qq|
89
90 ========================
91 = MISSING PERL MODULES =
92 ========================
93
94 You are missing some Perl modules which are required by Koha.
95 Once these modules have been installed, rerun this installer.
96 They can be installed by running (as root) the following:
97
98 %s
99 |;
100 $messages->{'AllPerlModulesInstalled'}->{en}=qq|
101
102 ==============================
103 = ALL PERL MODULES INSTALLED =
104 ==============================
105
106 All mandatory perl modules are installed.
107
108 Press <ENTER> to continue: |;
109 $messages->{'KohaVersionInstalled'}->{en}="You currently have Koha %s on your system.";
110 $messages->{'KohaUnknownVersionInstalled'}->{en}="I am not able to determine what version of Koha is installed now.";
111 $messages->{'KohaAlreadyInstalled'}->{en}=qq|
112 ==========================
113 = Koha already installed =
114 ==========================
115
116 It looks like Koha is already installed on your system (/etc/koha.conf exists
117 already).  If you would like to upgrade your system to %s, please use
118 the koha.upgrade script in this directory.
119
120 %s
121
122 |;
123 $messages->{'GetOpacDir'}->{en}=qq|
124 ==================
125 = OPAC DIRECTORY =
126 ==================
127
128 Please supply the directory you want Koha to store its OPAC files in.  This
129 directory will be auto-created for you if it doesn't exist.
130
131 OPAC Directory [%s]: |;
132
133 $messages->{'GetIntranetDir'}->{en}=qq|
134 =================================
135 = INTRANET/LIBRARIANS DIRECTORY =
136 =================================
137
138 Please supply the directory you want Koha to store its Intranet/Librarians
139 files in.  This directory will be auto-created for you if it doesn't exist.
140
141 Intranet Directory [%s]: |;
142
143 $messages->{'GetKohaLogDir'}->{en}=qq|
144 ======================
145 = KOHA LOG DIRECTORY =
146 ======================
147
148 Specify a log directory where any Koha daemons can create log files.
149
150 Koha Log Directory [%s]: |;
151
152 $messages->{'AuthenticationWarning'}->{en}=qq|
153 ==================
154 = Authentication =
155 ==================
156
157 This release of Koha has a new authentication module.  If you are not already
158 using basic authentication on your intranet, you will be required to log in to
159 access some of the features of the intranet.  You can log in using the userid
160 and password from the /etc/koha.conf configuration file at any time.  Use the
161 "Members" module to add passwords for other accounts and set their permissions.
162
163 [NOTE PERMISSIONS ARE NOT COMPLETED AS OF THIS RELEASE.  Do not give passwords
164 to any patrons unless you want them to have full access to your intranet.]
165
166 Press the <ENTER> key to continue: |;
167
168 $messages->{'Completed'}->{en}=qq|
169 ==============================
170 = KOHA INSTALLATION COMPLETE =
171 ==============================
172
173 Congratulations ... your Koha installation is complete!
174
175 You will be able to connect to your Librarian interface at:
176
177    http://%s\:%s/
178
179 and the OPAC interface at :
180
181    http://%s\:%s/
182
183
184 Be sure to read the INSTALL, and Hints files. 
185
186 For more information visit http://www.koha.org
187
188 Press <ENTER> to exit the installer: |;
189
190 sub releasecandidatewarning {
191     my $message=getmessage('ReleaseCandidateWarning', [$::kohaversion, $::kohaversion]);
192     my $answer=showmessage($message, 'yn', 'n');
193
194     if ($answer =~ /y/i) {
195         print getmessage('continuing');
196     } else {
197         my $message=getmessage('WatchForReleaseAnnouncements');
198         print $message;
199         exit;
200     };
201 }
202
203
204 #
205 # Test for Perl and Modules
206 #
207 #
208 sub checkperlmodules {
209     my $message = getmessage('CheckingPerlModules');
210     showmessage($message, 'none');
211     
212     unless (eval "require 5.6.0") {
213         die getmessage('PerlVersionFailure', ['5.6.0']);
214     }
215
216     my @missing = ();
217     unless (eval {require DBI})               { push @missing,"DBI" };
218     unless (eval {require Date::Manip})       { push @missing,"Date::Manip" };
219     unless (eval {require DBD::mysql})        { push @missing,"DBD::mysql" };
220     unless (eval {require Template})          { push @missing,"Template" };
221     unless (eval {require Set::Scalar})       { push @missing,"Set::Scalar" };
222     unless (eval {require Digest::MD5})       { push @missing,"Digest::MD5" };
223     unless (eval {require Net::Z3950})        { 
224         my $message = getmessage('NETZ3950Missing');
225         showmessage($message, 'PressEnter', '', 1);
226         if ($#missing>=0) {
227             push @missing, "Net::Z3950";
228         }
229     }
230
231 #
232 # Print out a list of any missing modules
233 #
234
235     if (@missing > 0) {
236         my $missing='';
237         foreach my $module (@missing) {
238             $missing.="   perl -MCPAN -e 'install \"$module\"'\n";
239         }
240         my $message=getmessage('MissingPerlModules', [$missing]);
241         showmessage($message, 'none');
242         exit;
243     } else {
244         showmessage(getmessage('AllPerlModulesInstalled'), 'PressEnter', '', 1);
245     }
246
247
248 }
249
250
251 sub getmessage {
252     my $messagename=shift;
253     my $variables=shift;
254     my $message=$messages->{$messagename}->{$::language} || $messages->{$messagename}->{en} || "Error: No message named $messagename in Install.pm\n";
255     if (defined($variables)) {
256         $message=sprintf $message, @$variables;
257     }
258     return $message;
259 }
260
261
262 sub showmessage {
263     my $message=shift;
264     my $responsetype=shift;
265     my $defaultresponse=shift;
266     my $noclear=shift;
267     ($noclear) || (system('clear'));
268     if ($responsetype =~ /^yn$/) {
269         $responsetype='restrictchar yn';
270     }
271     print $message;
272     SWITCH: {
273         if ($responsetype =~/^restrictchar (.*)/i) {
274             my $response='\0';
275             my $options=$1;
276             until ($options=~/$response/) {
277                 ($defaultresponse) || ($defaultresponse=substr($options,0,1));
278                 $response=<STDIN>;
279                 chomp $response;
280                 (length($response)) || ($response=$defaultresponse);
281                 unless ($options=~/$response/) {
282                     ($noclear) || (system('clear'));
283                     print "Invalid Response.  Choose from [$options].\n\n";
284                     print $message;
285                 }
286             }
287             return $response;
288         }
289         if ($responsetype =~/^free$/i) {
290             (defined($defaultresponse)) || ($defaultresponse='');
291             my $response=<STDIN>;
292             chomp $response;
293             ($response) || ($response=$defaultresponse);
294             return $response;
295         }
296         if ($responsetype =~/^numerical$/i) {
297             (defined($defaultresponse)) || ($defaultresponse='');
298             my $response='';
299             until ($response=~/^\d+$/) {
300                 $response=<STDIN>;
301                 chomp $response;
302                 ($response) || ($response=$defaultresponse);
303                 unless ($response=~/^\d+$/) {
304                     ($noclear) || (system('clear'));
305                     print "Invalid Response ($response).  Response must be a number.\n\n";
306                     print $message;
307                 }
308             }
309             return $response;
310         }
311         if ($responsetype =~/^email$/i) {
312             (defined($defaultresponse)) || ($defaultresponse='');
313             my $response='';
314             until ($response=~/.*\@.*\..*/) {
315                 $response=<STDIN>;
316                 chomp $response;
317                 ($response) || ($response=$defaultresponse);
318                 unless ($response=~/.*\@.*\..*/) {
319                     ($noclear) || (system('clear'));
320                     print "Invalid Response ($response).  Response must be a valid email address.\n\n";
321                     print $message;
322                 }
323             }
324             return $response;
325         }
326         if ($responsetype =~/^PressEnter$/i) {
327             <STDIN>;
328             return;
329         }
330         if ($responsetype =~/^none$/i) {
331             return;
332         }
333     }
334 }
335
336 sub getinstallationdirectories {
337     $::opacdir = '/usr/local/koha/opac';
338     $::intranetdir = '/usr/local/koha/intranet';
339     my $getdirinfo=1;
340     while ($getdirinfo) {
341         # Loop until opac directory and koha directory are different
342         my $message=getmessage('GetOpacDir', [$::opacdir]);
343         $::opacdir=showmessage($message, 'free', $::opacdir);
344
345         $message=getmessage('GetIntranetDir', [$::intranetdir]);
346         $::intranetdir=showmessage($message, 'free', $::intranetdir);
347
348         if ($::intranetdir eq $::opacdir) {
349             print qq|
350
351 You must specify different directories for the OPAC and INTRANET files!
352  :: $::intranetdir :: $::opacdir ::
353 |;
354 <STDIN>
355         } else {
356             $getdirinfo=0;
357         }
358     }
359     $::kohalogdir='/var/log/koha';
360     my $message=getmessage('GetKohaLogDir', [$::kohalogdir]);
361     $::kohalogdir=showmessage($message, 'free', $::kohalogdir);
362
363
364     unless ( -d $::intranetdir ) {
365        my $result=mkdir ($::intranetdir, oct(770));
366        if ($result==0) {
367            my @dirs = split(m#/#, $::intranetdir);
368             my $checkdir='';
369             foreach (@dirs) {
370                 $checkdir.="$_/";
371                 unless (-e "$checkdir") {
372                     mkdir($checkdir, 0775);
373                 }
374             }
375        }
376        chown (oct(0), (getgrnam($::httpduser))[2], "$::intranetdir");
377        chmod (oct(770), "$::intranetdir");
378     }
379     unless ( -d "$::intranetdir/htdocs" ) {
380        mkdir ("$::intranetdir/htdocs", oct(750));
381     }
382     unless ( -d "$::intranetdir/cgi-bin" ) {
383        mkdir ("$::intranetdir/cgi-bin", oct(750));
384     }
385     unless ( -d "$::intranetdir/modules" ) {
386        mkdir ("$::intranetdir/modules", oct(750));
387     }
388     unless ( -d "$::intranetdir/scripts" ) {
389        mkdir ("$::intranetdir/scripts", oct(750));
390     }
391     unless ( -d $::opacdir ) {
392        my $result=mkdir ($::opacdir, oct(770));
393        if ($result==0) {
394            my @dirs = split(m#/#, $::opacdir);
395             my $checkdir='';
396             foreach (@dirs) {
397                 $checkdir.="$_/";
398                 unless (-e "$checkdir") {
399                     mkdir($checkdir, 0775);
400                 }
401             }
402        }
403        chown (oct(0), (getgrnam($::httpduser))[2], "$::opacdir");
404        chmod (oct(770), "$::opacdir");
405     }
406     unless ( -d "$::opacdir/htdocs" ) {
407        mkdir ("$::opacdir/htdocs", oct(750));
408     }
409     unless ( -d "$::opacdir/cgi-bin" ) {
410        mkdir ("$::opacdir/cgi-bin", oct(750));
411     }
412
413
414     unless ( -d $::kohalogdir ) {
415        my $result=mkdir ($::kohalogdir, oct(770));
416        if ($result==0) {
417            my @dirs = split(m#/#, $::kohalogdir);
418             my $checkdir='';
419             foreach (@dirs) {
420                 $checkdir.="$_/";
421                 unless (-e "$checkdir") {
422                     mkdir($checkdir, 0775);
423                 }
424             }
425        }
426
427        chown (oct(0), (getgrnam($::httpduser))[2,3], "$::kohalogdir");
428        chmod (oct(770), "$::kohalogdir");
429     }
430 }
431
432
433
434 $messages->{'DatabaseName'}->{en}=qq|
435 ==========================
436 = Name of MySQL database =
437 ==========================
438
439 Please provide the name of the mysql database for your koha installation.
440
441 Database name [%s]: |;
442
443 $messages->{'DatabaseHost'}->{en}=qq|
444 =================
445 = Database Host =
446 =================
447
448 Please provide the hostname for mysql.  Unless the database is located on
449 another machine this will be "localhost".
450
451 Database host [%s]: |;
452
453 $messages->{'DatabaseUser'}->{en}=qq|
454 =================
455 = Database User =
456 =================
457
458 Please provide the name of the user, who will have full administrative rights
459 to the %s database, when authenticating from %s.
460
461 Database user [%s]: |;
462
463 $messages->{'DatabasePassword'}->{en}=qq|
464 =====================
465 = Database Password =
466 =====================
467
468 Please provide a good password for the user %s.
469
470 Database Password: |;
471
472 $messages->{'BlankPassword'}->{en}=qq|
473 ==================
474 = BLANK PASSWORD =
475 ==================
476
477 You must not use a blank password for your MySQL user!
478
479 Press <ENTER> to try again: 
480 |;
481
482 sub getdatabaseinfo {
483
484     $::dbname = 'Koha';
485     $::hostname = 'localhost';
486     $::user = 'kohaadmin';
487     $::pass = '';
488
489 #Get the database name
490
491     my $message=getmessage('DatabaseName', [$::dbname]);
492     $::dbname=showmessage($message, 'free', $::dbname);
493
494 #Get the hostname for the database
495     
496     $message=getmessage('DatabaseHost', [$::hostname]);
497     $::hostname=showmessage($message, 'free', $::hostname);
498
499 #Get the username for the database
500
501     $message=getmessage('DatabaseUser', [$::dbname, $::hostname, $::user]);
502     $::user=showmessage($message, 'free', $::user);
503
504 #Get the password for the database user
505
506     while ($::pass eq '') {
507         my $message=getmessage('DatabasePassword', [$::user]);
508         $::pass=showmessage($message, 'free', $::pass);
509         if ($::pass eq '') {
510             my $message=getmessage('BlankPassword');
511             showmessage($message,'PressEnter');
512         }
513     }
514 }
515
516
517
518 $messages->{'FoundMultipleApacheConfFiles'}->{en}=qq|
519 ================================
520 = MULTIPLE APACHE CONFIG FILES =
521 ================================
522
523 I found more than one possible Apache configuration file:
524
525 %s
526
527 Choose the correct file [1]: |;
528
529 $messages->{'NoApacheConfFiles'}->{en}=qq|
530 ===============================
531 = NO APACHE CONFIG FILE FOUND =
532 ===============================
533
534 I was not able to find your Apache configuration file.
535
536 The file is usually called httpd.conf or apache.conf.
537
538 Please specify the location of your config file: |;
539
540 $messages->{'NotAFile'}->{en}=qq|
541 =======================
542 = FILE DOES NOT EXIST =
543 =======================
544
545 The file %s does not exist.
546
547 Please press <ENTER> to continue: |;
548
549 $messages->{'EnterApacheUser'}->{en}=qq|
550 ====================
551 = NEED APACHE USER =
552 ====================
553
554 I was not able to determine the user that Apache is running as.  This
555 information is necessary in order to set the access privileges correctly on
556 /etc/koha.conf.  This user should be set in one of the Apache configuration
557 files using the "User" directive.
558
559 Enter the Apache userid: |;
560
561 $messages->{'InvalidUserid'}->{en}=qq|
562 ==================
563 = INVALID USERID =
564 ==================
565
566 The userid %s is not a valid userid on this system.
567
568 Press <ENTER> to continue: |;
569
570 sub getapacheinfo {
571     my @confpossibilities;
572
573     foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
574                           /usr/local/etc/apache/httpd.conf
575                           /usr/local/etc/apache/apache.conf
576                           /var/www/conf/httpd.conf
577                           /etc/apache/conf/httpd.conf
578                           /etc/apache/conf/apache.conf
579                           /etc/apache-ssl/conf/apache.conf
580                           /etc/httpd/conf/httpd.conf
581                           /etc/httpd/httpd.conf)) {
582         if ( -f $httpdconf ) {
583             push @confpossibilities, $httpdconf;
584         }
585     }
586
587     if ($#confpossibilities==-1) {
588         my $message=getmessage('NoApacheConfFiles');
589         my $choice='';
590         until (-f $choice) {
591             $choice=showmessage($message, "free", 1);
592             unless (-f $choice) {
593                 showmessage(getmessage('NotAFile', [$choice]),'PressEnter', '', 1);
594             }
595         }
596     } elsif ($#confpossibilities>0) {
597         my $conffiles='';
598         my $counter=1;
599         my $options='';
600         foreach (@confpossibilities) {
601             $conffiles.="   $counter: $_\n";
602             $options.="$counter";
603             $counter++;
604         }
605         my $message=getmessage('FoundMultipleApacheConfFiles', [$conffiles]);
606         my $choice=showmessage($message, "restrictchar $options", 1);
607         $::realhttpdconf=$confpossibilities[$choice-1];
608     } else {
609         $::realhttpdconf=$confpossibilities[0];
610     }
611     open (HTTPDCONF, $::realhttpdconf) or warn "Insufficient privileges to open $::realhttpdconf for reading.\n";
612     while (<HTTPDCONF>) {
613         if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
614             $::httpduser = $1;
615         }
616     }
617     close(HTTPDCONF);
618
619
620
621
622     unless ($::httpduser) {
623         my $message=getmessage('EnterApacheUser');
624         until (length($::httpduser) && getpwnam($::httpduser)) {
625             $::httpduser=showmessage($message, "free", '');
626             if (length($::httpduser)>0) {
627                 unless (getpwnam($::httpduser)) {
628                     my $message=getmessage('InvalidUserid', [$::httpduser]);
629                     showmessage($message,'PressEnter');
630                 }
631             } else {
632             }
633         }
634         print "AU: $::httpduser\n";
635     }
636 }
637
638
639 $messages->{'ApacheConfigIntroduction'}->{en}=qq|
640 ========================
641 = APACHE CONFIGURATION =
642 ========================
643
644 Koha needs to setup your Apache configuration file for the
645 OPAC and LIBRARIAN virtual hosts.  By default this installer
646 will do this by using one ip address and two different ports
647 for the virtual hosts.  There are other ways to set this up,
648 and the installer will leave comments in httpd.conf detailing
649 what these other options are.
650
651
652 Press <ENTER> to continue: |;
653
654 $messages->{'GetVirtualHostEmail'}->{en}=qq|
655 =============================
656 = WEB SERVER E-MAIL CONTACT =
657 =============================
658
659 Enter the e-mail address to be used as a contact for the virtual hosts (this
660 address is displayed if any errors are encountered).
661
662 E-mail contact [%s]: |;
663
664 $messages->{'GetServerName'}->{en}=qq|
665 ======================================
666 = WEB SERVER HOST NAME OR IP ADDRESS =
667 ======================================
668
669 Please enter the domain name or ip address of your computer.
670
671 Host name or IP Address [%s]: |;
672
673 $messages->{'GetOpacPort'}->{en}=qq|
674 ==========================
675 = OPAC VIRTUAL HOST PORT =
676 ==========================
677
678 Please enter the port for your OPAC interface.  This defaults to port 80, but
679 if you are already serving web content from this server, you should change it
680 to a different port (8000 might be a good choice).
681
682 Enter the OPAC Port [%s]: |;
683
684 $messages->{'GetIntranetPort'}->{en}=qq|
685 ==============================
686 = INTRANET VIRTUAL HOST PORT =
687 ==============================
688
689 Please enter the port for your Intranet interface.  This must be different from
690 the OPAC port (%s).
691
692 Enter the Intranet Port [%s]: |;
693
694
695 sub getapachevhostinfo {
696
697     $::svr_admin = "webmaster\@$::domainname";
698     $::servername=`hostname -f`;
699     chomp $::servername;
700     $::opacport=80;
701     $::intranetport=8080;
702
703     showmessage(getmessage('ApacheConfigIntroduction'), 'PressEnter');
704
705     $::svr_admin=showmessage(getmessage('GetVirtualHostEmail', [$::svr_admin]), 'email', $::svr_admin);
706     $::servername=showmessage(getmessage('GetServerName', [$::servername]), 'free', $::servername);
707
708
709     $::opacport=showmessage(getmessage('GetOpacPort', [$::opacport]), 'numerical', $::opacport);
710     $::intranetport=showmessage(getmessage('GetIntranetPort', [$::opacport, $::intranetport]), 'numerical', $::intranetport);
711
712 }
713
714 $messages->{'StartUpdateApache'}->{en}=qq|
715 =================================
716 = UPDATING APACHE CONFIGURATION =
717 =================================
718
719 Checking for modules that need to be loaded...
720 |;
721
722 $messages->{'LoadingApacheModuleModEnv'}->{en}="Loading SetEnv Apache module.\n";
723
724 $messages->{'LoadingApacheModuleModInc'}->{en}="Loading Includes Apache module.\n";
725
726 $messages->{'ApacheConfigBackupFailed'}->{en}=qq|
727 ======================================
728 = APACHE CONFIGURATION BACKUP FAILED =
729 ======================================
730
731 An error occurred while trying to make a backup copy of %s.
732
733   %s
734
735 No changes will be made to the apache configuration file at this time.
736
737 Press <ENTER> to continue: |;
738
739
740 $messages->{'ApacheAlreadyConfigured'}->{en}=qq|
741 =============================
742 = APACHE ALREADY CONFIGURED =
743 =============================
744
745 %s appears to already have an entry for Koha
746 Virtual Hosts.  You may need to edit %s
747 f anything has changed since it was last set up.  This
748 script will not attempt to modify an existing Koha apache
749 configuration.
750
751 Press <ENTER> to continue: |;
752
753 sub updateapacheconf {
754     my $logfiledir=`grep ^ErrorLog $::realhttpdconf`;
755     chomp $logfiledir;
756
757     if ($logfiledir) {
758         $logfiledir=~m#ErrorLog (.*)/[^/]*$#;
759         $logfiledir=$1;
760     }
761
762     unless ($logfiledir) {
763         $logfiledir='logs';
764     }
765
766     showmessage(getmessage('StartUpdateApache'), 'none');
767
768     my $httpdconf;
769     my $envmodule=0;
770     my $includesmodule=0;
771     open HC, $::realhttpdconf;
772     while (<HC>) {
773         if (/^\s*#\s*LoadModule env_module /) {
774             s/^\s*#\s*//;
775             showmessage(getmessage('LoadingApacheModuleModEnv'));
776             $envmodule=1;
777         }
778         if (/^\s*#\s*LoadModule includes_module /) {
779             s/^\s*#\s*//;
780             showmessage(getmessage('LoadingApacheModuleModInc'));
781         }
782         if (/\s*LoadModule includes_module / ) {
783             $includesmodule=1;
784         }
785         $httpdconf.=$_;
786     }
787
788     my $backupfailed=0;
789     $backupfailed=`cp -f $::realhttpdconf $::realhttpdconf\.prekoha`;
790     if ($backupfailed) {
791         showmessage(getmessage('ApacheConfigBackupFailed', [$::realhttpdconf,$backupfailed ]), 'PressEnter');
792         return;
793     }
794
795     if ($envmodule || $includesmodule) {
796         open HC, ">$::realhttpdconf";
797         print HC $httpdconf;
798         close HC;
799     }
800
801
802     
803     if (`grep 'VirtualHost $::servername' $::realhttpdconf`) {
804         showmessage(getmessage('ApacheAlreadyConfigured', [$::realhttpdconf, $::realhttpdconf]), 'PressEnter');
805         return;
806     } else {
807         my $includesdirectives='';
808         if ($includesmodule) {
809             $includesdirectives.="Options +Includes\n";
810             $includesdirectives.="   AddHandler server-parsed .html\n";
811         }
812         open(SITE,">>$::realhttpdconf") or warn "Insufficient priveleges to open $::realhttpdconf for writing.\n";
813         my $opaclisten = '';
814         if ($::opacport != 80) {
815             $opaclisten="Listen $::opacport";
816         }
817         my $intranetlisten = '';
818         if ($::intranetport != 80) {
819             $intranetlisten="Listen $::intranetport";
820         }
821         print SITE <<EOP
822
823 # Ports to listen to for Koha
824 $opaclisten
825 $intranetlisten
826
827 # NameVirtualHost is used by one of the optional configurations detailed below
828
829 #NameVirtualHost 11.22.33.44
830
831 # KOHA's OPAC Configuration
832 <VirtualHost $::servername\:$::opacport>
833    ServerAdmin $::svr_admin
834    DocumentRoot $::opacdir/htdocs
835    ServerName $::servername
836    ScriptAlias /cgi-bin/koha/ $::opacdir/cgi-bin/
837    ErrorLog $logfiledir/opac-error_log
838    TransferLog $logfiledir/opac-access_log
839    SetEnv PERL5LIB "$::intranetdir/modules"
840    $includesdirectives
841 </VirtualHost>
842
843 # KOHA's INTRANET Configuration
844 <VirtualHost $::servername\:$::intranetport>
845    ServerAdmin $::svr_admin
846    DocumentRoot $::intranetdir/htdocs
847    ServerName $::servername
848    ScriptAlias /cgi-bin/koha/ "$::intranetdir/cgi-bin/"
849    ErrorLog $logfiledir/koha-error_log
850    TransferLog $logfiledir/koha-access_log
851    SetEnv PERL5LIB "$::intranetdir/modules"
852    $includesdirectives
853 </VirtualHost>
854
855 # If you want to use name based Virtual Hosting:
856 #   1. remove the two Listen lines
857 #   2. replace $::servername\:$::opacport wih your.opac.domain.name
858 #   3. replace ServerName $::servername wih ServerName your.opac.domain.name
859 #   4. replace $::servername\:$::intranetport wih your intranet domain name
860 #   5. replace ServerName $::servername wih ServerName your.intranet.domain.name
861 #
862 # If you want to use NameVirtualHost'ing (using two names on one ip address):
863 #   1.  Follow steps 1-5 above
864 #   2.  Uncomment the NameVirtualHost line and set the correct ip address
865
866 EOP
867
868
869     }
870 }
871
872 $messages->{'IntranetAuthenticationQuestion'}->{en}=qq|
873 ===========================
874 = INTRANET AUTHENTICATION =
875 ===========================
876
877 I can set it up so that the Intranet/Librarian site is password protected using
878 Apache's Basic Authorization.
879
880 Would you like to do this ([Y]/N): |;
881
882 $messages->{'BasicAuthUsername'}->{en}="Please enter a userid for intranet access [%s]: ";
883 $messages->{'BasicAuthPassword'}->{en}="Please enter a password for %s: ";
884 $messages->{'BasicAuthPasswordWasBlank'}->{en}="\nYou cannot use a blank password!\n\n";
885
886 sub basicauthentication {
887     my $message=getmessage('IntranetAuthenticationQuestion');
888     my $answer=showmessage($message, 'yn', 'y');
889
890     my $apacheauthusername='librarian';
891     my $apacheauthpassword='';
892     if ($answer=~/^y/i) {
893         ($apacheauthusername) = showmessage(getmessage('BasicAuthUsername', [ $apacheauthusername]), 'free', $apacheauthusername, 1);
894         $apacheauthusername=~s/[^a-zA-Z0-9]//g;
895         while (! $apacheauthpassword) {
896             ($apacheauthpassword) = showmessage(getmessage('BasicAuthPassword', [ $apacheauthusername]), 'free', 1);
897             if (!$apacheauthpassword) {
898                 ($apacheauthpassword) = showmessage(getmessage('BasicAuthPasswordWasBlank'), 'none', '', 1);
899             }
900         }
901         open AUTH, ">/etc/kohaintranet.pass";
902         my $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
903         my $salt=substr($chars, int(rand(length($chars))),1);
904         $salt.=substr($chars, int(rand(length($chars))),1);
905         print AUTH $apacheauthusername.":".crypt($apacheauthpassword, $salt)."\n";
906         close AUTH;
907         open(SITE,">>$::realhttpdconf") or warn "Insufficient priveleges to open $::realhttpdconf for writing.\n";
908         print SITE <<EOP
909
910 <Directory $::intranetdir>
911     AuthUserFile /etc/kohaintranet.pass
912     AuthType Basic
913     AuthName "Koha Intranet (for librarians only)"
914     Require  valid-user
915 </Directory>
916 EOP
917     }
918     close(SITE);
919 }
920
921 $messages->{'InstallFiles'}->{en}=qq|
922 ====================
923 = INSTALLING FILES =
924 ====================
925
926 Copying files to installation directories:
927
928 |;
929
930
931 $messages->{'CopyingFiles'}->{en}="Copying %s to %s.\n";
932
933
934
935 sub installfiles {
936
937
938     showmessage(getmessage('InstallFiles'),'none');
939     print getmessage('CopyingFiles', ['internet-html', "$::intranetdir/htdocs" ]);
940     system("cp -R intranet-html/* $::intranetdir/htdocs/");
941     print getmessage('CopyingFiles', ['internet-cgi', "$::intranetdir/cgi-bin" ]);
942     system("cp -R intranet-cgi/* $::intranetdir/cgi-bin/");
943     print getmessage('CopyingFiles', ['stand-alone scripts', "$::intranetdir/scripts" ]);
944     system("cp -R scripts/* $::intranetdir/scripts/");
945     print getmessage('CopyingFiles', ['perl modules', "$::intranetdir/modules" ]);
946     system("cp -R modules/* $::intranetdir/modules/");
947     print getmessage('CopyingFiles', ['opac-html', "$::opacdir/htdocs" ]);
948     system("cp -R opac-html/* $::opacdir/htdocs/");
949     print getmessage('CopyingFiles', ['opac-cgi', "$::opacdir/cgi-bin" ]);
950     system("cp -R opac-cgi/* $::opacdir/cgi-bin/");
951     system("touch $::opacdir/cgi-bin/opac");
952
953     system("chown -R root.$::httpduser $::opacdir");
954     system("chown -R root.$::httpduser $::intranetdir");
955
956     # Create /etc/koha.conf
957
958     open(KC,">$::etcdir/koha.conf") or warn "Couldn't create file
959     at $::etcdir.  Must have write capability.\n";
960     print KC qq|
961 database=$::dbname
962 hostname=$::hostname
963 user=$::user
964 pass=$::pass
965 includes=$::intranetdir/htdocs/includes
966 intranetdir=$::intranetdir
967 opacdir=$::opacdir
968 kohalogdir=$::kohalogdir
969 kohaversion=$::kohaversion
970 httpduser=$::httpduser
971 |;
972     close(KC);
973
974     chown((getpwnam($::httpduser)) [2,3], "$::etcdir/koha.conf") or warn "can't chown koha.conf: $!";
975     chmod 0440, "$::etcdir/koha.conf";
976
977     chmod 0750, "$::intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
978     chmod 0750, "$::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
979     chmod 0750, "$::intranetdir/scripts/z3950daemon/processz3950queue";
980     chown(0, (getpwnam($::httpduser)) [3], "$::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
981     chown(0, (getpwnam($::httpduser)) [3], "$::intranetdir/scripts/z3950daemon/processz3950queue") or warn "can't chown $::intranetdir/scripts/z3950daemon/processz3950queue: $!";
982
983 }
984
985 $messages->{'MysqlRootPassword'}->{en}=qq|
986 ============================
987 = MYSQL ROOT USER PASSWORD =
988 ============================
989
990 To allow us to create the koha database please supply your
991 mysql server's root user password:
992
993 Enter MySql root user password: |;
994
995 $messages->{'InvalidMysqlRootPassword'}->{en}="Invalid Password.  Please try again.";
996
997 $messages->{'CreatingDatabase'}->{en}=qq|
998 =====================
999 = CREATING DATABASE =
1000 =====================
1001
1002 Creating the MySql database for Koha...
1003
1004 |;
1005
1006 $messages->{'CreatingDatabaseError'}->{en}=qq|
1007 ===========================
1008 = ERROR CREATING DATABASE =
1009 ===========================
1010
1011 Couldn't connect to the MySQL server for the reason given above.
1012 This is a serious problem, the database will not get installed.\a
1013
1014 Press <ENTER> to continue: |;
1015
1016 $messages->{'SampleData'}->{en}=qq|
1017 ===============
1018 = SAMPLE DATA =
1019 ===============
1020
1021 If you are installing Koha for evaluation purposes,  I have a batch of sample
1022 data that you can install now.
1023
1024 If you are installing Koha with the intention of populating it with your own
1025 data, you probably don't want this sample data installed.
1026
1027 Would you like to install the sample data? Y/[N]: |;
1028
1029 $messages->{'SampleDataInstalled'}->{en}=qq|
1030 =========================
1031 = SAMPLE DATA INSTALLED =
1032 =========================
1033
1034 Sample data has been installed.  For some suggestions on testing Koha, please
1035 read the file doc/HOWTO-Testing.  If you find any bugs, please submit them at
1036 http://bugs.koha.org/.  If you need help with testing Koha, you can post a
1037 question through the koha-devel mailing list, or you can check for a developer
1038 online at +irc.katipo.co.nz:6667 channel #koha.
1039
1040 You can find instructions for subscribing to the Koha mailing lists at:
1041
1042     http://www.koha.org
1043
1044
1045 Press <ENTER> to continue: |;
1046
1047 $messages->{'AddBranchPrinter'}->{en}=qq|
1048 ==========================
1049 = Add Branch and Printer =
1050 ==========================
1051
1052 Would you like to install an initial branch and printer? [Y]/N: |;
1053
1054 $messages->{'BranchName'}->{en}="Branch Name [%s]: ";
1055 $messages->{'BranchCode'}->{en}="Branch Code (4 letters or numbers) [%s]: ";
1056 $messages->{'PrinterQueue'}->{en}="Printer Queue [%s]: ";
1057 $messages->{'PrinterName'}->{en}="Printer Name [%s]: ";
1058
1059 sub databasesetup {
1060     my $mysqldir;
1061     $::mysqluser = 'root';
1062     $::mysqlpass = '';
1063
1064     foreach my $mysql (qw(/usr/local/mysql
1065                           /opt/mysql
1066                           )) {
1067        if ( -d $mysql ) {
1068                 $mysqldir=$mysql;
1069        }
1070     }
1071     if (!$mysqldir){
1072         $mysqldir='/usr';
1073     }
1074
1075
1076     my $needpassword=1;
1077     while ($needpassword) {
1078         $::mysqlpass=showmessage(getmessage('MysqlRootPassword'), 'free');
1079         my $result=system("$mysqldir/bin/mysqladmin -u$::mysqluser -p$::mysqlpass proc > /dev/null 2>&1");
1080         if ($result) {
1081             print getmessage('InvalidMysqlRootPassword');
1082         } else {
1083             $needpassword=0;
1084         }
1085     }
1086
1087     showmessage(getmessage('CreatingDatabase'),'none');
1088
1089     my $result=system("$mysqldir/bin/mysqladmin -u$::mysqluser -p$::mysqlpass create $::dbname");
1090     if ($result) {
1091         showmessage(getmessage('CreatingDatabaseError'),'PressEnter', '', 1);
1092     } else {
1093         system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname < koha.mysql");
1094         system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass mysql -e \"insert into user (Host,User,Password) values ('$::hostname','$::user',password('$::pass'))\"\;");
1095         system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass mysql -e \"insert into db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv, index_priv, alter_priv) values ('%','$::dbname','$::user','Y','Y','Y','Y','Y','Y','Y','Y')\"");
1096         system("$mysqldir/bin/mysqladmin -u$::mysqluser -p$::mysqlpass reload");
1097
1098         system ("perl -I $::intranetdir/modules scripts/updater/updatedatabase");
1099
1100
1101         my $response=showmessage(getmessage('SampleData'), 'yn', 'n');
1102         if ($response =~/^y/i) {
1103             system("gunzip sampledata-1.2.gz");
1104             system("cat sampledata-1.2 | $mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname");
1105             system("gzip -9 sampledata-1.2");
1106             system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname -e \"insert into branches (branchcode,branchname,issuing) values ('MAIN', 'Main Library', 1)\"");
1107             system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname -e \"insert into printers (printername,printqueue,printtype) values ('Circulation Desk Printer', 'lp', 'hp')\"");
1108             showmessage(getmessage('SampleDataInstalled'), 'PressEnter');
1109         } else {
1110             my $input;
1111             my $response=showmessage(getmessage('AddBranchPrinter'), 'yn', 'y');
1112
1113             unless ($response =~/^n/i) {
1114                 my $branch='Main Library';
1115                 print "Enter a name for the library branch [$branch]: ";
1116                 $branch=showmessage(getmessage('BranchName', [$branch]), 'free', $branch, 1);
1117                 $branch=~s/[^A-Za-z0-9\s]//g;
1118
1119                 my $branchcode=$branch;
1120                 $branchcode=~s/[^A-Za-z0-9]//g;
1121                 $branchcode=uc($branchcode);
1122                 $branchcode=substr($branchcode,0,4);
1123                 $branchcode=showmessage(getmessage('BranchCode', [$branchcode]), 'free', $branchcode, 1);
1124                 $branchcode=~s/[^A-Z]//g;
1125                 $branchcode=uc($branchcode);
1126                 $branchcode=substr($branchcode,0,4);
1127
1128                 system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname -e \"insert into branches (branchcode,branchname,issuing) values ('$branchcode', '$branch', 1)\"");
1129
1130                 my $printername='Library Printer';
1131                 $printername=showmessage(getmessage('PrinterName', [$printername]), 'free', $printername, 1);
1132                 $printername=~s/[^A-Za-z0-9\s]//g;
1133
1134                 my $printerqueue='lp';
1135                 $printerqueue=showmessage(getmessage('PrinterQueue', [$printerqueue]), 'free', $printerqueue, 1);
1136                 $printerqueue=~s/[^A-Za-z0-9]//g;
1137                 system("$mysqldir/bin/mysql -u$::mysqluser -p$::mysqlpass $::dbname -e \"insert into printers (printername,printqueue,printtype) values ('$printername', '$printerqueue', '')\"");
1138
1139             }
1140         }
1141
1142
1143     }
1144
1145 }
1146
1147 $messages->{'RestartApache'}->{en}=qq|
1148 ==================
1149 = RESTART APACHE =
1150 ==================
1151
1152 Apache needs to be restarted to load the new configuration for Koha.
1153
1154 Would you like to restart Apache now?  [Y]/N: |;
1155
1156 sub restartapache {
1157
1158     my $response=showmessage(getmessage('RestartApache'), 'yn', 'y');
1159
1160
1161
1162     unless ($response=~/^n/i) {
1163         # Need to support other init structures here?
1164         if (-e "/etc/rc.d/init.d/httpd") {
1165             system('/etc/rc.d/init.d/httpd restart');
1166         } elsif (-e "/etc/init.d/apache") {
1167             system('/etc//init.d/apache restart');
1168         } elsif (-e "/etc/init.d/apache-ssl") {
1169             system('/etc/init.d/apache-ssl restart');
1170         }
1171     }
1172
1173 }
1174
1175 sub loadconfigfile {
1176     my %configfile;
1177
1178     open (KC, "/etc/koha.conf");
1179     while (<KC>) {
1180      chomp;
1181      (next) if (/^\s*#/);
1182      if (/(.*)\s*=\s*(.*)/) {
1183        my $variable=$1;
1184        my $value=$2;
1185        # Clean up white space at beginning and end
1186        $variable=~s/^\s*//g;
1187        $variable=~s/\s*$//g;
1188        $value=~s/^\s*//g;
1189        $value=~s/\s*$//g;
1190        $configfile{$variable}=$value;
1191      }
1192     }
1193
1194     $::intranetdir=$configfile{'intranetdir'};
1195     $::opacdir=$configfile{'opacdir'};
1196     $::kohaversion=$configfile{'kohaversion'};
1197     $::kohalogdir=$configfile{'kohalogdir'};
1198     $::database=$configfile{'database'};
1199     $::hostname=$configfile{'hostname'};
1200     $::user=$configfile{'user'};
1201     $::pass=$configfile{'pass'};
1202 }
1203
1204 END { }       # module clean-up code here (global destructor)
1205