Fixes bug in adding branch... branchcode parameter is called 'searchfield'
[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/www/opac';
87 my $kohadir = '/usr/local/www/koha';
88 print qq|
89
90 OPAC DIRECTORY
91 ==============
92 Please supply the directory you want Koha to store its OPAC files in.  Leave off
93 the trailing slash.  This directory will be auto-created for you if it doesn't
94 exist.
95
96 Usually $opacdir
97 |;
98
99 print "Enter directory [$opacdir]: ";
100 chomp($input = <STDIN>);
101
102 if ($input) {
103   $opacdir = $input;
104 }
105
106
107 print qq|
108
109 INTRANET/LIBRARIANS DIRECTORY
110 =============================
111 Please supply the directory you want Koha to store its Intranet/Librarians files 
112 in.  Leave off the trailing slash.  This directory will be auto-created for you if 
113 it doesn't exist.
114
115 Usually $kohadir
116 |;
117
118 print "Enter directory [$kohadir]: ";
119 chomp($input = <STDIN>);
120
121 if ($input) {
122   $kohadir = $input;
123 }
124
125 #
126 #KOHA conf
127 #
128 my $etcdir = '/etc';
129 my $dbname = 'Koha';
130 my $hostname = 'localhost';
131 my $user = 'kohaadmin';
132 my $pass = '';
133
134 print qq|
135
136 KOHA.CONF
137 =========
138 Koha uses a small configuration file that is usually placed in your /etc/ files 
139 directory. The configuration file, will be created in this directory
140
141 |;
142
143 #Get the path to the koha.conf directory
144 print "Enter the path to your configuration directory [$etcdir]: ";
145 chomp($input = <STDIN>);
146
147 if ($input) {
148   $etcdir = $input;
149 }
150
151
152 #Get the database name
153 print qq|
154
155 Please provide the name of the mysql database for your koha installation.
156 This is normally "$dbname".
157
158 |;
159
160 print "Enter database name [$dbname]: ";
161 chomp($input = <STDIN>);
162
163 if ($input) {
164   $dbname = $input;
165 }
166
167
168 #Get the hostname for the database
169 print qq|
170
171 Please provide the hostname for mysql.  Unless the database is located on another 
172 machine this will be "localhost".
173 |;
174
175 print "Enter hostname [$hostname]: ";
176 chomp($input = <STDIN>);
177
178 if ($input) {
179   $hostname = $input;
180 }
181
182 #Get the username for the database
183 print qq|
184
185 Please provide the name of the user, who will full administrative rights to the 
186 $dbname database, when authenticating from $hostname.
187
188 If no user is entered it will default to $user.
189 |;
190
191 print "Enter username [$user]:";
192 chomp($input = <STDIN>);
193
194 if ($input) {
195   $user = $input;
196 }
197
198 #Get the password for the database user
199 print qq|
200
201 Please provide a good password for the user $user.
202 |;
203
204 print "Enter password:";
205 chomp($input = <STDIN>);
206
207 if ($input) {
208   $pass = $input;
209 }
210
211 print "\n";
212
213
214 #Create the configuration file
215 open(SITES,">$etcdir/koha.conf") or warn "Couldn't create file
216 at $etcdir.  Must have write capability.\n";
217 print SITES <<EOP
218 database=$dbname
219 hostname=$hostname
220 user=$user
221 pass=$pass
222 includes=$kohadir/htdocs/includes
223 EOP
224 ;
225 close(SITES);
226
227 print "Successfully created the Koha configuration file.\n";
228
229 my $httpduser;
230 my $realhttpdconf;
231
232 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
233                       /usr/local/etc/apache/httpd.conf
234                       /usr/local/etc/apache/apache.conf
235                       /var/www/conf/httpd.conf
236                       /etc/apache/conf/httpd.conf
237                       /etc/apache/conf/apache.conf
238                       /etc/httpd/conf/httpd.conf
239                       /etc/httpd/httpd.conf)) {
240    if ( -f $httpdconf ) {
241             $realhttpdconf=$httpdconf;
242             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
243       while (<HTTPDCONF>) {
244          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
245             $httpduser = $1;
246          }
247       }
248       close(HTTPDCONF);
249    }
250 }
251 $httpduser ||= 'Undetermined';
252
253 #
254 # Set ownership of the koha.conf file for security
255 #
256 chown((getpwnam($httpduser)) [2,3], "$etcdir/koha.conf") or warn "can't chown koha.conf: $!";
257 chmod 0440, "$etcdir/koha.conf";
258
259 #
260 #SETUP opac
261 #
262 my $svr_admin = "webmaster\@$domainname";
263 my $opac_svr_name = "opac.$domainname";
264 my $koha_svr_name = "koha.$domainname";
265
266 print qq|
267
268 OPAC and KOHA/LIBRARIAN CONFIGURATION
269 =====================================
270 Koha needs to setup your Apache configuration file for the
271 OPAC virtual host.
272
273 Please enter the e-mail address for your webserver admin.
274 Usually $svr_admin
275 |;
276
277 print "Enter e-mail address [$svr_admin]:";
278 chomp($input = <STDIN>);
279
280 if ($input) {
281   $svr_admin = $input;
282 }
283
284
285 print qq|
286
287 Please enter the servername for your OPAC interface.
288 Usually $opac_svr_name
289 |;
290 print "Enter servername address [$opac_svr_name]:";
291 chomp($input = <STDIN>);
292
293 if ($input) {
294   $opac_svr_name = $input;
295 }
296
297 print qq|
298
299 Please enter the servername for your Intranet/Librarian interface.
300 Usually $koha_svr_name
301 |;
302 print "Enter servername address [$koha_svr_name]:";
303 chomp($input = <STDIN>);
304
305 if ($input) {
306   $koha_svr_name = $input;
307 }
308
309
310 #
311 # Update Apache Conf File.
312 #
313 print qq|
314
315 UPDATING APACHE.CONF
316 ====================
317
318 |;
319 if (`grep 'VirtualHost $opac_svr_name' $realhttpdconf`) {
320     warn "$realhttpdconf appears to already have an entry for Koha\nVirtual Hosts.\n";
321     print "Press <ENTER> to continue...";
322     <STDIN>;
323     print "\n";
324 } else {
325     open(SITE,">>$realhttpdconf") or warn "Insufficient priveleges to open $realhttpdconf for writing.\n";
326     print SITE <<EOP
327
328 <VirtualHost $opac_svr_name>
329    ServerAdmin $svr_admin
330    DocumentRoot $opacdir/htdocs
331    ServerName $opac_svr_name
332    ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
333    ErrorLog logs/opac-error_log
334    TransferLog logs/opac-access_log common
335    SetEnv PERL5LIB "$kohadir/modules"
336 </VirtualHost>
337
338 <VirtualHost $koha_svr_name>
339    ServerAdmin $svr_admin
340    DocumentRoot $kohadir/htdocs
341    ServerName $koha_svr_name
342    ScriptAlias /cgi-bin/koha/ "$kohadir/cgi-bin"
343    ErrorLog logs/koha-error_log
344    TransferLog logs/koha-access_log common
345    SetEnv PERL5LIB "$kohadir/modules"
346 </VirtualHost>
347
348 EOP
349 ;
350     close(SITE);
351     print "Successfully updated Apache Configuration file.\n";
352
353 }
354
355 #
356 # Setup the modules directory
357 #
358 print qq|
359
360 CREATING REQUIRED DIRECTORIES
361 =============================
362
363 |;
364
365 # Still need to check that all parent directories of $kohadir and $opacdir
366 # exist
367
368
369 unless ( -d $kohadir ) {
370    print "Creating $kohadir...\n";
371    mkdir ($kohadir, oct(770));
372    chown (oct(0), (getgrnam($httpduser))[2], "$kohadir");
373    chmod (oct(770), "$kohadir");
374 }
375 unless ( -d "$kohadir/htdocs" ) {
376    print "Creating $kohadir/htdocs...\n";
377    mkdir ("$kohadir/htdocs", oct(750));
378 }
379 unless ( -d "$kohadir/cgi-bin" ) {
380    print "Creating $kohadir/cgi-bin...\n";
381    mkdir ("$kohadir/cgi-bin", oct(750));
382 }
383 unless ( -d "$kohadir/modules" ) {
384    print "Creating $kohadir/modules...\n";
385    mkdir ("$kohadir/modules", oct(750));
386 }
387 unless ( -d $opacdir ) {
388    print "Creating $opacdir...\n";
389    mkdir ($opacdir, oct(770));
390    chown (oct(0), (getgrnam($httpduser))[2], "$opacdir");
391    chmod (oct(770), "$opacdir");
392 }
393 unless ( -d "$opacdir/htdocs" ) {
394    print "Creating $opacdir/htdocs...\n";
395    mkdir ("$opacdir/htdocs", oct(750));
396 }
397 unless ( -d "$opacdir/cgi-bin" ) {
398    print "Creating $opacdir/cgi-bin...\n";
399    mkdir ("$opacdir/cgi-bin", oct(750));
400 }
401
402
403
404 print "\n\nINSTALLING KOHA...\n";
405 print "\n\n==================\n";
406 print "Copying internet-html files to $kohadir/htdocs...\n";
407 system("cp -R intranet-html/* $kohadir/htdocs/");
408 print "Copying intranet-cgi files to $kohadir/cgi-bin...\n";
409 system("cp -R intranet-cgi/* $kohadir/cgi-bin/");
410 print "Copying script files to $kohadir/modules...\n";
411 system("cp -R modules/* $kohadir/modules/");
412 print "Copying opac-html files to $opacdir/htdocs...\n";
413 system("cp -R opac-html/* $opacdir/htdocs/");
414 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
415 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
416
417 print qq|
418
419 MYSQL CONFIGURATION
420 ===================
421 |;
422 my $mysql;
423 my $mysqldir;
424 my $mysqluser = 'root';
425 my $mysqlpass = '';
426
427 foreach my $mysql (qw(/usr/local/mysql
428                       /opt/mysql)) {
429    if ( -d $mysql ) {
430             $mysql=$mysqldir;
431    }
432 }
433 if (!$mysqldir){
434     $mysqldir='/usr';
435 }
436 print qq|
437 To allow us to create the koha database please supply the 
438 mysql\'s root users password
439 |;
440
441 print "Enter mysql\'s root users password: ";
442 chomp($input = <STDIN>);
443
444 if ($input) {
445   $mysqlpass = $input;
446 }
447
448
449 print qq|
450
451 CREATING DATABASE
452 =================
453 |;
454 system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass create $dbname");
455 system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass $dbname < koha.mysql");
456 system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass mysql -e \"insert into user (Host,User,Password) values ('$hostname','$user',password('$pass'))\"\;");
457 system("$mysqldir/bin/mysql -u$mysqluser -p$mysqlpass mysql -e \"insert into db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv) values ('%','$dbname','$user','Y','Y','Y','Y');");
458 system("$mysqldir/bin/mysqladmin -u$mysqluser -p$mysqlpass reload");
459
460 system ("perl scripts/updater/updatedatabase -I $kohadir/modules");
461
462
463 #RESTART APACHE
464 #system('clear');
465 print "\n\n";
466 print qq|
467 COMPLETED
468 =========
469 Congratulations ... your Koha installation is almost complete!
470 The final step is to restart your webserver.
471
472 Be sure to read the INSTALL, and Hints files. 
473
474 For more information visit http://www.koha.org
475
476 Would you like to restart your webserver now? (Y/[N]):
477 |;
478
479 my $restart = <STDIN>;
480 chomp $restart;
481
482 if ($answer eq "Y" || $answer eq "y") {
483         # Need to support other init structures here?
484         if (-e "/etc/rc.d/init.d/httpd") {
485             system('/etc/rc.d/init.d/httpd restart');
486         } elsif (-e "/etc/init.d/apache") {
487             system('/etc//init.d/apache restart');
488         } elsif (-e "/etc/init.d/apache-ssl") {
489             system('/etc/init.d/apache-ssl restart');
490         }
491     } else {
492     print qq|
493 print "\nCongratulations ... your Koha installation is complete!\n";
494 print "\nYou will need to restart your webserver before using Koha!\n";
495 |;
496     exit;
497 };