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