Bug fix. Again, it's not sufficient that they are equal, they must also
[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 system('clear');
12 print qq|
13 **********************************
14 * Welcome to the Koha Installer  *
15 **********************************
16 Welcome to the Koha install script!  This script will prompt you for some
17 basic information about your desired setup, then install Koha according to
18 your specifications.  To accept the default value for any question, simply hit
19 Enter at the prompt.
20
21 Please be sure to read the documentation, or visit the Koha website at 
22 http://www.koha.org for more information.
23
24 Are you ready to begin the installation? (Y/[N]):
25 |;
26
27 my $answer = <STDIN>;
28 chomp $answer;
29
30 if ($answer eq "Y" || $answer eq "y") {
31         print "Great! continuing setup... \n";
32     } else {
33     print qq|
34 This installer currently does not support an completely automated 
35 setup.
36
37 Please be sure to read the documentation, or visit the Koha website 
38 at http://www.koha.org for more information.
39 |;
40     exit;
41 };
42
43 print "\n";
44
45 #
46 # Test for Perl and Modules
47 #
48 print qq|
49
50 PERL & MODULES
51 ==============
52
53 |;
54
55 print "\nChecking perl modules ...\n";
56     unless (eval "require 5.004") {
57     die "Sorry, you need at least Perl 5.004\n";
58 }
59
60 my @missing = ();
61 unless (eval {require DBI})               { push @missing,"DBI" };
62 unless (eval {require Date::Manip})       { push @missing,"Date::Manip" };
63 unless (eval {require DBD::mysql})        { push @missing,"DBD::mysql" };
64 unless (eval {require Set::Scalar})       { push @missing,"Set::Scalar" };
65
66 #
67 # Print out a list of any missing modules
68 #
69 if (@missing > 0) {
70     print "\n\n";
71     print "You are missing some Perl modules which are required by Koha.\n";
72     print "Once these modules have been installed, rerun this installer.\n";
73     print "They can be installed by running (as root) the following:\n";
74     foreach my $module (@missing) {
75         print "   perl -MCPAN -e 'install \"$module\"'\n";
76         exit(1);
77     }} else{
78     print "All modules appear to be installed, continuing...\n";
79 };
80
81
82 print "\n";
83 my $input;
84 my $domainname = `hostname -d`;
85 chomp $domainname;
86 my $opacdir = '/usr/local/koha/opac';
87 my $kohadir = '/usr/local/koha/intranet';
88 my $getdirinfo=1;
89 while ($getdirinfo) {
90     # Loop until opac directory and koha directory are different
91     print qq|
92
93 OPAC DIRECTORY
94 ==============
95 Please supply the directory you want Koha to store its OPAC files in.  Leave off
96 the trailing slash.  This directory will be auto-created for you if it doesn't
97 exist.
98
99 Usually $opacdir
100 |;
101
102     print "Enter directory [$opacdir]: ";
103     chomp($input = <STDIN>);
104
105     if ($input) {
106       $opacdir = $input;
107     }
108
109
110     print qq|
111
112 INTRANET/LIBRARIANS DIRECTORY
113 =============================
114 Please supply the directory you want Koha to store its Intranet/Librarians files 
115 in.  Leave off the trailing slash.  This directory will be auto-created for you if 
116 it doesn't exist.
117
118 |;
119
120     print "Enter directory [$kohadir]: ";
121     chomp($input = <STDIN>);
122
123     if ($input) {
124       $kohadir = $input;
125     }
126     if ($kohadir eq $opacdir) {
127         print qq|
128
129 You must specify different directories for the OPAC and INTRANET files!
130
131 |;
132     } else {
133         $getdirinfo=0;
134     }
135 }
136
137 #
138 #KOHA conf
139 #
140 my $etcdir = '/etc';
141 my $dbname = 'Koha';
142 my $hostname = 'localhost';
143 my $user = 'kohaadmin';
144 my $pass = '';
145
146 print qq|
147
148 KOHA.CONF
149 =========
150 Koha uses a small configuration file that is usually placed in your /etc/ files 
151 directory. The configuration file, will be created in this directory
152
153 |;
154
155 #Get the path to the koha.conf directory
156 print "Enter the path to your configuration directory [$etcdir]: ";
157 chomp($input = <STDIN>);
158
159 if ($input) {
160   $etcdir = $input;
161 }
162
163
164 #Get the database name
165 print qq|
166
167 Please provide the name of the mysql database for your koha installation.
168 This is normally "$dbname".
169
170 |;
171
172 print "Enter database name [$dbname]: ";
173 chomp($input = <STDIN>);
174
175 if ($input) {
176   $dbname = $input;
177 }
178
179
180 #Get the hostname for the database
181 print qq|
182
183 Please provide the hostname for mysql.  Unless the database is located on another 
184 machine this will be "localhost".
185 |;
186
187 print "Enter hostname [$hostname]: ";
188 chomp($input = <STDIN>);
189
190 if ($input) {
191   $hostname = $input;
192 }
193
194 #Get the username for the database
195 print qq|
196
197 Please provide the name of the user, who will have full administrative rights
198 to the $dbname database, when authenticating from $hostname.
199
200 If no user is entered it will default to $user.
201 |;
202
203 print "Enter username [$user]:";
204 chomp($input = <STDIN>);
205
206 if ($input) {
207   $user = $input;
208 }
209
210 #Get the password for the database user
211 print qq|
212
213 Please provide a good password for the user $user.
214 |;
215
216 print "Enter password:";
217 chomp($input = <STDIN>);
218
219 if ($input) {
220   $pass = $input;
221 }
222
223 print "\n";
224
225
226 #Create the configuration file
227 open(SITES,">$etcdir/koha.conf") or warn "Couldn't create file
228 at $etcdir.  Must have write capability.\n";
229 print SITES <<EOP
230 database=$dbname
231 hostname=$hostname
232 user=$user
233 pass=$pass
234 includes=$kohadir/htdocs/includes
235 EOP
236 ;
237 close(SITES);
238
239 print "Successfully created the Koha configuration file.\n";
240
241 my $httpduser;
242 my $realhttpdconf;
243
244 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
245                       /usr/local/etc/apache/httpd.conf
246                       /usr/local/etc/apache/apache.conf
247                       /var/www/conf/httpd.conf
248                       /etc/apache/conf/httpd.conf
249                       /etc/apache/conf/apache.conf
250                       /etc/httpd/conf/httpd.conf
251                       /etc/httpd/httpd.conf)) {
252    if ( -f $httpdconf ) {
253             $realhttpdconf=$httpdconf;
254             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
255       while (<HTTPDCONF>) {
256          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
257             $httpduser = $1;
258          }
259       }
260       close(HTTPDCONF);
261    }
262 }
263 $httpduser ||= 'Undetermined';
264
265 #
266 # Set ownership of the koha.conf file for security
267 #
268 chown((getpwnam($httpduser)) [2,3], "$etcdir/koha.conf") or warn "can't chown koha.conf: $!";
269 chmod 0440, "$etcdir/koha.conf";
270
271 #
272 #SETUP opac
273 #
274 my $svr_admin = "webmaster\@$domainname";
275 my $opac_svr_name = "opac.$domainname";
276 my $koha_svr_name = "koha.$domainname";
277
278 print qq|
279
280 OPAC and KOHA/LIBRARIAN CONFIGURATION
281 =====================================
282 Koha needs to setup your Apache configuration file for the
283 OPAC virtual host.
284
285 Please enter the e-mail address for your webserver admin.
286 Usually $svr_admin
287 |;
288
289 print "Enter e-mail address [$svr_admin]:";
290 chomp($input = <STDIN>);
291
292 if ($input) {
293   $svr_admin = $input;
294 }
295
296
297 print qq|
298
299 Please enter the servername for your OPAC interface.
300 Usually $opac_svr_name
301 |;
302 print "Enter servername address [$opac_svr_name]:";
303 chomp($input = <STDIN>);
304
305 if ($input) {
306   $opac_svr_name = $input;
307 }
308
309 print qq|
310
311 Please enter the servername for your Intranet/Librarian interface.
312 Usually $koha_svr_name
313 |;
314 print "Enter servername address [$koha_svr_name]:";
315 chomp($input = <STDIN>);
316
317 if ($input) {
318   $koha_svr_name = $input;
319 }
320
321
322 #
323 # Update Apache Conf File.
324 #
325 #
326
327 my $logfiledir=`grep ^ErrorLog $realhttpdconf`;
328 chomp $logfiledir;
329
330 if ($logfiledir) {
331     $logfiledir=~m#ErrorLog (.*)/[^/]*$#;
332     $logfiledir=$1;
333 }
334
335 unless ($logfiledir) {
336     $logfiledir='logs';
337 }
338 print qq|
339
340 UPDATING APACHE.CONF
341 ====================
342
343 |;
344 if (`grep 'VirtualHost $opac_svr_name' $realhttpdconf`) {
345     warn "$realhttpdconf appears to already have an entry for Koha\nVirtual Hosts.\n";
346     print "Press <ENTER> to continue...";
347     <STDIN>;
348     print "\n";
349 } else {
350     open(SITE,">>$realhttpdconf") or warn "Insufficient priveleges to open $realhttpdconf for writing.\n";
351     print SITE <<EOP
352
353 <VirtualHost $opac_svr_name>
354    ServerAdmin $svr_admin
355    DocumentRoot $opacdir/htdocs
356    ServerName $opac_svr_name
357    ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
358    ErrorLog $logfiledir/opac-error_log
359    TransferLog $logfiledir/opac-access_log
360    SetEnv PERL5LIB "$kohadir/modules"
361 </VirtualHost>
362
363 <VirtualHost $koha_svr_name>
364    ServerAdmin $svr_admin
365    DocumentRoot $kohadir/htdocs
366    ServerName $koha_svr_name
367    ScriptAlias /cgi-bin/koha/ "$kohadir/cgi-bin/"
368    ErrorLog $logfiledir/koha-error_log
369    TransferLog $logfiledir/koha-access_log
370    SetEnv PERL5LIB "$kohadir/modules"
371 </VirtualHost>
372
373 EOP
374 ;
375     close(SITE);
376     print "Successfully updated Apache Configuration file.\n";
377 }
378
379 #
380 # Setup the modules directory
381 #
382 print qq|
383
384 CREATING REQUIRED DIRECTORIES
385 =============================
386
387 |;
388
389
390 unless ( -d $kohadir ) {
391    print "Creating $kohadir...\n";
392    my $result=mkdir ($kohadir, oct(770));
393    if ($result==0) {
394        my @dirs = split(m#/#, $kohadir);
395         my $checkdir='';
396         foreach (@dirs) {
397             $checkdir.="$_/";
398             unless (-e "$checkdir") {
399                 mkdir($checkdir, 0775);
400             }
401         }
402    }
403    chown (oct(0), (getgrnam($httpduser))[2], "$kohadir");
404    chmod (oct(770), "$kohadir");
405 }
406 unless ( -d "$kohadir/htdocs" ) {
407    print "Creating $kohadir/htdocs...\n";
408    mkdir ("$kohadir/htdocs", oct(750));
409 }
410 unless ( -d "$kohadir/cgi-bin" ) {
411    print "Creating $kohadir/cgi-bin...\n";
412    mkdir ("$kohadir/cgi-bin", oct(750));
413 }
414 unless ( -d "$kohadir/modules" ) {
415    print "Creating $kohadir/modules...\n";
416    mkdir ("$kohadir/modules", oct(750));
417 }
418 unless ( -d $opacdir ) {
419    print "Creating $opacdir...\n";
420    my $result=mkdir ($opacdir, oct(770));
421    if ($result==0) {
422        my @dirs = split(m#/#, $opacdir);
423         my $checkdir='';
424         foreach (@dirs) {
425             $checkdir.="$_/";
426             unless (-e "$checkdir") {
427                 mkdir($checkdir, 0775);
428             }
429         }
430    }
431    chown (oct(0), (getgrnam($httpduser))[2], "$opacdir");
432    chmod (oct(770), "$opacdir");
433 }
434 unless ( -d "$opacdir/htdocs" ) {
435    print "Creating $opacdir/htdocs...\n";
436    mkdir ("$opacdir/htdocs", oct(750));
437 }
438 unless ( -d "$opacdir/cgi-bin" ) {
439    print "Creating $opacdir/cgi-bin...\n";
440    mkdir ("$opacdir/cgi-bin", oct(750));
441 }
442
443
444
445 print "\n\nINSTALLING KOHA...\n";
446 print "\n\n==================\n";
447 print "Copying internet-html files to $kohadir/htdocs...\n";
448 system("cp -R intranet-html/* $kohadir/htdocs/");
449 print "Copying intranet-cgi files to $kohadir/cgi-bin...\n";
450 system("cp -R intranet-cgi/* $kohadir/cgi-bin/");
451 print "Copying script files to $kohadir/modules...\n";
452 system("cp -R modules/* $kohadir/modules/");
453 print "Copying opac-html files to $opacdir/htdocs...\n";
454 system("cp -R opac-html/* $opacdir/htdocs/");
455 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
456 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
457
458 system("chown -R root.$httpduser $opacdir");
459 system("chown -R root.$httpduser $kohadir");
460
461 print qq|
462
463 MYSQL CONFIGURATION
464 ===================
465 |;
466 my $mysql;
467 my $mysqldir;
468 my $mysqluser = 'root';
469 my $mysqlpass = '';
470
471 foreach my $mysql (qw(/usr/local/mysql
472                       /opt/mysql
473                       )) {
474    if ( -d $mysql ) {
475             $mysqldir=$mysql;
476    }
477 }
478 if (!$mysqldir){
479     $mysqldir='/usr';
480 }
481 print qq|
482 To allow us to create the koha database please supply the 
483 mysql\'s root users password
484 |;
485
486 print "Enter mysql\'s root users password: ";
487 chomp($input = <STDIN>);
488
489 if ($input) {
490   $mysqlpass = $input;
491 }
492
493
494 print qq|
495
496 CREATING DATABASE
497 =================
498 |;
499 my $result=system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass create $dbname");
500 if ($result) {
501     print "\nCouldn't connect to the MySQL server for the reason given above.\n";
502     print "This is a serious problem, the database will not get installed.\a\n";
503     print "Press <ENTER> to continue...";
504     <STDIN>;
505     print "\n";
506 } else {
507     system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname < koha.mysql");
508     system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass mysql -e \"insert into user (Host,User,Password) values ('$hostname','$user',password('$pass'))\"\;");
509     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')\"");
510     system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass reload");
511
512     system ("perl -I $kohadir/modules scripts/updater/updatedatabase");
513
514
515
516
517     print "\n\nWould you like to add a branch and printer? [Y]/N: ";
518     chomp($input = <STDIN>);
519
520
521     unless ($input =~/^n/i) {
522         my $branch='Main Library';
523         print "Enter a name for the library branch [$branch]: ";
524         chomp($input = <STDIN>);
525         if ($input) {
526             $branch=$input;
527         }
528         $branch=~s/[^A-Za-z0-9\s]//g;
529         my $branchcode=$branch;
530         $branchcode=~s/[^A-Za-z0-9]//g;
531         $branchcode=uc($branchcode);
532         $branchcode=substr($branchcode,0,4);
533         print "Enter a four letter code for your branch [$branchcode]: ";
534         chomp($input = <STDIN>);
535         if ($input) {
536             $branchcode=$input;
537         }
538         $branchcode=~s/[^A-Z]//g;
539         $branchcode=uc($branchcode);
540         $branchcode=substr($branchcode,0,4);
541         print "Adding branch '$branch' with code '$branchcode'.\n";
542         system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass Koha -e \"insert into branches (branchcode,branchname,issuing) values ('$branchcode', '$branch', 1)\"");
543         my $printername='Library Printer';
544         print "Enter a name for the printer [$printername]: ";
545         chomp($input = <STDIN>);
546         if ($input) {
547             $printername=$input;
548         }
549         $printername=~s/[^A-Za-z0-9\s]//g;
550         my $printerqueue='lp';
551         print "Enter the queue for the printer [$printerqueue]: ";
552         chomp($input = <STDIN>);
553         if ($input) {
554             $printerqueue=$input;
555         }
556         $printerqueue=~s/[^A-Za-z0-9]//g;
557         system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass Koha -e \"insert into printers (printername,printqueue,printtype) values ('$printername', '$printerqueue', '')\"");
558     }
559
560
561 }
562
563
564 #RESTART APACHE
565 #system('clear');
566 print "\n\n";
567 print qq|
568 COMPLETED
569 =========
570 Congratulations ... your Koha installation is almost complete!
571 The final step is to restart your webserver.
572
573 Be sure to read the INSTALL, and Hints files. 
574
575 For more information visit http://www.koha.org
576
577 Would you like to restart your webserver now? (Y/[N]):
578 |;
579
580 my $restart = <STDIN>;
581 chomp $restart;
582
583 if ($answer eq "Y" || $answer eq "y") {
584         # Need to support other init structures here?
585         if (-e "/etc/rc.d/init.d/httpd") {
586             system('/etc/rc.d/init.d/httpd restart');
587         } elsif (-e "/etc/init.d/apache") {
588             system('/etc//init.d/apache restart');
589         } elsif (-e "/etc/init.d/apache-ssl") {
590             system('/etc/init.d/apache-ssl restart');
591         }
592     } else {
593     print qq|
594 print "\nCongratulations ... your Koha installation is complete!\n";
595 print "\nYou will need to restart your webserver before using Koha!\n";
596 |;
597     exit;
598 };