Fixed bugs with variable scope after switching some functions to Install.pm
[koha.git] / koha.upgrade
1 #!/usr/bin/perl -w
2
3 #use diagnostics;
4 use strict; # please develop with the strict pragma
5 use Install;
6
7
8 if ($<) {
9     print "\n\nYou must run koha.upgrade as root.\n\n";
10     exit;
11 }
12
13 my $input;
14
15
16 loadconfigfile();
17
18
19
20 ($::kohaversion) || ($::kohaversion='unknown version');
21 my $newversion=`cat koha.version`;
22 chomp $newversion;
23 if ($::kohaversion =~ /RC/) {
24     releasecandidatewarning();
25 }
26
27 print qq|
28
29 ================
30 = Koha Upgrade =
31 ================
32
33 You are attempting to upgrade from Koha $::kohaversion to $newversion.
34
35 We recommend that you do a complete backup of all your files before upgrading.
36 This upgrade script will make a backup copy of your files for you.
37
38 Would you like to proceed?  ([Y]/N):  
39 |;
40
41 my $answer = <STDIN>;
42 chomp $answer;
43
44 if ($answer eq "Y" || $answer eq "y") {
45         print "Great! continuing upgrade... \n";
46     } else {
47     print qq|
48
49 Aborting.  Please re-run koha.upgrade when you are ready to upgrade Koha.
50 |;
51     exit;
52 };
53
54
55
56 checkperlmodules();
57
58
59
60 my $backupdir='/usr/local/koha/backups';
61 print "Please specify a backup directory [$backupdir]: ";  
62
63 $answer = <STDIN>;
64 chomp $answer;
65
66 if ($answer) {
67     $backupdir=$answer;
68 }
69
70 if (! -e $backupdir) {
71    my $result=mkdir ($backupdir, oct(770));
72    if ($result==0) {
73        my @dirs = split(m#/#, $backupdir);
74         my $checkdir='';
75         foreach (@dirs) {
76             $checkdir.="$_/";
77             unless (-e "$checkdir") {
78                 mkdir($checkdir, 0775);
79             }
80         }
81    }
82 }
83
84 chmod 0770, $backupdir;
85
86 # Backup MySql database
87 #
88 #
89 my $mysql;
90 my $mysqldir;
91
92 foreach my $mysql (qw(/usr/local/mysql
93                       /opt/mysql
94                     )) {
95     if ( -d $mysql ) {
96             $mysqldir=$mysql;
97     }
98 }
99 if (!$mysqldir){
100     $mysqldir='/usr';
101 }
102
103
104
105 my ($sec, $min, $hr, $day, $month, $year) = (localtime(time))[0,1,2,3,4,5];
106 $month++;
107 $year+=1900;
108 my $date= sprintf "%4d-%02d-%02d_%02d:%02d:%02d", $year, $month, $day,$hr,$min,$sec;
109
110 open (MD, "$mysqldir/bin/mysqldump --user=$::user --password=$::pass --host=$::hostname $::database|");
111
112 (open BF, ">$backupdir/Koha.backup_$date") || (die "Error opening up backup file $backupdir/Koha.backup_$date: $!\n");
113
114 my $itemcounter=0;
115 my $bibliocounter=0;
116 my $biblioitemcounter=0;
117 my $membercounter=0;
118
119 while (<MD>) {
120     (/insert into items /i) && ($itemcounter++);
121     (/insert into biblioitems /i) && ($biblioitemcounter++);
122     (/insert into biblio /i) && ($bibliocounter++);
123     (/insert into borrowers /i) && ($membercounter++);
124     print BF $_;
125 }
126
127 close BF;
128 close MD;
129
130
131 my $filels=`ls -hl $backupdir/Koha.backup_$date`;
132 chomp $filels;
133 printf qq|
134
135 Backed up:
136
137 %6d biblio entries
138 %6d biblioitems entries
139 %6d items entries
140 %6d borrowers
141
142 $filels
143
144 Does this look right? ([Y]/N):
145 |, $bibliocounter, $biblioitemcounter, $itemcounter, $membercounter;
146
147 $answer = <STDIN>;
148 chomp $answer;
149
150 if ($answer=~/^n/i) {
151     print qq|
152
153 Aborting.  The database dump is located in:
154
155         $backupdir/Koha.backup_$date
156
157 |;
158     exit;
159 } else {
160         print "Great! continuing upgrade... \n";
161 };
162
163
164
165 if ($::opacdir && $::intranetdir) {
166     print qq|
167
168 I believe that your old files are located in:
169
170   OPAC:      $::opacdir
171   INTRANET:  $::intranetdir
172
173
174 Does this look right?  ([Y]/N):
175 |;
176     $answer = <STDIN>;
177     chomp $answer;
178
179     if ($answer =~/n/i) {
180         $::intranetdir='';
181         $::opacdir='';
182     } else {
183         print "Great! continuing upgrade... \n";
184     }
185 }
186
187
188 if (!$::opacdir || !$::intranetdir) {
189     $::intranetdir='';
190     $::opacdir='';
191     while (!$::intranetdir) {
192         print "Please specify the location of your INTRANET files: ";  
193
194         $answer = <STDIN>;
195         chomp $answer;
196
197         if ($answer) {
198             $::intranetdir=$answer;
199         }
200         if (! -e "$::intranetdir/htdocs") {
201             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
202             $::intranetdir='';
203         }
204     }
205     while (!$::opacdir) {
206         print "Please specify the location of your OPAC files: ";  
207
208         $answer = <STDIN>;
209         chomp $answer;
210
211         if ($answer) {
212             $::opacdir=$answer;
213         }
214         if (! -e "$::opacdir/htdocs") {
215             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
216             $::opacdir='';
217         }
218     }
219 }
220
221
222
223 print "\n\nBacking up old Koha scripts...\n";
224 print     "===============================\n\n";
225
226 mkdir "$backupdir/kohafiles-$date", 0770;
227 mkdir "$backupdir/kohafiles-$date/intranet", 0770;
228 mkdir "$backupdir/kohafiles-$date/opac", 0770;
229
230 my $result=system("cp -R $::intranetdir/* $backupdir/kohafiles-$date/intranet/");
231 if ($result) {
232     print "Error encounted when copying $::intranetdir to $backupdir/kohafiles-$date/intranet/\n";
233     exit;
234 } else {
235     system("rm -rf $::intranetdir/*");
236 }
237 $result=system("cp -R $::opacdir/* $backupdir/kohafiles-$date/opac/");
238 if ($result) {
239     print "Error encounted when copying $::opacdir to $backupdir/kohafiles-$date/opac/\n";
240     exit;
241 } else {
242     system("rm -rf $::opacdir/*");
243 }
244
245    print "Creating $::intranetdir/htdocs...\n";
246    mkdir ("$::intranetdir/htdocs", oct(750));
247    print "Creating $::intranetdir/cgi-bin...\n";
248    mkdir ("$::intranetdir/cgi-bin", oct(750));
249    print "Creating $::intranetdir/modules...\n";
250    mkdir ("$::intranetdir/modules", oct(750));
251    print "Creating $::intranetdir/scripts...\n";
252    mkdir ("$::intranetdir/scripts", oct(750));
253    chmod (oct(770), "$::opacdir");
254    print "Creating $::opacdir/htdocs...\n";
255    mkdir ("$::opacdir/htdocs", oct(750));
256    print "Creating $::opacdir/cgi-bin...\n";
257    mkdir ("$::opacdir/cgi-bin", oct(750));
258
259 my $httpduser;
260 my $realhttpdconf;
261
262 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
263                       /usr/local/etc/apache/httpd.conf
264                       /usr/local/etc/apache/apache.conf
265                       /var/www/conf/httpd.conf
266                       /etc/apache/conf/httpd.conf
267                       /etc/apache/conf/apache.conf
268                       /etc/apache-ssl/conf/apache.conf
269                       /etc/httpd/conf/httpd.conf
270                       /etc/httpd/httpd.conf)) {
271    if ( -f $httpdconf ) {
272             $realhttpdconf=$httpdconf;
273             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
274       while (<HTTPDCONF>) {
275          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
276             $httpduser = $1;
277          }
278       }
279       close(HTTPDCONF);
280    }
281 }
282
283 unless ($httpduser) {
284     print qq|
285 I was not able to determine the user that Apache is running as.  This
286 information is necessary in order to set the access privileges correctly on
287 /etc/koha.conf.  This user should be set in one of the Apache configuration
288 files using the "User" directive.
289 |;
290     print "What is your Apache user? ";
291     chomp($input = <STDIN>);
292
293     if ($input) {
294         $httpduser = $input;
295     } else {
296         $httpduser='Undetermined';
297     }
298 }
299
300 print "\n\nINSTALLING KOHA...\n";
301 print "\n\n==================\n";
302 print "Copying internet-html files to $::intranetdir/htdocs...\n";
303 system("cp -R intranet-html/* $::intranetdir/htdocs/");
304 print "Copying intranet-cgi files to $::intranetdir/cgi-bin...\n";
305 system("cp -R intranet-cgi/* $::intranetdir/cgi-bin/");
306 print "Copying script files to $::intranetdir/scripts...\n";
307 system("cp -R scripts/* $::intranetdir/scripts/");
308 print "Copying module files to $::intranetdir/modules...\n";
309 system("cp -R modules/* $::intranetdir/modules/");
310 print "Copying opac-html files to $::opacdir/htdocs...\n";
311 system("cp -R opac-html/* $::opacdir/htdocs/");
312 print "Copying opac-cgi files to $::opacdir/cgi-bin...\n";
313 system("cp -R opac-cgi/* $::opacdir/cgi-bin/");
314 system("touch $::opacdir/cgi-bin/opac");
315
316 system("chown -R root.$httpduser $::opacdir");
317 system("chown -R root.$httpduser $::intranetdir");
318
319
320 unless ($::kohalogdir && -e $::kohalogdir) {
321     $::kohalogdir='/var/log/koha';
322     print "\n\nDirectory for logging by Z39.50 daemon [$::kohalogdir]: ";
323     chomp($input = <STDIN>);
324     if ($input) {
325         $::kohalogdir=$input;
326     }
327 }
328
329 unless (-e "$::kohalogdir") {
330     my $result = mkdir 0770, "$::kohalogdir"; 
331     if ($result==0) {
332         my @dirs = split(m#/#, $::kohalogdir);
333         my $checkdir='';
334         foreach (@dirs) {
335             $checkdir.="$_/";
336             unless (-e "$checkdir") {
337                 mkdir($checkdir, 0775);
338             }
339         }
340     }
341 }
342
343 chmod 0750, "$::intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
344 chmod 0750, "$::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
345 chmod 0750, "$::intranetdir/scripts/z3950daemon/processz3950queue";
346 chown(0, (getpwnam($httpduser)) [3], "$::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
347 chown(0, (getpwnam($httpduser)) [3], "$::intranetdir/scripts/z3950daemon/processz3950queue") or warn "can't chown $::intranetdir/scripts/z3950daemon/processz3950queue: $!";
348
349
350 open (KC, "/etc/koha.conf");
351 my $kccontents='';
352 my $kc;
353 while (<KC>) {
354     if (/^\s*includes\s*=/) {
355         $kccontents.="includes=$::intranetdir/htdocs/includes\n";
356         $kc->{'includes'}=1;
357     } elsif (/^\s*httpduser\s*=/) {
358         $kccontents.="httpduser=$httpduser\n";
359         $kc->{'httpduser'}=1;
360     } elsif (/^\s*kohaversion\s*=/) {
361         $kccontents.="kohaversion=$newversion\n";
362         $kc->{'kohaversion'}=1;
363     } elsif (/^\s*kohalogdir\s*=/) {
364         $kccontents.="kohalogdir=$::kohalogdir\n";
365         $kc->{'kohalogdir'}=1;
366     } elsif (/^\s*intranetdir\s*=/) {
367         $kccontents.="intranetdir=$::intranetdir\n";
368         $kc->{'intranetdir'}=1;
369     } elsif (/^\s*opacdir\s*=/) {
370         $kccontents.="opacdir=$::opacdir\n";
371         $kc->{'opacdir'}=1;
372     } else {
373         $kccontents.="$_";
374     }
375 }
376
377 unless (defined($kc->{'kohaversion'})) {
378     $kccontents.="kohaversion=$newversion\n";
379 }
380 unless (defined($kc->{'includes'})) {
381     $kccontents.="includes=$::intranetdir/htdocs/includes\n";
382 }
383 unless (defined($kc->{'httpduser'})) {
384     $kccontents.="httpduser=$httpduser\n";
385 }
386 unless (defined($kc->{'intranetdir'})) {
387     $kccontents.="intranetdir=$::intranetdir\n";
388 }
389 unless (defined($kc->{'opacdir'})) {
390     $kccontents.="opacdir=$::opacdir\n";
391 }
392 unless (defined($kc->{'kohalogdir'})) {
393     $kccontents.="kohalogdir=$::kohalogdir\n";
394 }
395
396
397 system("mv /etc/koha.conf /etc/koha.conf.backup");
398
399 open (KC, ">/etc/koha.conf") || warn "Couldn't open /etc/koha.conf for writing.";
400 print KC $kccontents;
401 close KC;
402
403
404 print qq|
405
406 Upgrading Database
407 ==================
408 |;
409 system ("perl -I $::intranetdir/modules scripts/updater/updatedatabase");
410
411
412 print qq|
413
414 ==================
415 = Authentication =
416 ==================
417
418 This release of Koha has a new authentication module.  If you are not already
419 using basic authentication on your intranet, you will be required to log in to
420 access some of the features of the intranet.  You can log in using the userid
421 and password from the /etc/koha.conf configuration file at any time.  Use the
422 "Members" module to add passwords for other accounts and set their permissions.
423
424 [NOTE PERMISSIONS ARE NOT COMPLETED AS OF 1.2.3RC1.  Do not give passwords to
425  any patrons unless you want them to have full access to your intranet.]
426 |;
427 print "Press the <ENTER> key to continue: ";
428 <STDIN>;
429
430
431
432 print qq|
433
434 ==========================
435 = Koha Upgrade Completed =
436 ==========================
437 The Koha Upgrade is finished.  If you are upgrading from a version of Koha
438 prior to 1.2.1, it is likely that you will have to modify your Apache
439 configuration to point it to the new files.
440
441 In your INTRANET VirtualHost section you should have:
442   DocumentRoot $::intranetdir/htdocs
443   ScriptAlias /cgi-bin/koha/ $::intranetdir/cgi-bin/
444   SetEnv PERL5LIB $::intranetdir/modules
445
446 In the OPAC VirtualHost section you should have:
447   DocumentRoot $::opacdir/htdocs
448   ScriptAlias /cgi-bin/koha/ $::opacdir/cgi-bin/
449   SetEnv PERL5LIB $::intranetdir/modules
450
451 You may also need to uncomment a "LoadModules env_module ... " line and restart
452 Apache.
453
454 Please report any problems you encounter through http://bugs.koha.org/
455 |;