Fix permissions on /etc/koha.conf on initial install.
[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 #SETUP opac
379 #
380 my $svr_admin = "webmaster\@$domainname";
381 my $servername=`hostname -f`;
382 chomp $servername;
383 my $opacport=80;
384 my $kohaport=8080;
385
386 print qq|
387
388 OPAC and KOHA/LIBRARIAN CONFIGURATION
389 =====================================
390 Koha needs to setup your Apache configuration file for the
391 OPAC and LIBRARIAN virtual hosts.  By default this installer
392 will do this by using one ip address and two different ports
393 for the virtual hosts.  There are other ways to set this up,
394 and the installer will leave comments in httpd.conf detailing
395 what these other options are.
396
397 Please enter the e-mail address for your webserver admin.
398 Usually $svr_admin
399 |;
400
401 print "Enter e-mail address [$svr_admin]:";
402 chomp($input = <STDIN>);
403
404 if ($input) {
405   $svr_admin = $input;
406 }
407
408
409 print qq|
410
411
412 Please enter the domain name or ip address of your computer.
413 |;
414 print "Enter server name/ip address [$servername]:";
415 chomp($input = <STDIN>);
416
417 if ($input) {
418   $servername = $input;
419 }
420
421 print qq|
422
423 Please enter the port for your OPAC interface.
424 |;
425 print "Enter OPAC port [$opacport]:";
426 chomp($input = <STDIN>);
427
428 if ($input) {
429   $opacport = $input;
430 }
431
432 print qq|
433
434 Please enter the port for your Intranet/Librarian interface.
435 |;
436 print "Enter intranet port [$kohaport]:";
437 chomp($input = <STDIN>);
438
439 if ($input) {
440   $kohaport = $input;
441 }
442
443
444 #
445 # Update Apache Conf File.
446 #
447 #
448
449 my $logfiledir=`grep ^ErrorLog $realhttpdconf`;
450 chomp $logfiledir;
451
452 if ($logfiledir) {
453     $logfiledir=~m#ErrorLog (.*)/[^/]*$#;
454     $logfiledir=$1;
455 }
456
457 unless ($logfiledir) {
458     $logfiledir='logs';
459 }
460 print qq|
461
462 UPDATING APACHE.CONF
463 ====================
464
465 |;
466
467
468 print "Checking for modules that need to be loaded...\n";
469 my $httpdconf='';
470 my $envmodule=0;
471 my $includesmodule=0;
472 open HC, $realhttpdconf;
473 while (<HC>) {
474     if (/^\s*#\s*LoadModule env_module /) {
475         s/^\s*#\s*//;
476         print "  Loading env_module in httpd.conf\n";
477         $envmodule=1;
478     }
479     if (/^\s*#\s*LoadModule includes_module /) {
480         s/^\s*#\s*//;
481         print "  Loading includes_module in httpd.conf\n";
482     }
483     if (/\s*LoadModule includes_module / ) {
484         $includesmodule=1;
485     }
486     $httpdconf.=$_;
487 }
488
489 my $apachebackupmade=0;
490 if ($envmodule || $includesmodule) {
491     system("mv -f $realhttpdconf $realhttpdconf\.prekoha");
492     $apachebackupmade=1;
493     open HC, ">$realhttpdconf";
494     print HC $httpdconf;
495     close HC;
496 }
497
498
499 if (`grep 'VirtualHost $servername' $realhttpdconf`) {
500     print qq|
501 $realhttpdconf appears to already have an entry for Koha
502 Virtual Hosts.  You may need to edit $realhttpdconf
503 if anything has changed since it was last set up.  This
504 script will not attempt to modify an existing Koha apache
505 configuration.
506
507 |;
508     print "Press <ENTER> to continue...";
509     <STDIN>;
510     print "\n";
511 } else {
512     unless ($apachebackupmade) {
513         system("cp -f $realhttpdconf $realhttpdconf\.prekoha");
514     }
515     my $includesdirectives='';
516     if ($includesmodule) {
517         $includesdirectives.="Options +Includes\n";
518         $includesdirectives.="   AddHandler server-parsed .html\n";
519     }
520     open(SITE,">>$realhttpdconf") or warn "Insufficient priveleges to open $realhttpdconf for writing.\n";
521     print SITE <<EOP
522
523
524 # Ports to listen to for Koha
525 Listen $opacport
526 Listen $kohaport
527
528 # NameVirtualHost is used by one of the optional configurations detailed below
529
530 #NameVirtualHost 11.22.33.44
531
532 # KOHA's OPAC Configuration
533 <VirtualHost $servername\:$opacport>
534    ServerAdmin $svr_admin
535    DocumentRoot $opacdir/htdocs
536    ServerName $servername
537    ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
538    ErrorLog $logfiledir/opac-error_log
539    TransferLog $logfiledir/opac-access_log
540    SetEnv PERL5LIB "$kohadir/modules"
541    $includesdirectives
542 </VirtualHost>
543
544 # KOHA's INTRANET Configuration
545 <VirtualHost $servername\:$kohaport>
546    ServerAdmin $svr_admin
547    DocumentRoot $kohadir/htdocs
548    ServerName $servername
549    ScriptAlias /cgi-bin/koha/ "$kohadir/cgi-bin/"
550    ErrorLog $logfiledir/koha-error_log
551    TransferLog $logfiledir/koha-access_log
552    SetEnv PERL5LIB "$kohadir/modules"
553    $includesdirectives
554 </VirtualHost>
555
556 # If you want to use name based Virtual Hosting:
557 #   1. remove the two Listen lines
558 #   2. replace $servername\:$opacport wih your.opac.domain.name
559 #   3. replace ServerName $servername wih ServerName your.opac.domain.name
560 #   4. replace $servername\:$kohaport wih your intranet domain name
561 #   5. replace ServerName $servername wih ServerName your.intranet.domain.name
562 #
563 # If you want to use NameVirtualHost'ing (using two names on one ip address):
564 #   1.  Follow steps 1-5 above
565 #   2.  Uncomment the NameVirtualHost line and set the correct ip address
566
567 EOP
568 ;
569
570
571     print qq|
572
573 Intranet Authentication
574 =======================
575
576 I can set it up so that the Intranet/Librarian site is password protected.
577 |;
578 print "Would you like to do this? ([Y]/N): ";
579 chomp($input = <STDIN>);
580
581 my $apacheauthusername='librarian';
582 my $apacheauthpassword='';
583 unless ($input=~/^n/i) {
584     print "\nEnter a userid to login with [$apacheauthusername]: ";
585     chomp ($input = <STDIN>);
586     if ($input) {
587         $apacheauthusername=$input;
588         $apacheauthusername=~s/[^a-zA-Z0-9]//g;
589     }
590     while (! $apacheauthpassword) {
591         print "\nEnter a password for the $apacheauthusername user: ";
592         chomp ($input = <STDIN>);
593         if ($input) {
594             $apacheauthpassword=$input;
595         }
596         if (!$apacheauthpassword) {
597             print "\nPlease enter a password.\n";
598         }
599     }
600     open AUTH, ">/etc/kohaintranet.pass";
601     my $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
602     my $salt=substr($chars, int(rand(length($chars))),1);
603     $salt.=substr($chars, int(rand(length($chars))),1);
604     print AUTH $apacheauthusername.":".crypt($apacheauthpassword, $salt)."\n";
605     close AUTH;
606     print SITE <<EOP
607
608 <Directory $kohadir>
609     AuthUserFile /etc/kohaintranet.pass
610     AuthType Basic
611     AuthName "Koha Intranet (for librarians only)"
612     Require  valid-user
613 </Directory>
614 EOP
615 }
616
617     close(SITE);
618
619     print "Successfully updated Apache Configuration file.\n";
620 }
621
622 print qq|
623
624 SETTING UP Z39.50 DAEMON
625 ========================
626 |;
627
628 my $kohalogdir='/var/log/koha';
629 print "Directory for logging by Z39.50 daemon [$kohalogdir]: ";
630 chomp($input = <STDIN>);
631 if ($input) {
632     $kohalogdir=$input;
633 }
634
635 unless (-e "$kohalogdir") {
636     my $result = mkdir 0770, "$kohalogdir"; 
637     if ($result==0) {
638         my @dirs = split(m#/#, $kohalogdir);
639         my $checkdir='';
640         foreach (@dirs) {
641             $checkdir.="$_/";
642             unless (-e "$checkdir") {
643                 mkdir($checkdir, 0775);
644             }
645         }
646     }
647 }
648
649 #
650 # Setup the modules directory
651 #
652 print qq|
653
654 CREATING REQUIRED DIRECTORIES
655 =============================
656
657 |;
658
659
660 unless ( -d $kohadir ) {
661    print "Creating $kohadir...\n";
662    my $result=mkdir ($kohadir, oct(770));
663    if ($result==0) {
664        my @dirs = split(m#/#, $kohadir);
665         my $checkdir='';
666         foreach (@dirs) {
667             $checkdir.="$_/";
668             unless (-e "$checkdir") {
669                 mkdir($checkdir, 0775);
670             }
671         }
672    }
673    chown (oct(0), (getgrnam($httpduser))[2], "$kohadir");
674    chmod (oct(770), "$kohadir");
675 }
676 unless ( -d "$kohadir/htdocs" ) {
677    print "Creating $kohadir/htdocs...\n";
678    mkdir ("$kohadir/htdocs", oct(750));
679 }
680 unless ( -d "$kohadir/cgi-bin" ) {
681    print "Creating $kohadir/cgi-bin...\n";
682    mkdir ("$kohadir/cgi-bin", oct(750));
683 }
684 unless ( -d "$kohadir/modules" ) {
685    print "Creating $kohadir/modules...\n";
686    mkdir ("$kohadir/modules", oct(750));
687 }
688 unless ( -d "$kohadir/scripts" ) {
689    print "Creating $kohadir/scripts...\n";
690    mkdir ("$kohadir/scripts", oct(750));
691 }
692 unless ( -d $opacdir ) {
693    print "Creating $opacdir...\n";
694    my $result=mkdir ($opacdir, oct(770));
695    if ($result==0) {
696        my @dirs = split(m#/#, $opacdir);
697         my $checkdir='';
698         foreach (@dirs) {
699             $checkdir.="$_/";
700             unless (-e "$checkdir") {
701                 mkdir($checkdir, 0775);
702             }
703         }
704    }
705    chown (oct(0), (getgrnam($httpduser))[2], "$opacdir");
706    chmod (oct(770), "$opacdir");
707 }
708 unless ( -d "$opacdir/htdocs" ) {
709    print "Creating $opacdir/htdocs...\n";
710    mkdir ("$opacdir/htdocs", oct(750));
711 }
712 unless ( -d "$opacdir/cgi-bin" ) {
713    print "Creating $opacdir/cgi-bin...\n";
714    mkdir ("$opacdir/cgi-bin", oct(750));
715 }
716
717
718
719 print "\n\nINSTALLING KOHA...\n";
720 print "\n\n==================\n";
721 print "Copying internet-html files to $kohadir/htdocs...\n";
722 system("cp -R intranet-html/* $kohadir/htdocs/");
723 print "Copying intranet-cgi files to $kohadir/cgi-bin...\n";
724 system("cp -R intranet-cgi/* $kohadir/cgi-bin/");
725 print "Copying script files to $kohadir/scripts...\n";
726 system("cp -R scripts/* $kohadir/scripts/");
727 print "Copying module files to $kohadir/modules...\n";
728 system("cp -R modules/* $kohadir/modules/");
729 print "Copying opac-html files to $opacdir/htdocs...\n";
730 system("cp -R opac-html/* $opacdir/htdocs/");
731 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
732 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
733
734 system("chown -R root.$httpduser $opacdir");
735 system("chown -R root.$httpduser $kohadir");
736
737
738
739 #Create the configuration file
740 open(SITES,">$etcdir/koha.conf") or warn "Couldn't create file
741 at $etcdir.  Must have write capability.\n";
742 print SITES <<EOP
743 database=$dbname
744 hostname=$hostname
745 user=$user
746 pass=$pass
747 includes=$kohadir/htdocs/includes
748 intranetdir=$kohadir
749 opacdir=$opacdir
750 kohalogdir=$kohalogdir
751 kohaversion=$kohaversion
752 httpduser=$httpduser
753 EOP
754 ;
755 close(SITES);
756
757 #
758 # Set ownership of the koha.conf file for security
759 #
760 chown((getpwnam($httpduser)) [2,3], "$etcdir/koha.conf") or warn "can't chown koha.conf: $!";
761 chmod 0440, "$etcdir/koha.conf";
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 };