*** empty log message ***
[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 Net::Z3950})        { 
125     print qq|
126
127 The Net::Z3950 module is missing.  This module is necessary if you want to use
128 Koha's Z39.50 client to download bibliographic records from other libraries.
129 To install this module, you will need the yaz client installed from
130 http://www.indexdata.dk/yaz/ and then you can install the perl module with the
131 command:
132
133 perl -MCPAN -e 'install Net::Z3950'
134
135 Press the <ENTER> key to continue:
136 |;
137     <STDIN>;
138 }
139
140 #
141 # Print out a list of any missing modules
142 #
143 if (@missing > 0) {
144     print "\n\n";
145     print "You are missing some Perl modules which are required by Koha.\n";
146     print "Once these modules have been installed, rerun this installer.\n";
147     print "They can be installed by running (as root) the following:\n";
148     foreach my $module (@missing) {
149         print "   perl -MCPAN -e 'install \"$module\"'\n";
150         exit(1);
151     }} else{
152     print "All modules appear to be installed, continuing...\n";
153 };
154
155
156 my $backupdir='/usr/local/koha/backups';
157 print "Please specify a backup directory [$backupdir]: ";  
158
159 $answer = <STDIN>;
160 chomp $answer;
161
162 if ($answer) {
163     $backupdir=$answer;
164 }
165
166 if (! -e $backupdir) {
167    my $result=mkdir ($backupdir, oct(770));
168    if ($result==0) {
169        my @dirs = split(m#/#, $backupdir);
170         my $checkdir='';
171         foreach (@dirs) {
172             $checkdir.="$_/";
173             unless (-e "$checkdir") {
174                 mkdir($checkdir, 0775);
175             }
176         }
177    }
178 }
179
180 chmod 0770, $backupdir;
181
182 # Backup MySql database
183 #
184 #
185 my $mysql;
186 my $mysqldir;
187
188 foreach my $mysql (qw(/usr/local/mysql
189                       /opt/mysql
190                     )) {
191     if ( -d $mysql ) {
192             $mysqldir=$mysql;
193     }
194 }
195 if (!$mysqldir){
196     $mysqldir='/usr';
197 }
198
199
200
201 my ($sec, $min, $hr, $day, $month, $year) = (localtime(time))[0,1,2,3,4,5];
202 $month++;
203 $year+=1900;
204 my $date= sprintf "%4d-%02d-%02d_%02d:%02d:%02d", $year, $month, $day,$hr,$min,$sec;
205
206 open (MD, "$mysqldir/bin/mysqldump --user=$user --password=$pass --host=$hostname $database|");
207
208 (open BF, ">$backupdir/Koha.backup_$date") || (die "Error opening up backup file $backupdir/Koha.backup_$date: $!\n");
209
210 my $itemcounter=0;
211 my $bibliocounter=0;
212 my $biblioitemcounter=0;
213 my $membercounter=0;
214
215 while (<MD>) {
216     (/insert into items /i) && ($itemcounter++);
217     (/insert into biblioitems /i) && ($biblioitemcounter++);
218     (/insert into biblio /i) && ($bibliocounter++);
219     (/insert into borrowers /i) && ($membercounter++);
220     print BF $_;
221 }
222
223 close BF;
224 close MD;
225
226
227 my $filels=`ls -hl $backupdir/Koha.backup_$date`;
228 chomp $filels;
229 printf qq|
230
231 Backed up:
232
233 %6d biblio entries
234 %6d biblioitems entries
235 %6d items entries
236 %6d borrowers
237
238 $filels
239
240 Does this look right? ([Y]/N):
241 |, $bibliocounter, $biblioitemcounter, $itemcounter, $membercounter;
242
243 $answer = <STDIN>;
244 chomp $answer;
245
246 if ($answer=~/^n/i) {
247     print qq|
248
249 Aborting.  The database dump is located in:
250
251         $backupdir/Koha.backup_$date
252
253 |;
254     exit;
255 } else {
256         print "Great! continuing upgrade... \n";
257 };
258
259
260
261 if ($opacdir && $intranetdir) {
262     print qq|
263
264 I believe that your old files are located in:
265
266   OPAC:      $opacdir
267   INTRANET:  $intranetdir
268
269
270 Does this look right?  ([Y]/N):
271 |;
272     $answer = <STDIN>;
273     chomp $answer;
274
275     if ($answer =~/n/i) {
276         $intranetdir='';
277         $opacdir='';
278     } else {
279         print "Great! continuing upgrade... \n";
280     }
281 }
282
283
284 if (!$opacdir || !$intranetdir) {
285     $intranetdir='';
286     $opacdir='';
287     while (!$intranetdir) {
288         print "Please specify the location of your INTRANET files: ";  
289
290         $answer = <STDIN>;
291         chomp $answer;
292
293         if ($answer) {
294             $intranetdir=$answer;
295         }
296         if (! -e "$intranetdir/htdocs") {
297             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
298             $intranetdir='';
299         }
300     }
301     while (!$opacdir) {
302         print "Please specify the location of your OPAC files: ";  
303
304         $answer = <STDIN>;
305         chomp $answer;
306
307         if ($answer) {
308             $opacdir=$answer;
309         }
310         if (! -e "$opacdir/htdocs") {
311             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
312             $opacdir='';
313         }
314     }
315 }
316
317
318
319 print "\n\nBacking up old Koha scripts...\n";
320 print     "===============================\n\n";
321
322 mkdir "$backupdir/kohafiles-$date", 0770;
323 mkdir "$backupdir/kohafiles-$date/intranet", 0770;
324 mkdir "$backupdir/kohafiles-$date/opac", 0770;
325
326 my $result=system("cp -R $intranetdir/* $backupdir/kohafiles-$date/intranet/");
327 if ($result) {
328     print "Error encounted when copying $intranetdir to $backupdir/kohafiles-$date/intranet/\n";
329     exit;
330 } else {
331     system("rm -rf $intranetdir/*");
332 }
333 $result=system("cp -R $opacdir/* $backupdir/kohafiles-$date/opac/");
334 if ($result) {
335     print "Error encounted when copying $opacdir to $backupdir/kohafiles-$date/opac/\n";
336     exit;
337 } else {
338     system("rm -rf $opacdir/*");
339 }
340
341    print "Creating $intranetdir/htdocs...\n";
342    mkdir ("$intranetdir/htdocs", oct(750));
343    print "Creating $intranetdir/cgi-bin...\n";
344    mkdir ("$intranetdir/cgi-bin", oct(750));
345    print "Creating $intranetdir/modules...\n";
346    mkdir ("$intranetdir/modules", oct(750));
347    print "Creating $intranetdir/scripts...\n";
348    mkdir ("$intranetdir/scripts", oct(750));
349    chmod (oct(770), "$opacdir");
350    print "Creating $opacdir/htdocs...\n";
351    mkdir ("$opacdir/htdocs", oct(750));
352    print "Creating $opacdir/cgi-bin...\n";
353    mkdir ("$opacdir/cgi-bin", oct(750));
354
355 my $httpduser;
356 my $realhttpdconf;
357
358 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
359                       /usr/local/etc/apache/httpd.conf
360                       /usr/local/etc/apache/apache.conf
361                       /var/www/conf/httpd.conf
362                       /etc/apache/conf/httpd.conf
363                       /etc/apache/conf/apache.conf
364                       /etc/apache-ssl/conf/apache.conf
365                       /etc/httpd/conf/httpd.conf
366                       /etc/httpd/httpd.conf)) {
367    if ( -f $httpdconf ) {
368             $realhttpdconf=$httpdconf;
369             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
370       while (<HTTPDCONF>) {
371          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
372             $httpduser = $1;
373          }
374       }
375       close(HTTPDCONF);
376    }
377 }
378
379 unless ($httpduser) {
380     print qq|
381 I was not able to determine the user that Apache is running as.  This
382 information is necessary in order to set the access privileges correctly on
383 /etc/koha.conf.  This user should be set in one of the Apache configuration
384 files using the "User" directive.
385 |;
386     print "What is your Apache user? ";
387     chomp($input = <STDIN>);
388
389     if ($input) {
390         $httpduser = $input;
391     } else {
392         $httpduser='Undetermined';
393     }
394 }
395
396 print "\n\nINSTALLING KOHA...\n";
397 print "\n\n==================\n";
398 print "Copying internet-html files to $intranetdir/htdocs...\n";
399 system("cp -R intranet-html/* $intranetdir/htdocs/");
400 print "Copying intranet-cgi files to $intranetdir/cgi-bin...\n";
401 system("cp -R intranet-cgi/* $intranetdir/cgi-bin/");
402 print "Copying script files to $intranetdir/scripts...\n";
403 system("cp -R scripts/* $intranetdir/scripts/");
404 print "Copying module files to $intranetdir/modules...\n";
405 system("cp -R modules/* $intranetdir/modules/");
406 print "Copying opac-html files to $opacdir/htdocs...\n";
407 system("cp -R opac-html/* $opacdir/htdocs/");
408 print "Copying opac-cgi files to $opacdir/cgi-bin...\n";
409 system("cp -R opac-cgi/* $opacdir/cgi-bin/");
410
411 system("chown -R root.$httpduser $opacdir");
412 system("chown -R root.$httpduser $intranetdir");
413
414 # LAUNCH SCRIPT
415 print "Modifying Z39.50 daemon launch script...\n";
416 my $newfile='';
417 open (L, "$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh");
418 while (<L>) {
419     if (/^RunAsUser=/) {
420         $newfile.="RunAsUser=$httpduser\n";
421     } elsif (/^KohaZ3950Dir=/) {
422         $newfile.="KohaZ3950Dir=$intranetdir/scripts/z3950daemon\n";
423     } else {
424         $newfile.=$_;
425     }
426 }
427 close L;
428 system("mv $intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh $intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh.orig");
429 open L, ">$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
430 print L $newfile;
431 close L;
432
433 unless ($kohalogdir && -e $kohalogdir) {
434     $kohalogdir='/var/log/koha';
435     print "\n\nDirectory for logging by Z39.50 daemon [$kohalogdir]: ";
436     chomp($input = <STDIN>);
437     if ($input) {
438         $kohalogdir=$input;
439     }
440 }
441
442 unless (-e "$kohalogdir") {
443     my $result = mkdir 0770, "$kohalogdir"; 
444     if ($result==0) {
445         my @dirs = split(m#/#, $kohalogdir);
446         my $checkdir='';
447         foreach (@dirs) {
448             $checkdir.="$_/";
449             unless (-e "$checkdir") {
450                 mkdir($checkdir, 0775);
451             }
452         }
453     }
454 }
455
456 # SHELL SCRIPT
457 print "Modifying Z39.50 daemon wrapper script...\n";
458 $newfile='';
459 open (S, "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh");
460 while (<S>) {
461     if (/^KohaModuleDir=/) {
462         $newfile.="KohaModuleDir=$intranetdir/modules\n";
463     } elsif (/^KohaZ3950Dir=/) {
464         $newfile.="KohaZ3950Dir=$intranetdir/scripts/z3950daemon\n";
465     } elsif (/^LogDir=/) {
466         $newfile.="LogDir=$kohalogdir\n";
467     } else {
468         $newfile.=$_;
469     }
470 }
471 close S;
472
473 system("mv $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh.orig");
474 open S, ">$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
475 print S $newfile;
476 close S;
477 chmod 0750, "$intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
478 chmod 0750, "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
479 chmod 0750, "$intranetdir/scripts/z3950daemon/processz3950queue";
480 chown(0, (getpwnam($httpduser)) [3], "$intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
481 chown(0, (getpwnam($httpduser)) [3], "$intranetdir/scripts/z3950daemon/processz3950queue") or warn "can't chown $intranetdir/scripts/z3950daemon/processz3950queue: $!";
482
483
484 open (KC, "/etc/koha.conf");
485 my $kccontents='';
486 my $kc;
487 while (<KC>) {
488     if (/^\s*includes\s*=/) {
489         $kccontents.="includes=$intranetdir/htdocs/includes\n";
490         $kc->{'includes'}=1;
491     } elsif (/^\s*httpduser\s*=/) {
492         $kccontents.="httpduser=$httpduser\n";
493         $kc->{'httpduser'}=1;
494     } elsif (/^\s*kohaversion\s*=/) {
495         $kccontents.="kohaversion=$newversion\n";
496         $kc->{'kohaversion'}=1;
497     } elsif (/^\s*kohalogdir\s*=/) {
498         $kccontents.="kohalogdir=$kohalogdir\n";
499         $kc->{'kohalogdir'}=1;
500     } elsif (/^\s*intranetdir\s*=/) {
501         $kccontents.="intranetdir=$intranetdir\n";
502         $kc->{'intranetdir'}=1;
503     } elsif (/^\s*opacdir\s*=/) {
504         $kccontents.="opacdir=$opacdir\n";
505         $kc->{'opacdir'}=1;
506     } else {
507         $kccontents.="$_";
508     }
509 }
510
511 unless (defined($kc->{'kohaversion'})) {
512     $kccontents.="kohaversion=$newversion\n";
513 }
514 unless (defined($kc->{'includes'})) {
515     $kccontents.="includes=$intranetdir/htdocs/includes\n";
516 }
517 unless (defined($kc->{'httpduser'})) {
518     $kccontents.="httpduser=$httpduser\n";
519 }
520 unless (defined($kc->{'intranetdir'})) {
521     $kccontents.="intranetdir=$intranetdir\n";
522 }
523 unless (defined($kc->{'opacdir'})) {
524     $kccontents.="opacdir=$opacdir\n";
525 }
526 unless (defined($kc->{'kohalogdir'})) {
527     $kccontents.="kohalogdir=$kohalogdir\n";
528 }
529
530
531 system("mv /etc/koha.conf /etc/koha.conf.backup");
532
533 open (KC, ">/etc/koha.conf") || warn "Couldn't open /etc/koha.conf for writing.";
534 print KC $kccontents;
535 close KC;
536
537
538 print qq|
539
540 Upgrading Database
541 ==================
542 |;
543 system ("perl -I $intranetdir/modules scripts/updater/updatedatabase");
544
545
546
547 print qq|
548
549 ==========================
550 = Koha Upgrade Completed =
551 ==========================
552 The Koha Upgrade is finished.  If you are upgrading from a version of Koha
553 prior to 1.2.1, it is likely that you will have to modify your Apache
554 configuration to point it to the new files.
555
556 In your INTRANET VirtualHost section you should have:
557   DocumentRoot $intranetdir/htdocs
558   ScriptAlias /cgi-bin/koha/ $intranetdir/cgi-bin/
559   SetEnv PERL5LIB $intranetdir/modules
560
561 In the OPAC VirtualHost section you should have:
562   DocumentRoot $opacdir/htdocs
563   ScriptAlias /cgi-bin/koha/ $opacdir/cgi-bin/
564   SetEnv PERL5LIB $intranetdir/modules
565
566 You may also need to uncomment a "LoadModules env_module ... " line and restart
567 Apache.
568
569 Please report any problems you encounter through http://bugs.koha.org/
570 |;