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