Added a couple of comments.
[koha.git] / koha.upgrade
1 #!/usr/bin/perl -w
2
3 #use diagnostics;
4 use strict; # please develop with the strict pragma
5
6
7 if ($<) {
8     print "\n\nYou must run koha.upgrade as root.\n\n";
9     exit;
10 }
11
12 my $input;
13
14 my %configfile;
15
16 open (KC, "/etc/koha.conf");
17 while (<KC>) {
18  chomp;
19  (next) if (/^\s*#/);
20  if (/(.*)\s*=\s*(.*)/) {
21    my $variable=$1;
22    my $value=$2;
23    # Clean up white space at beginning and end
24    $variable=~s/^\s*//g;
25    $variable=~s/\s*$//g;
26    $value=~s/^\s*//g;
27    $value=~s/\s*$//g;
28    $configfile{$variable}=$value;
29  }
30 }
31
32 my $intranetdir=$configfile{'intranetdir'};
33 my $opacdir=$configfile{'opacdir'};
34 my $kohaversion=$configfile{'kohaversion'};
35 my $kohalogdir=$configfile{'kohalogdir'};
36 my $database=$configfile{'database'};
37 my $hostname=$configfile{'hostname'};
38 my $user=$configfile{'user'};
39 my $pass=$configfile{'pass'};
40
41
42 ($kohaversion) || ($kohaversion='unknown version');
43 my $newversion=`cat koha.version`;
44 chomp $newversion;
45 if ($newversion =~ /RC/) {
46     print qq|
47 =====================
48 = RELEASE CANDIDATE =
49 =====================
50
51 WARNING WARNING WARNING WARNING WARNING
52
53 You are about to install Koha version $newversion.  This version of Koha is a
54 release candidate.  It is not intended to be installed on production systems.
55 It is being released so that users can test it before we release a final
56 version.
57
58 |;
59     print "Are you sure you want to install Koha $newversion? (Y/[N]): ";
60
61     my $answer = <STDIN>;
62     chomp $answer;
63
64     if ($answer eq "Y" || $answer eq "y") {
65         print "Great! continuing setup... \n";
66     } else {
67         print qq|
68
69 Watch for announcements of Koha releases on the Koha mailing list or the Koha
70 web site (http://www.koha.org/).
71
72 |;
73         exit;
74     };
75 }
76
77 print qq|
78
79 ================
80 = Koha Upgrade =
81 ================
82
83 You are attempting to upgrade from Koha $kohaversion to $newversion.
84
85 We recommend that you do a complete backup of all your files before upgrading.
86 This upgrade script will make a backup copy of your files for you.
87
88 Would you like to proceed?  ([Y]/N):  
89 |;
90
91 my $answer = <STDIN>;
92 chomp $answer;
93
94 if ($answer eq "Y" || $answer eq "y") {
95         print "Great! continuing upgrade... \n";
96     } else {
97     print qq|
98
99 Aborting.  Please re-run koha.upgrade when you are ready to upgrade Koha.
100 |;
101     exit;
102 };
103
104
105 #
106 # Test for Perl and Modules
107 #
108 print qq|
109
110 PERL & MODULES
111 ==============
112
113 |;
114
115 print "\nChecking perl modules ...\n";
116     unless (eval "require 5.004") {
117     die "Sorry, you need at least Perl 5.004\n";
118 }
119
120 my @missing = ();
121 unless (eval {require DBI})               { push @missing,"DBI" };
122 unless (eval {require Date::Manip})       { push @missing,"Date::Manip" };
123 unless (eval {require DBD::mysql})        { push @missing,"DBD::mysql" };
124 unless (eval {require Set::Scalar})       { push @missing,"Set::Scalar" };
125 unless (eval {require Net::Z3950})        { 
126     print qq|
127
128 The Net::Z3950 module is missing.  This module is necessary if you want to use
129 Koha's Z39.50 client to download bibliographic records from other libraries.
130 To install this module, you will need the yaz client installed from
131 http://www.indexdata.dk/yaz/ and then you can install the perl module with the
132 command:
133
134 perl -MCPAN -e 'install Net::Z3950'
135
136 Press the <ENTER> key to continue:
137 |;
138     <STDIN>;
139 }
140
141 #
142 # Print out a list of any missing modules
143 #
144 if (@missing > 0) {
145     print "\n\n";
146     print "You are missing some Perl modules which are required by Koha.\n";
147     print "Once these modules have been installed, rerun this installer.\n";
148     print "They can be installed by running (as root) the following:\n";
149     foreach my $module (@missing) {
150         print "   perl -MCPAN -e 'install \"$module\"'\n";
151         exit(1);
152     }} else{
153     print "All modules appear to be installed, continuing...\n";
154 };
155
156
157 my $backupdir='/usr/local/koha/backups';
158 print "Please specify a backup directory [$backupdir]: ";  
159
160 $answer = <STDIN>;
161 chomp $answer;
162
163 if ($answer) {
164     $backupdir=$answer;
165 }
166
167 if (! -e $backupdir) {
168    my $result=mkdir ($backupdir, oct(770));
169    if ($result==0) {
170        my @dirs = split(m#/#, $backupdir);
171         my $checkdir='';
172         foreach (@dirs) {
173             $checkdir.="$_/";
174             unless (-e "$checkdir") {
175                 mkdir($checkdir, 0775);
176             }
177         }
178    }
179 }
180
181 chmod 0770, $backupdir;
182
183 # Backup MySql database
184 #
185 #
186 my $mysql;
187 my $mysqldir;
188
189 foreach my $mysql (qw(/usr/local/mysql
190                       /opt/mysql
191                     )) {
192     if ( -d $mysql ) {
193             $mysqldir=$mysql;
194     }
195 }
196 if (!$mysqldir){
197     $mysqldir='/usr';
198 }
199
200
201
202 my ($sec, $min, $hr, $day, $month, $year) = (localtime(time))[0,1,2,3,4,5];
203 $month++;
204 $year+=1900;
205 my $date= sprintf "%4d-%02d-%02d_%02d:%02d:%02d", $year, $month, $day,$hr,$min,$sec;
206
207 open (MD, "$mysqldir/bin/mysqldump --user=$user --password=$pass --host=$hostname $database|");
208
209 (open BF, ">$backupdir/Koha.backup_$date") || (die "Error opening up backup file $backupdir/Koha.backup_$date: $!\n");
210
211 my $itemcounter=0;
212 my $bibliocounter=0;
213 my $biblioitemcounter=0;
214 my $membercounter=0;
215
216 while (<MD>) {
217     (/insert into items /i) && ($itemcounter++);
218     (/insert into biblioitems /i) && ($biblioitemcounter++);
219     (/insert into biblio /i) && ($bibliocounter++);
220     (/insert into borrowers /i) && ($membercounter++);
221     print BF $_;
222 }
223
224 close BF;
225 close MD;
226
227
228 my $filels=`ls -hl $backupdir/Koha.backup_$date`;
229 chomp $filels;
230 printf qq|
231
232 Backed up:
233
234 %6d biblio entries
235 %6d biblioitems entries
236 %6d items entries
237 %6d borrowers
238
239 $filels
240
241 Does this look right? ([Y]/N):
242 |, $bibliocounter, $biblioitemcounter, $itemcounter, $membercounter;
243
244 $answer = <STDIN>;
245 chomp $answer;
246
247 if ($answer=~/^n/i) {
248     print qq|
249
250 Aborting.  The database dump is located in:
251
252         $backupdir/Koha.backup_$date
253
254 |;
255     exit;
256 } else {
257         print "Great! continuing upgrade... \n";
258 };
259
260
261
262 if ($opacdir && $intranetdir) {
263     print qq|
264
265 I believe that your old files are located in:
266
267   OPAC:      $opacdir
268   INTRANET:  $intranetdir
269
270
271 Does this look right?  ([Y]/N):
272 |;
273     $answer = <STDIN>;
274     chomp $answer;
275
276     if ($answer =~/n/i) {
277         $intranetdir='';
278         $opacdir='';
279     } else {
280         print "Great! continuing upgrade... \n";
281     }
282 }
283
284
285 if (!$opacdir || !$intranetdir) {
286     $intranetdir='';
287     $opacdir='';
288     while (!$intranetdir) {
289         print "Please specify the location of your INTRANET files: ";  
290
291         $answer = <STDIN>;
292         chomp $answer;
293
294         if ($answer) {
295             $intranetdir=$answer;
296         }
297         if (! -e "$intranetdir/htdocs") {
298             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
299             $intranetdir='';
300         }
301     }
302     while (!$opacdir) {
303         print "Please specify the location of your OPAC files: ";  
304
305         $answer = <STDIN>;
306         chomp $answer;
307
308         if ($answer) {
309             $opacdir=$answer;
310         }
311         if (! -e "$opacdir/htdocs") {
312             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
313             $opacdir='';
314         }
315     }
316 }
317
318
319
320 print "\n\nBacking up old Koha scripts...\n";
321 print     "===============================\n\n";
322
323 mkdir "$backupdir/kohafiles-$date", 0770;
324 mkdir "$backupdir/kohafiles-$date/intranet", 0770;
325 mkdir "$backupdir/kohafiles-$date/opac", 0770;
326
327 my $result=system("cp -R $intranetdir/* $backupdir/kohafiles-$date/intranet/");
328 if ($result) {
329     print "Error encounted when copying $intranetdir to $backupdir/kohafiles-$date/intranet/\n";
330     exit;
331 } else {
332     system("rm -rf $intranetdir/*");
333 }
334 $result=system("cp -R $opacdir/* $backupdir/kohafiles-$date/opac/");
335 if ($result) {
336     print "Error encounted when copying $opacdir to $backupdir/kohafiles-$date/opac/\n";
337     exit;
338 } else {
339     system("rm -rf $opacdir/*");
340 }
341
342    print "Creating $intranetdir/htdocs...\n";
343    mkdir ("$intranetdir/htdocs", oct(750));
344    print "Creating $intranetdir/cgi-bin...\n";
345    mkdir ("$intranetdir/cgi-bin", oct(750));
346    print "Creating $intranetdir/modules...\n";
347    mkdir ("$intranetdir/modules", oct(750));
348    print "Creating $intranetdir/scripts...\n";
349    mkdir ("$intranetdir/scripts", oct(750));
350    chmod (oct(770), "$opacdir");
351    print "Creating $opacdir/htdocs...\n";
352    mkdir ("$opacdir/htdocs", oct(750));
353    print "Creating $opacdir/cgi-bin...\n";
354    mkdir ("$opacdir/cgi-bin", oct(750));
355
356 my $httpduser;
357 my $realhttpdconf;
358
359 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
360                       /usr/local/etc/apache/httpd.conf
361                       /usr/local/etc/apache/apache.conf
362                       /var/www/conf/httpd.conf
363                       /etc/apache/conf/httpd.conf
364                       /etc/apache/conf/apache.conf
365                       /etc/apache-ssl/conf/apache.conf
366                       /etc/httpd/conf/httpd.conf
367                       /etc/httpd/httpd.conf)) {
368    if ( -f $httpdconf ) {
369             $realhttpdconf=$httpdconf;
370             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
371       while (<HTTPDCONF>) {
372          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
373             $httpduser = $1;
374          }
375       }
376       close(HTTPDCONF);
377    }
378 }
379
380 unless ($httpduser) {
381     print qq|
382 I was not able to determine the user that Apache is running as.  This
383 information is necessary in order to set the access privileges correctly on
384 /etc/koha.conf.  This user should be set in one of the Apache configuration
385 files using the "User" directive.
386 |;
387     print "What is your Apache user? ";
388     chomp($input = <STDIN>);
389
390     if ($input) {
391         $httpduser = $input;
392     } else {
393         $httpduser='Undetermined';
394     }
395 }
396
397 print "\n\nINSTALLING KOHA...\n";
398 print "\n\n==================\n";
399 print "Copying internet-html files to $intranetdir/htdocs...\n";
400 system("cp -R intranet-html/* $intranetdir/htdocs/");
401 print "Copying intranet-cgi files to $intranetdir/cgi-bin...\n";
402 system("cp -R intranet-cgi/* $intranetdir/cgi-bin/");
403 print "Copying script files to $intranetdir/scripts...\n";
404 system("cp -R scripts/* $intranetdir/scripts/");
405 print "Copying module files to $intranetdir/modules...\n";
406 system("cp -R modules/* $intranetdir/modules/");
407 print "Copying opac-html files to $opacdir/htdocs...\n";
408 system("cp -R opac-html/* $opacdir/htdocs/");
409 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
410 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
411
412 system("chown -R root.$httpduser $opacdir");
413 system("chown -R root.$httpduser $intranetdir");
414
415 # LAUNCH SCRIPT
416 print "Modifying Z39.50 daemon launch script...\n";
417 my $newfile='';
418 open (L, "$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh");
419 while (<L>) {
420     if (/^RunAsUser=/) {
421         $newfile.="RunAsUser=$httpduser\n";
422     } elsif (/^KohaZ3950Dir=/) {
423         $newfile.="KohaZ3950Dir=$intranetdir/scripts/z3950daemon\n";
424     } else {
425         $newfile.=$_;
426     }
427 }
428 close L;
429 system("mv $intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh $intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh.orig");
430 open L, ">$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
431 print L $newfile;
432 close L;
433
434 unless ($kohalogdir && -e $kohalogdir) {
435     $kohalogdir='/var/log/koha';
436     print "\n\nDirectory for logging by Z39.50 daemon [$kohalogdir]: ";
437     chomp($input = <STDIN>);
438     if ($input) {
439         $kohalogdir=$input;
440     }
441 }
442
443 unless (-e "$kohalogdir") {
444     my $result = mkdir 0770, "$kohalogdir"; 
445     if ($result==0) {
446         my @dirs = split(m#/#, $kohalogdir);
447         my $checkdir='';
448         foreach (@dirs) {
449             $checkdir.="$_/";
450             unless (-e "$checkdir") {
451                 mkdir($checkdir, 0775);
452             }
453         }
454     }
455 }
456
457 # SHELL SCRIPT
458 print "Modifying Z39.50 daemon wrapper script...\n";
459 $newfile='';
460 open (S, "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh");
461 while (<S>) {
462     if (/^KohaModuleDir=/) {
463         $newfile.="KohaModuleDir=$intranetdir/modules\n";
464     } elsif (/^KohaZ3950Dir=/) {
465         $newfile.="KohaZ3950Dir=$intranetdir/scripts/z3950daemon\n";
466     } elsif (/^LogDir=/) {
467         $newfile.="LogDir=$kohalogdir\n";
468     } else {
469         $newfile.=$_;
470     }
471 }
472 close S;
473
474 system("mv $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh.orig");
475 open S, ">$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
476 print S $newfile;
477 close S;
478 chmod 0750, "$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
479 chmod 0750, "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
480 chmod 0750, "$intranetdir/scripts/z3950daemon/processz3950queue";
481 chown(0, (getpwnam($httpduser)) [3], "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
482 chown(0, (getpwnam($httpduser)) [3], "$intranetdir/scripts/z3950daemon/processz3950queue") or warn "can't chown $intranetdir/scripts/z3950daemon/processz3950queue: $!";
483
484
485 open (KC, "/etc/koha.conf");
486 my $kccontents='';
487 my $kc;
488 while (<KC>) {
489     if (/^\s*includes\s*=/) {
490         $kccontents.="includes=$intranetdir/htdocs/includes\n";
491         $kc->{'includes'}=1;
492     } elsif (/^\s*httpduser\s*=/) {
493         $kccontents.="httpduser=$httpduser\n";
494         $kc->{'httpduser'}=1;
495     } elsif (/^\s*kohaversion\s*=/) {
496         $kccontents.="kohaversion=$newversion\n";
497         $kc->{'kohaversion'}=1;
498     } elsif (/^\s*kohalogdir\s*=/) {
499         $kccontents.="kohalogdir=$kohalogdir\n";
500         $kc->{'kohalogdir'}=1;
501     } elsif (/^\s*intranetdir\s*=/) {
502         $kccontents.="intranetdir=$intranetdir\n";
503         $kc->{'intranetdir'}=1;
504     } elsif (/^\s*opacdir\s*=/) {
505         $kccontents.="opacdir=$opacdir\n";
506         $kc->{'opacdir'}=1;
507     } else {
508         $kccontents.="$_";
509     }
510 }
511
512 unless (defined($kc->{'kohaversion'})) {
513     $kccontents.="kohaversion=$newversion\n";
514 }
515 unless (defined($kc->{'includes'})) {
516     $kccontents.="includes=$intranetdir/htdocs/includes\n";
517 }
518 unless (defined($kc->{'httpduser'})) {
519     $kccontents.="httpduser=$httpduser\n";
520 }
521 unless (defined($kc->{'intranetdir'})) {
522     $kccontents.="intranetdir=$intranetdir\n";
523 }
524 unless (defined($kc->{'opacdir'})) {
525     $kccontents.="opacdir=$opacdir\n";
526 }
527 unless (defined($kc->{'kohalogdir'})) {
528     $kccontents.="kohalogdir=$kohalogdir\n";
529 }
530
531
532 system("mv /etc/koha.conf /etc/koha.conf.backup");
533
534 open (KC, ">/etc/koha.conf") || warn "Couldn't open /etc/koha.conf for writing.";
535 print KC $kccontents;
536 close KC;
537
538
539 print qq|
540
541 Upgrading Database
542 ==================
543 |;
544 system ("perl -I $intranetdir/modules scripts/updater/updatedatabase");
545
546
547
548 print qq|
549
550 ==========================
551 = Koha Upgrade Completed =
552 ==========================
553 The Koha Upgrade is finished.  If you are upgrading from a version of Koha
554 prior to 1.2.1, it is likely that you will have to modify your Apache
555 configuration to point it to the new files.
556
557 In your INTRANET VirtualHost section you should have:
558   DocumentRoot $intranetdir/htdocs
559   ScriptAlias /cgi-bin/koha/ $intranetdir/cgi-bin/
560   SetEnv PERL5LIB $intranetdir/modules
561
562 In the OPAC VirtualHost section you should have:
563   DocumentRoot $opacdir/htdocs
564   ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
565   SetEnv PERL5LIB $intranetdir/modules
566
567 You may also need to uncomment a "LoadModules env_module ... " line and restart
568 Apache.
569
570 Please report any problems you encounter through http://bugs.koha.org/
571 |;