fugfixes from Dombes Abbey work
[koha.git] / misc / installer.pl
1 #!/usr/bin/perl -w # please develop with -w
2
3 # $Id$
4
5 #use diagnostics;
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict; # please develop with the strict pragma
25
26 if ($<) {
27     print "\n\nYou must run $0 as root.\n\n";
28     exit;
29 }
30 unless ($< == 0) {
31     print "You must be root to run this script.\n";
32     exit 1;
33 }
34
35 my $kohaversion=`cat koha.version`;
36 chomp $kohaversion;
37
38
39 if ($kohaversion =~ /RC/) {
40     print qq|
41 =====================
42 = RELEASE CANDIDATE =
43 =====================
44
45 WARNING WARNING WARNING WARNING WARNING
46
47 You are about to install Koha version $kohaversion.  This version of Koha is a
48 release candidate.  It is not intended to be installed on production systems.
49 It is being released so that users can test it before we release a final
50 version.
51
52 |;
53     print "Are you sure you want to install Koha $kohaversion? (Y/[N]): ";
54
55     my $answer = <STDIN>;
56     chomp $answer;
57
58     if ($answer eq "Y" || $answer eq "y") {
59         print "Great! continuing setup... \n";
60     } else {
61         print qq|
62
63 Watch for announcements of Koha releases on the Koha mailing list or the Koha
64 web site (http://www.koha.org/).
65
66 |;
67         exit;
68     };
69 }
70
71 if (-e "/etc/koha.conf") {
72     my $installedversion=`grep kohaversion= /etc/koha.conf`;
73     chomp $installedversion;
74     $installedversion=~m/kohaversion=(.*)/;
75     $installedversion=$1;
76     if ($installedversion) {
77         $installedversion="You currently have Koha $installedversion on your system.\n";
78     } else {
79         $installedversion="I am not able to determine what version of Koha is installed now.\n";
80     }
81
82     print qq|
83                         ==========================
84                         = Koha already installed =
85                         ==========================
86
87 It looks like Koha is already installed on your system (/etc/koha.conf exists
88 already).  If you would like to upgrade your system to $kohaversion, please use
89 the koha.upgrade script in this directory.
90
91 $installedversion
92
93 |;
94     exit;
95 }
96
97 system('clear');
98 print qq|
99 **********************************
100 * Welcome to the Koha Installer  *
101 **********************************
102 Welcome to the Koha install script!  This script will prompt you for some
103 basic information about your desired setup, then install Koha according to
104 your specifications.  To accept the default value for any question, simply hit
105 Enter at the prompt.
106
107 Please be sure to read the documentation, or visit the Koha website at
108 http://www.koha.org for more information.
109
110 Are you ready to begin the installation? (Y/[N]):
111 |;
112
113 my $answer = <STDIN>;
114 chomp $answer;
115
116 if ($answer eq "Y" || $answer eq "y") {
117         print "Great! continuing setup... \n";
118     } else {
119     print qq|
120 This installer currently does not support a completely automated
121 setup.
122
123 Please be sure to read the documentation, or visit the Koha website
124 at http://www.koha.org for more information.
125 |;
126     exit;
127 };
128
129 print "\n";
130
131 #
132 # Test for Perl and Modules
133 #
134 print qq|
135
136 PERL & MODULES
137 ==============
138
139 |;
140
141 print "\nChecking perl modules ...\n";
142     unless (eval "require 5.004") {
143     die "Sorry, you need at least Perl 5.004\n";
144 }
145
146 my @missing = ();
147 unless (eval {require DBI})               { push @missing,"DBI" };
148 unless (eval {require Date::Manip})       { push @missing,"Date::Manip" };
149 unless (eval {require DBD::mysql})        { push @missing,"DBD::mysql" };
150 unless (eval {require MARC::Record}) { push @missing, "MARC::Record"};
151
152 unless (eval {require Net::Z3950})        {
153     print qq|
154
155 The Net::Z3950 module is missing.  This module is necessary if you want to use
156 Koha's Z39.50 client to download bibliographic records from other libraries.
157 To install this module, you will need the yaz client installed from
158 http://www.indexdata.dk/yaz/ and then you can install the perl module with the
159 command:
160
161 perl -MCPAN -e 'install Net::Z3950'
162
163 Press the <ENTER> key to continue:
164 |;
165     <STDIN>;
166 }
167
168 #
169 # Print out a list of any missing modules
170 #
171 if (@missing > 0) {
172     print "\n\n";
173     print "You are missing some Perl modules which are required by Koha.\n";
174     print "Once these modules have been installed, rerun this installer.\n";
175     print "They can be installed by running (as root) the following:\n";
176     foreach my $module (@missing) {
177         print "   perl -MCPAN -e 'install \"$module\"'\n";
178         exit(1);
179     }} else{
180     print "All modules appear to be installed, continuing...\n";
181 };
182
183
184 print "\n";
185 my $input;
186 my $domainname = `hostname -d`;
187 chomp $domainname;
188 my $opacdir = '/usr/local/koha/opac';
189 my $kohadir = '/usr/local/koha/intranet';
190 my $getdirinfo=1;
191 while ($getdirinfo) {
192     # Loop until opac directory and koha directory are different
193     print qq|
194
195 OPAC DIRECTORY
196 ==============
197 Please supply the directory you want Koha to store its OPAC files in.  Leave off
198 the trailing slash.  This directory will be auto-created for you if it doesn't
199 exist.
200
201 Usually $opacdir
202 |;
203
204     print "Enter directory [$opacdir]: ";
205     chomp($input = <STDIN>);
206
207     if ($input) {
208       $opacdir = $input;
209     }
210
211
212     print qq|
213
214 INTRANET/LIBRARIANS DIRECTORY
215 =============================
216 Please supply the directory you want Koha to store its Intranet/Librarians files
217 in.  Leave off the trailing slash.  This directory will be auto-created for you if
218 it doesn't exist.
219
220 |;
221
222     print "Enter directory [$kohadir]: ";
223     chomp($input = <STDIN>);
224
225     if ($input) {
226       $kohadir = $input;
227     }
228     if ($kohadir eq $opacdir) {
229         print qq|
230
231 You must specify different directories for the OPAC and INTRANET files!
232
233 |;
234     } else {
235         $getdirinfo=0;
236     }
237 }
238
239 #
240 #KOHA conf
241 #
242 my $etcdir = '/etc';
243 my $dbname = 'Koha';
244 my $hostname = 'localhost';
245 my $user = 'kohaadmin';
246 my $pass = '';
247
248 print qq|
249
250 KOHA.CONF
251 =========
252 Koha uses a small configuration file that is placed in your /etc/ files
253 directory. The configuration file, will be created in this directory.
254
255 |;
256
257 #Get the path to the koha.conf directory
258 #print "Enter the path to your configuration directory [$etcdir]: ";
259 #chomp($input = <STDIN>);
260 #
261 #if ($input) {
262 #  $etcdir = $input;
263 #}
264
265
266 #Get the database name
267 print qq|
268
269 Please provide the name of the mysql database for your koha installation.
270 This is normally "$dbname".
271
272 |;
273
274 print "Enter database name [$dbname]: ";
275 chomp($input = <STDIN>);
276
277 if ($input) {
278   $dbname = $input;
279 }
280
281
282 #Get the hostname for the database
283 print qq|
284
285 Please provide the hostname for mysql.  Unless the database is located on another
286 machine this will be "localhost".
287 |;
288
289 print "Enter hostname [$hostname]: ";
290 chomp($input = <STDIN>);
291
292 if ($input) {
293   $hostname = $input;
294 }
295
296 #Get the username for the database
297 print qq|
298
299 Please provide the name of the user, who will have full administrative rights
300 to the $dbname database, when authenticating from $hostname.
301
302 If no user is entered it will default to $user.
303 |;
304
305 print "Enter username [$user]:";
306 chomp($input = <STDIN>);
307
308 if ($input) {
309   $user = $input;
310 }
311
312 #Get the password for the database user
313 print qq|
314
315 Please provide a good password for the user $user.
316 |;
317
318 print "Enter password:";
319 chomp($input = <STDIN>);
320
321 if ($input) {
322   $pass = $input;
323 }
324
325 print "\n";
326
327
328
329 print "Successfully created the Koha configuration file.\n";
330
331 my $httpduser;
332 my $realhttpdconf;
333
334 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
335                       /usr/local/etc/apache/httpd.conf
336                       /usr/local/etc/apache/apache.conf
337                       /var/www/conf/httpd.conf
338                       /etc/apache/conf/httpd.conf
339                       /etc/apache/conf/apache.conf
340                       /etc/apache-ssl/conf/apache.conf
341                       /etc/httpd/conf/httpd.conf
342                       /etc/httpd/httpd.conf)) {
343    if ( -f $httpdconf ) {
344             $realhttpdconf=$httpdconf;
345             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
346       while (<HTTPDCONF>) {
347          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
348             $httpduser = $1;
349          }
350       }
351       close(HTTPDCONF);
352    }
353 }
354 unless ($realhttpdconf) {
355     print qq|
356
357 I was not able to find your apache configuration file.  It is usually
358 called httpd.conf or apache.conf.
359 |;
360     print "Where is your Apache configuratin file? ";
361     chomp($input = <STDIN>);
362
363     if ($input) {
364         $realhttpdconf = $input;
365     } else {
366         $realhttpdconf='';
367     }
368     if ( -f $realhttpdconf ) {
369         open (HTTPDCONF, $realhttpdconf) or warn "Insufficient privileges to open $realhttpdconf for reading.\n";
370         while (<HTTPDCONF>) {
371             if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
372                 $httpduser = $1;
373             }
374         }
375         close(HTTPDCONF);
376     }
377 }
378
379 unless ($httpduser) {
380     print qq|
381
382 I was not able to determine the user that Apache is running as.  This
383 information is necessary in order to set the access privileges correctly on
384 /etc/koha.conf.  This user should be set in one of the Apache configuration
385 files using the "User" directive.
386 |;
387     print "What is your Apache user? ";
388     chomp($input = <STDIN>);
389
390     if ($input) {
391         $httpduser = $input;
392     } else {
393         $httpduser='Undetermined';
394     }
395 }
396
397
398 #
399 #SETUP opac
400 #
401 my $svr_admin = "webmaster\@$domainname";
402 my $servername=`hostname -f`;
403 chomp $servername;
404 my $opacport=80;
405 my $kohaport=8080;
406
407 print qq|
408
409 OPAC and KOHA/LIBRARIAN CONFIGURATION
410 =====================================
411 Koha needs to setup your Apache configuration file for the
412 OPAC and LIBRARIAN virtual hosts.  By default this installer
413 will do this by using one ip address and two different ports
414 for the virtual hosts.  There are other ways to set this up,
415 and the installer will leave comments in httpd.conf detailing
416 what these other options are.
417
418 Please enter the e-mail address for your webserver admin.
419 Usually $svr_admin
420 |;
421
422 print "Enter e-mail address [$svr_admin]:";
423 chomp($input = <STDIN>);
424
425 if ($input) {
426   $svr_admin = $input;
427 }
428
429
430 print qq|
431
432
433 Please enter the domain name or ip address of your computer.
434 |;
435 print "Enter server name/ip address [$servername]:";
436 chomp($input = <STDIN>);
437
438 if ($input) {
439   $servername = $input;
440 }
441
442 print qq|
443
444 Please enter the port for your OPAC interface.
445 |;
446 print "Enter OPAC port [$opacport]:";
447 chomp($input = <STDIN>);
448
449 if ($input) {
450   $opacport = $input;
451 }
452
453 print qq|
454
455 Please enter the port for your Intranet/Librarian interface.
456 |;
457 print "Enter intranet port [$kohaport]:";
458 chomp($input = <STDIN>);
459
460 if ($input) {
461   $kohaport = $input;
462 }
463
464
465 #
466 # Update Apache Conf File.
467 #
468 #
469
470 my $logfiledir=`grep ^ErrorLog $realhttpdconf`;
471 chomp $logfiledir;
472
473 if ($logfiledir) {
474     $logfiledir=~m#ErrorLog (.*)/[^/]*$#;
475     $logfiledir=$1;
476 }
477
478 unless ($logfiledir) {
479     $logfiledir='logs';
480 }
481 print qq|
482
483 UPDATING APACHE.CONF
484 ====================
485
486 |;
487
488
489 print "Checking for modules that need to be loaded...\n";
490 my $httpdconf='';
491 my $envmodule=0;
492 my $includesmodule=0;
493 open HC, $realhttpdconf;
494 while (<HC>) {
495     if (/^\s*#\s*LoadModule env_module /) {
496         s/^\s*#\s*//;
497         print "  Loading env_module in httpd.conf\n";
498         $envmodule=1;
499     }
500     if (/^\s*#\s*LoadModule includes_module /) {
501         s/^\s*#\s*//;
502         print "  Loading includes_module in httpd.conf\n";
503     }
504     if (/\s*LoadModule includes_module / ) {
505         $includesmodule=1;
506     }
507     $httpdconf.=$_;
508 }
509
510 my $apachebackupmade=0;
511 if ($envmodule || $includesmodule) {
512     system("mv -f $realhttpdconf $realhttpdconf\.prekoha");
513     $apachebackupmade=1;
514     open HC, ">$realhttpdconf";
515     print HC $httpdconf;
516     close HC;
517 }
518
519
520 if (`grep 'VirtualHost $servername' $realhttpdconf`) {
521     print qq|
522 $realhttpdconf appears to already have an entry for Koha
523 Virtual Hosts.  You may need to edit $realhttpdconf
524 if anything has changed since it was last set up.  This
525 script will not attempt to modify an existing Koha apache
526 configuration.
527
528 |;
529     print "Press <ENTER> to continue...";
530     <STDIN>;
531     print "\n";
532 } else {
533     unless ($apachebackupmade) {
534         system("cp -f $realhttpdconf $realhttpdconf\.prekoha");
535     }
536     my $includesdirectives='';
537     if ($includesmodule) {
538         $includesdirectives.="Options +Includes\n";
539         $includesdirectives.="   AddHandler server-parsed .html\n";
540     }
541     open(SITE,">>$realhttpdconf") or warn "Insufficient priveleges to open $realhttpdconf for writing.\n";
542     print SITE <<EOP
543
544
545 # Ports to listen to for Koha
546 Listen $opacport
547 Listen $kohaport
548
549 # NameVirtualHost is used by one of the optional configurations detailed below
550
551 #NameVirtualHost 11.22.33.44
552
553 # KOHA's OPAC Configuration
554 <VirtualHost $servername\:$opacport>
555    ServerAdmin $svr_admin
556    DocumentRoot $opacdir/htdocs
557    ServerName $servername
558    ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
559    ErrorLog $logfiledir/opac-error_log
560    TransferLog $logfiledir/opac-access_log
561    SetEnv PERL5LIB "$kohadir/modules"
562    $includesdirectives
563 </VirtualHost>
564
565 # KOHA's INTRANET Configuration
566 <VirtualHost $servername\:$kohaport>
567    ServerAdmin $svr_admin
568    DocumentRoot $kohadir/htdocs
569    ServerName $servername
570    ScriptAlias /cgi-bin/koha/ "$kohadir/cgi-bin/"
571    ErrorLog $logfiledir/koha-error_log
572    TransferLog $logfiledir/koha-access_log
573    SetEnv PERL5LIB "$kohadir/modules"
574    $includesdirectives
575 </VirtualHost>
576
577 # If you want to use name based Virtual Hosting:
578 #   1. remove the two Listen lines
579 #   2. replace $servername\:$opacport wih your.opac.domain.name
580 #   3. replace ServerName $servername wih ServerName your.opac.domain.name
581 #   4. replace $servername\:$kohaport wih your intranet domain name
582 #   5. replace ServerName $servername wih ServerName your.intranet.domain.name
583 #
584 # If you want to use NameVirtualHost'ing (using two names on one ip address):
585 #   1.  Follow steps 1-5 above
586 #   2.  Uncomment the NameVirtualHost line and set the correct ip address
587
588 EOP
589 ;
590
591
592     print qq|
593
594 Intranet Authentication
595 =======================
596
597 I can set it up so that the Intranet/Librarian site is password protected.
598 |;
599 print "Would you like to do this? ([Y]/N): ";
600 chomp($input = <STDIN>);
601
602 my $apacheauthusername='librarian';
603 my $apacheauthpassword='';
604 unless ($input=~/^n/i) {
605     print "\nEnter a userid to login with [$apacheauthusername]: ";
606     chomp ($input = <STDIN>);
607     if ($input) {
608         $apacheauthusername=$input;
609         $apacheauthusername=~s/[^a-zA-Z0-9]//g;
610     }
611     while (! $apacheauthpassword) {
612         print "\nEnter a password for the $apacheauthusername user: ";
613         chomp ($input = <STDIN>);
614         if ($input) {
615             $apacheauthpassword=$input;
616         }
617         if (!$apacheauthpassword) {
618             print "\nPlease enter a password.\n";
619         }
620     }
621     open AUTH, ">/etc/kohaintranet.pass";
622     my $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
623     my $salt=substr($chars, int(rand(length($chars))),1);
624     $salt.=substr($chars, int(rand(length($chars))),1);
625     print AUTH $apacheauthusername.":".crypt($apacheauthpassword, $salt)."\n";
626     close AUTH;
627     print SITE <<EOP
628
629 <Directory $kohadir>
630     AuthUserFile /etc/kohaintranet.pass
631     AuthType Basic
632     AuthName "Koha Intranet (for librarians only)"
633     Require  valid-user
634 </Directory>
635 EOP
636 }
637
638     close(SITE);
639
640     print "Successfully updated Apache Configuration file.\n";
641 }
642
643 print qq|
644
645 SETTING UP Z39.50 DAEMON
646 ========================
647 |;
648
649 my $kohalogdir='/var/log/koha';
650 print "Directory for logging by Z39.50 daemon [$kohalogdir]: ";
651 chomp($input = <STDIN>);
652 if ($input) {
653     $kohalogdir=$input;
654 }
655
656 unless (-e "$kohalogdir") {
657     my $result = mkdir "$kohalogdir",  0770;
658     if ($result==0) {
659         my @dirs = split(m#/#, $kohalogdir);
660         my $checkdir='';
661         foreach (@dirs) {
662             $checkdir.="$_/";
663             unless (-e "$checkdir") {
664                 mkdir($checkdir, 0775);
665             }
666         }
667     }
668 }
669
670 #
671 # Setup the modules directory
672 #
673 print qq|
674
675 CREATING REQUIRED DIRECTORIES
676 =============================
677
678 |;
679
680
681 unless ( -d $kohadir ) {
682    print "Creating $kohadir...\n";
683    my $result=mkdir ($kohadir, 0770);
684    if ($result==0) {
685        my @dirs = split(m#/#, $kohadir);
686         my $checkdir='';
687         foreach (@dirs) {
688             $checkdir.="$_/";
689             unless (-e "$checkdir") {
690                 mkdir($checkdir, 0775);
691             }
692         }
693    }
694    chown (0, (getgrnam($httpduser))[2], "$kohadir");
695    chmod (0770, "$kohadir");
696 }
697 unless ( -d "$kohadir/htdocs" ) {
698    print "Creating $kohadir/htdocs...\n";
699    mkdir ("$kohadir/htdocs", 0750);
700 }
701 unless ( -d "$kohadir/cgi-bin" ) {
702    print "Creating $kohadir/cgi-bin...\n";
703    mkdir ("$kohadir/cgi-bin", 0750);
704 }
705 unless ( -d "$kohadir/modules" ) {
706    print "Creating $kohadir/modules...\n";
707    mkdir ("$kohadir/modules", 0750);
708 }
709 unless ( -d "$kohadir/scripts" ) {
710    print "Creating $kohadir/scripts...\n";
711    mkdir ("$kohadir/scripts", 0750);
712 }
713 unless ( -d $opacdir ) {
714    print "Creating $opacdir...\n";
715    my $result=mkdir ($opacdir, 0770);
716    if ($result==0) {
717        my @dirs = split(m#/#, $opacdir);
718         my $checkdir='';
719         foreach (@dirs) {
720             $checkdir.="$_/";
721             unless (-e "$checkdir") {
722                 mkdir($checkdir, 0775);
723             }
724         }
725    }
726    chown (0, (getgrnam($httpduser))[2], "$opacdir");
727    chmod (0770, "$opacdir");
728 }
729 unless ( -d "$opacdir/htdocs" ) {
730    print "Creating $opacdir/htdocs...\n";
731    mkdir ("$opacdir/htdocs", 0750);
732 }
733 unless ( -d "$opacdir/cgi-bin" ) {
734    print "Creating $opacdir/cgi-bin...\n";
735    mkdir ("$opacdir/cgi-bin", 0750);
736 }
737
738
739
740 print "\n\nINSTALLING KOHA...\n";
741 print "\n\n==================\n";
742 print "Copying internet-html files to $kohadir/htdocs...\n";
743 system("cp -R intranet-html/* $kohadir/htdocs/");
744 print "Copying intranet-cgi files to $kohadir/cgi-bin...\n";
745 system("cp -R intranet-cgi/* $kohadir/cgi-bin/");
746 print "Copying script files to $kohadir/scripts...\n";
747 system("cp -R scripts/* $kohadir/scripts/");
748 print "Copying module files to $kohadir/modules...\n";
749 system("cp -R modules/* $kohadir/modules/");
750 print "Copying opac-html files to $opacdir/htdocs...\n";
751 system("cp -R opac-html/* $opacdir/htdocs/");
752 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
753 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
754
755 system("chown -R root.$httpduser $opacdir");
756 system("chown -R root.$httpduser $kohadir");
757
758
759
760 #Create the configuration file
761 open(SITES,">$etcdir/koha.conf") or warn "Couldn't create file
762 at $etcdir.  Must have write capability.\n";
763 print SITES <<EOP
764 database=$dbname
765 hostname=$hostname
766 user=$user
767 pass=$pass
768 intrahtdocs=$kohadir/cgi-bin/koha-tmpl/intranet-tmpl
769 opachtdocs=$kohadir/cgi-bin/koha-tmpl/opac-tmpl
770 kohalogdir=$kohalogdir
771 kohaversion=$kohaversion
772 httpduser=$httpduser
773 EOP
774 ;
775 close(SITES);
776
777 #
778 # Set ownership of the koha.conf file for security
779 #
780 chown((getpwnam($httpduser)) [2,3], "$etcdir/koha.conf") or warn "can't chown koha.conf: $!";
781 chmod 0440, "$etcdir/koha.conf";
782
783
784 print qq|
785
786 MYSQL CONFIGURATION
787 ===================
788 |;
789 my $mysql;
790 my $mysqldir;
791 my $mysqluser = 'root';
792 my $mysqlpass = '';
793
794 foreach my $mysql (qw(/usr/local/mysql
795                       /opt/mysql
796                       /usr
797                       )) {
798    if ( -d $mysql ) {
799             $mysqldir=$mysql;
800    }
801 }
802 if (!$mysqldir){
803     $mysqldir='/usr';
804 }
805 print qq|
806 To allow us to create the koha database please supply the
807 mysql\'s root users password
808 |;
809
810 my $needpassword=1;
811 while ($needpassword) {
812     print "Enter mysql\'s root users password: ";
813     chomp($input = <STDIN>);
814     $mysqlpass = $input;
815     my $result=system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass proc > /dev/null 2>&1");
816     if ($result) {
817         print "\n\nInvalid password for the MySql root user.\n\n";
818     } else {
819         $needpassword=0;
820     }
821 }
822
823
824 print qq|
825
826 CREATING DATABASE
827 =================
828 |;
829 my $result=system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass create $dbname");
830 if ($result) {
831     print "\nCouldn't connect to the MySQL server for the reason given above.\n";
832     print "This is a serious problem, the database will not get installed.\a\n";
833     print "Press <ENTER> to continue...";
834     <STDIN>;
835     print "\n";
836 } else {
837     system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname < koha.mysql");
838     system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass mysql -e \"insert into user (Host,User,Password) values ('$hostname','$user',password('$pass'))\"\;");
839     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')\"");
840     system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass reload");
841
842     system ("perl -I $kohadir/modules scripts/updater/updatedatabase");
843
844
845     print qq|
846
847 SAMPLE DATA
848 ===========
849 If you are installing Koha for evaluation purposes,  I have a batch of sample
850 data that you can install now.
851
852 If you are installing Koha with the intention of populating it with your own
853 data, you probably don't want this sample data installed.
854 |;#'
855     print "\nWould you like to install the sample data? Y/[N]: ";
856     chomp($input = <STDIN>);
857     if ($input =~/^y/i) {
858         system("gunzip sampledata-1.2.gz");
859         system("cat sampledata-1.2 | $mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname");
860         system("gzip -9 sampledata-1.2");
861         system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname -e \"insert into branches (branchcode,branchname,issuing) values ('MAIN', 'Main Library', 1)\"");
862         system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname -e \"insert into printers (printername,printqueue,printtype) values ('Circulation Desk Printer', 'lp', 'hp')\"");
863         print qq|
864
865 Sample data has been installed.  For some suggestions on testing Koha, please
866 read the file doc/HOWTO-Testing.  If you find any bugs, please submit them at
867 http://bugs.koha.org/.  If you need help with testing Koha, you can post a
868 question through the koha-devel mailing list, or you can check for a developer
869 online at +irc.katipo.co.nz:6667 channel #koha.
870
871 You can find instructions for subscribing to the Koha mailing lists at:
872
873     http://www.koha.org
874
875
876 Press <ENTER> to continue...
877 |;
878         <STDIN>;
879     } else {
880         print "\n\nWould you like to add a branch and printer? [Y]/N: ";
881         chomp($input = <STDIN>);
882
883
884         unless ($input =~/^n/i) {
885             my $branch='Main Library';
886             print "Enter a name for the library branch [$branch]: ";
887             chomp($input = <STDIN>);
888             if ($input) {
889                 $branch=$input;
890             }
891             $branch=~s/[^A-Za-z0-9\s]//g;
892             my $branchcode=$branch;
893             $branchcode=~s/[^A-Za-z0-9]//g;
894             $branchcode=uc($branchcode);
895             $branchcode=substr($branchcode,0,4);
896             print "Enter a four letter code for your branch [$branchcode]: ";
897             chomp($input = <STDIN>);
898             if ($input) {
899                 $branchcode=$input;
900             }
901             $branchcode=~s/[^A-Z]//g;
902             $branchcode=uc($branchcode);
903             $branchcode=substr($branchcode,0,4);
904             print "Adding branch '$branch' with code '$branchcode'.\n";
905             system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname -e \"insert into branches (branchcode,branchname,issuing) values ('$branchcode', '$branch', 1)\"");
906             my $printername='Library Printer';
907             print "Enter a name for the printer [$printername]: ";
908             chomp($input = <STDIN>);
909             if ($input) {
910                 $printername=$input;
911             }
912             $printername=~s/[^A-Za-z0-9\s]//g;
913             my $printerqueue='lp';
914             print "Enter the queue for the printer [$printerqueue]: ";
915             chomp($input = <STDIN>);
916             if ($input) {
917                 $printerqueue=$input;
918             }
919             $printerqueue=~s/[^A-Za-z0-9]//g;
920             system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname -e \"insert into printers (printername,printqueue,printtype) values ('$printername', '$printerqueue', '')\"");
921         }
922     }
923
924
925 }
926
927 print qq|
928
929 UPDATING DATABASE (MARC TABLES)
930 ===============================
931 |;
932
933 $answer="";
934 while ($answer ne "1" && $answer ne "2" && $answer ne "3") {
935         print qq|
936 You can import marc parameters for :
937 1- english marc21 or for
938 2- french unimarc.
939 3- none.
940 Please choose which parameter you want to install. Note if you choose 3, nothing will be added, and it can be a BIG job to manually create those tables
941 |;
942         $answer = <STDIN>;
943         chomp $answer;
944 }
945 if ($answer eq "1") {
946 system("cat script/misc/marc_datas/marc21_en/structure_def.sql | $mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname");
947 }
948 if ($answer eq "2") {
949 system("cat scripts/misc/marc_datas/unimarc_fr/structure_def.sql | $mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname");
950 system("cat scripts/misc/lang-datas/fr/stopwords.sql | $mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname");
951 }
952
953 #system ("perl -I $kohadir/modules scripts/marc/fill_usmarc.pl");
954 system ("perl -I $kohadir/modules scripts/marc/updatedb2marc.pl");
955
956 chmod 0770, $kohalogdir;
957 chown((getpwnam($httpduser)) [2,3], $kohalogdir) or warn "can't chown $kohalogdir: $!";
958
959 # LAUNCH SCRIPT
960 print "Modifying Z39.50 daemon launch script...\n";
961 my $newfile='';
962 open (L, "$kohadir/scripts/z3950daemon/z3950-daemon-launch.sh");
963 while (<L>) {
964     if (/^RunAsUser=/) {
965         $newfile.="RunAsUser=$httpduser\n";
966     } elsif (/^KohaZ3950Dir=/) {
967         $newfile.="KohaZ3950Dir=$kohadir/scripts/z3950daemon\n";
968     } else {
969         $newfile.=$_;
970     }
971 }
972 close L;
973 system("mv $kohadir/scripts/z3950daemon/z3950-daemon-launch.sh $kohadir/scripts/z3950daemon/z3950-daemon-launch.sh.orig");
974 open L, ">$kohadir/scripts/z3950daemon/z3950-daemon-launch.sh";
975 print L $newfile;
976 close L;
977
978
979 # SHELL SCRIPT
980 print "Modifying Z39.50 daemon wrapper script...\n";
981 $newfile='';
982 open (S, "$kohadir/scripts/z3950daemon/z3950-daemon-shell.sh");
983 while (<S>) {
984     if (/^KohaModuleDir=/) {
985         $newfile.="KohaModuleDir=$kohadir/modules\n";
986     } elsif (/^KohaZ3950Dir=/) {
987         $newfile.="KohaZ3950Dir=$kohadir/scripts/z3950daemon\n";
988     } elsif (/^LogDir=/) {
989         $newfile.="LogDir=$kohalogdir\n";
990     } else {
991         $newfile.=$_;
992     }
993 }
994 close S;
995
996 system("mv $kohadir/scripts/z3950daemon/z3950-daemon-shell.sh $kohadir/scripts/z3950daemon/z3950-daemon-shell.sh.orig");
997 open S, ">$kohadir/scripts/z3950daemon/z3950-daemon-shell.sh";
998 print S $newfile;
999 close S;
1000 chmod 0750, "$kohadir/scripts/z3950daemon/z3950-daemon-launch.sh";
1001 chmod 0750, "$kohadir/scripts/z3950daemon/z3950-daemon-shell.sh";
1002 chmod 0750, "$kohadir/scripts/z3950daemon/processz3950queue";
1003 chown(0, (getpwnam($httpduser)) [3], "$kohadir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $kohadir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
1004 chown(0, (getpwnam($httpduser)) [3], "$kohadir/scripts/z3950daemon/processz3950queue") or warn "can't chown $kohadir/scripts/z3950daemon/processz3950queue: $!";
1005
1006
1007 #RESTART APACHE
1008 print "\n\n";
1009 print qq|
1010
1011 COMPLETED
1012 =========
1013 Congratulations ... your Koha installation is almost complete!
1014 The final step is to restart your webserver.
1015
1016 You will be able to connect to your Librarian interface at:
1017
1018    http://$servername\:$kohaport/
1019
1020 and the OPAC interface at :
1021
1022    http://$servername\:$opacport/
1023
1024
1025 Be sure to read the INSTALL, and Hints files.
1026
1027 For more information visit http://www.koha.org
1028
1029 Would you like to restart your webserver now? (Y/[N]):
1030 |;
1031
1032 my $restart = <STDIN>;
1033 chomp $restart;
1034
1035 if ($restart=~/^y/i) {
1036         # Need to support other init structures here?
1037         if (-e "/etc/rc.d/init.d/httpd") {
1038             system('/etc/rc.d/init.d/httpd restart');
1039         } elsif (-e "/etc/init.d/apache") {
1040             system('/etc//init.d/apache restart');
1041         } elsif (-e "/etc/init.d/apache-ssl") {
1042             system('/etc/init.d/apache-ssl restart');
1043         }
1044     } else {
1045         print qq|
1046 Congratulations ... your Koha installation is complete!
1047 You will need to restart your webserver before using Koha!
1048 |;
1049     exit;
1050 };