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