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