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