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