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