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