* The CSS definition has been removed from the config.pl file and
[BackupPC.git] / configure.pl
1 #!/bin/perl
2 #============================================================= -*-perl-*-
3 #
4 # configure.pl: Configuration and installation program for BackupPC
5 #
6 # DESCRIPTION
7 #
8 #   This script should be run as root:
9 #
10 #        perl configure.pl
11 #
12 #   The installation steps are described as the script runs.
13 #
14 # AUTHOR
15 #   Craig Barratt <cbarratt@users.sourceforge.net>
16 #
17 # COPYRIGHT
18 #   Copyright (C) 2001-2004  Craig Barratt
19 #
20 #   This program is free software; you can redistribute it and/or modify
21 #   it under the terms of the GNU General Public License as published by
22 #   the Free Software Foundation; either version 2 of the License, or
23 #   (at your option) any later version.
24 #
25 #   This program is distributed in the hope that it will be useful,
26 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
27 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 #   GNU General Public License for more details.
29 #
30 #   You should have received a copy of the GNU General Public License
31 #   along with this program; if not, write to the Free Software
32 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33 #
34 #========================================================================
35 #
36 # Version 2.0.0_CVS, released 18 Jan 2003.
37 #
38 # See http://backuppc.sourceforge.net.
39 #
40 #========================================================================
41
42 use strict;
43 no  utf8;
44 use vars qw(%Conf %OrigConf);
45 use lib "./lib";
46
47 my @Packages = qw(ExtUtils::MakeMaker File::Path File::Spec File::Copy
48                   DirHandle Digest::MD5 Data::Dumper Getopt::Std
49                   BackupPC::Lib BackupPC::FileZIO);
50
51 foreach my $pkg ( @Packages ) {
52     eval "use $pkg";
53     next if ( !$@ );
54     die <<EOF;
55
56 BackupPC needs the package $pkg.  Please install $pkg
57 before installing BackupPC.
58
59 EOF
60 }
61
62 if ( $< != 0 ) {
63     print <<EOF;
64
65 This configure script should be run as root, rather than uid $<.
66 Provided uid $< has sufficient permissions to create the data and
67 install directories, then it should be ok to proceed.  Otherwise,
68 please quit and restart as root.
69
70 EOF
71     exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
72 }
73
74 print <<EOF;
75
76 Is this a new installation or upgrade for BackupPC?  If this is
77 an upgrade please tell me the full path of the existing BackupPC
78 configuration file (eg: /xxxx/conf/config.pl).  Otherwise, just
79 hit return.
80
81 EOF
82
83 #
84 # Check if this is an upgrade, in which case read the existing
85 # config file to get all the defaults.
86 #
87 my $ConfigPath = "";
88 while ( 1 ) {
89     $ConfigPath = prompt("--> Full path to existing conf/config.pl",
90                                     $ConfigPath);
91     last if ( $ConfigPath eq ""
92             || ($ConfigPath =~ /^\// && -r $ConfigPath && -w $ConfigPath) );
93     my $problem = "is not an absolute path";
94     $problem = "is not writable" if ( !-w $ConfigPath );
95     $problem = "is not readable" if ( !-r $ConfigPath );
96     $problem = "doesn't exist"   if ( !-f $ConfigPath );
97     print("The file '$ConfigPath' $problem.\n");
98 }
99 my $bpc;
100 if ( $ConfigPath ne "" && -r $ConfigPath ) {
101     (my $topDir = $ConfigPath) =~ s{/[^/]+/[^/]+$}{};
102     die("BackupPC::Lib->new failed\n")
103             if ( !($bpc = BackupPC::Lib->new($topDir, ".", 1)) );
104     %Conf = $bpc->Conf();
105     %OrigConf = %Conf;
106     $Conf{TopDir} = $topDir;
107     my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}, 1);
108     if ( $err eq "" ) {
109         print <<EOF;
110
111 BackupPC is running on $Conf{ServerHost}.  You need to stop BackupPC
112 before you can upgrade the code.  Depending upon your installation,
113 you could run "/etc/init.d/backuppc stop".
114
115 EOF
116         exit(1);
117     }
118 }
119
120 #
121 # These are the programs whose paths we need to find
122 #
123 my %Programs = (
124     perl           => "PerlPath",
125     'gtar/tar'     => "TarClientPath",
126     smbclient      => "SmbClientPath",
127     nmblookup      => "NmbLookupPath",
128     rsync          => "RsyncClientPath",
129     ping           => "PingPath",
130     df             => "DfPath",
131     'ssh/ssh2'     => "SshPath",
132     sendmail       => "SendmailPath",
133     hostname       => "HostnamePath",
134     split          => "SplitPath",
135     'parchive/par' => "ParPath",
136     cat            => "CatPath",
137     gzip           => "GzipPath",
138     bzip2          => "Bzip2Path",
139 );
140
141 foreach my $prog ( sort(keys(%Programs)) ) {
142     my $path;
143     foreach my $subProg ( split(/\//, $prog) ) {
144         $path ||= FindProgram("$ENV{PATH}:/bin:/usr/bin:/sbin:/usr/sbin",
145                               $subProg);
146     }
147     $Conf{$Programs{$prog}} ||= $path;
148 }
149
150 while ( 1 ) {
151     print <<EOF;
152
153 I found the following locations for these programs:
154
155 EOF
156     foreach my $prog ( sort(keys(%Programs)) ) {
157         printf("    %-12s => %s\n", $prog, $Conf{$Programs{$prog}});
158     }
159     print "\n";
160     last if (prompt('--> Are these paths correct?', 'y') =~ /^y/i);
161     foreach my $prog ( sort(keys(%Programs)) ) {
162         $Conf{$Programs{$prog}} = prompt("--> $prog path",
163                                          $Conf{$Programs{$prog}});
164     }
165 }
166
167 my $Perl56 = system($Conf{PerlPath}
168                         . q{ -e 'exit($^V && $^V ge v5.6.0 ? 1 : 0);'});
169
170 if ( !$Perl56 ) {
171     print <<EOF;
172
173 BackupPC needs perl version 5.6.0 or later.  $Conf{PerlPath} appears
174 to be an older version.  Please upgrade to a newer version of perl
175 and re-run this configure script.
176
177 EOF
178     exit(1);
179 }
180
181 print <<EOF;
182
183 Please tell me the hostname of the machine that BackupPC will run on.
184
185 EOF
186 chomp($Conf{ServerHost} = `$Conf{HostnamePath}`)
187         if ( defined($Conf{HostnamePath}) && !defined($Conf{ServerHost}) );
188 $Conf{ServerHost} = prompt("--> BackupPC will run on host", $Conf{ServerHost});
189
190 print <<EOF;
191
192 BackupPC should run as a dedicated user with limited privileges.  You
193 need to create a user.  This user will need read/write permission on
194 the main data directory and read/execute permission on the install
195 directory (these directories will be setup shortly).
196
197 The primary group for this user should also be chosen carefully.
198 By default the install directories will have group write permission.
199 The data directories and files will have group read permission but
200 no other permission.
201
202 EOF
203 my($name, $passwd, $Uid, $Gid);
204 while ( 1 ) {
205     $Conf{BackupPCUser} = prompt("--> BackupPC should run as user",
206                                  $Conf{BackupPCUser} || "backuppc");
207     ($name, $passwd, $Uid, $Gid) = getpwnam($Conf{BackupPCUser});
208     last if ( $name ne "" );
209     print <<EOF;
210
211 getpwnam() says that user $Conf{BackupPCUser} doesn't exist.  Please check the
212 name and verify that this user is in the passwd file.
213
214 EOF
215 }
216
217 print <<EOF;
218
219 Please specify an install directory for BackupPC.  This is where the
220 BackupPC scripts, library and documentation will be installed.
221
222 EOF
223
224 while ( 1 ) {
225     $Conf{InstallDir} = prompt("--> Install directory (full path)",
226                                $Conf{InstallDir});
227     last if ( $Conf{InstallDir} =~ /^\// );
228 }
229
230 print <<EOF;
231
232 Please specify a data directory for BackupPC.  This is where the
233 configuration files, LOG files and all the PC backups are stored.
234 This file system needs to be big enough to accommodate all the
235 PCs you expect to backup (eg: at least 1-2GB per machine).
236
237 EOF
238
239 while ( 1 ) {
240     $Conf{TopDir} = prompt("--> Data directory (full path)", $Conf{TopDir});
241     last if ( $Conf{TopDir} =~ /^\// );
242 }
243
244 if ( !defined($Conf{CompressLevel}) ) {
245     $Conf{CompressLevel} = BackupPC::FileZIO->compOk ? 3 : 0;
246     if ( $ConfigPath eq "" && $Conf{CompressLevel} ) {
247         print <<EOF;
248
249 BackupPC can compress pool files, providing around a 40% reduction in pool
250 size (your mileage may vary). Specify the compression level (0 turns
251 off compression, and 1 to 9 represent good/fastest to best/slowest).
252 The recommended values are 0 (off) or 3 (reasonable compression and speed).
253 Increasing the compression level to 5 will use around 20% more cpu time
254 and give perhaps 2-3% more compression.
255
256 EOF
257     } elsif ( $ConfigPath eq "" ) {
258         print <<EOF;
259
260 BackupPC can compress pool files, but it needs the Compress::Zlib
261 package installed (see www.cpan.org). Compression will provide around a
262 40% reduction in pool size, at the expense of cpu time.  You can leave
263 compression off and run BackupPC without compression, in which case you
264 should leave the compression level at 0 (which means off).  You could
265 install Compress::Zlib and turn compression on later, but read the
266 documentation first about how to do this.  Or the better choice is
267 to quit, install Compress::Zlib, and re-run configure.pl.
268
269 EOF
270     } elsif ( $Conf{CompressLevel} ) {
271         $Conf{CompressLevel} = 0;
272         print <<EOF;
273
274 BackupPC now supports pool file compression.  Since you are upgrading
275 BackupPC you probably have existing uncompressed backups.  You have
276 several choices if you want to turn on compression.  You can run
277 the script BackupPC_compressPool to convert everything to compressed
278 form.  Or you can simply turn on compression, so that new backups
279 will be compressed.  This will increase the pool storage requirement,
280 since both uncompressed and compressed copies of files will be stored.
281 But eventually the old uncompressed backups will expire, recovering
282 the pool storage.  Please see the documentation for more details.
283
284 If you are not sure what to do, leave the Compression Level at 0,
285 which disables compression.  You can always read the documentation
286 and turn it on later.
287
288 EOF
289     } else {
290         $Conf{CompressLevel} = 0;
291         print <<EOF;
292
293 BackupPC now supports pool file compression, but it needs the
294 Compress::Zlib module (see www.cpan.org).  For now, leave
295 the compression level set at 0 to disable compression.  If you
296 want you can install Compress::Zlib and turn compression on.
297 Please see the documentation for more details about converting
298 old backups to compressed form.
299
300 EOF
301     }
302     while ( 1 ) {
303         $Conf{CompressLevel}
304                     = prompt("--> Compression level", $Conf{CompressLevel});
305         last if ( $Conf{CompressLevel} =~ /^\d+$/ );
306     }
307 }
308
309 print <<EOF;
310
311 BackupPC has a powerful CGI perl interface that runs under Apache.
312 A single executable needs to be installed in a cgi-bin directory.
313 This executable needs to run as set-uid $Conf{BackupPCUser}, or
314 it can be run under mod_perl with Apache running as user $Conf{BackupPCUser}.
315
316 Leave this path empty if you don't want to install the CGI interface.
317
318 EOF
319
320 while ( 1 ) {
321     $Conf{CgiDir} = prompt("--> CGI bin directory (full path)", $Conf{CgiDir});
322     last if ( $Conf{CgiDir} =~ /^\// || $Conf{CgiDir} eq "" );
323 }
324
325 if ( $Conf{CgiDir} ne "" ) {
326
327     print <<EOF;
328
329 BackupPC's CGI script needs to display various GIF images that
330 should be stored where Apache can serve them.  They should be
331 placed somewher under Apache's DocumentRoot.  BackupPC also
332 needs to know the URL to access these images.  Example:
333
334     Apache image directory:  /usr/local/apache/htdocs/BackupPC
335     URL for image directory: /BackupPC
336
337 The URL for the image directory should start with a slash.
338
339 EOF
340     while ( 1 ) {
341         $Conf{CgiImageDir} = prompt("--> Apache image directory (full path)",
342                                         $Conf{CgiImageDir});
343         last if ( $Conf{CgiImageDir} =~ /^\// );
344     }
345     while ( 1 ) {
346         $Conf{CgiImageDirURL} = prompt("--> URL for image directory (omit http://host; starts with '/')",
347                                         $Conf{CgiImageDirURL});
348         last if ( $Conf{CgiImageDirURL} =~ /^\// );
349     }
350 }
351
352 print <<EOF;
353
354 Ok, we're about to:
355
356   - install the binaries, lib and docs in $Conf{InstallDir},
357   - create the data directory $Conf{TopDir},
358   - create/update the config.pl file $Conf{TopDir}/conf,
359   - optionally install the cgi-bin interface.
360
361 EOF
362
363 exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
364
365 #
366 # Create install directories
367 #
368 foreach my $dir ( qw(bin doc
369                      lib/BackupPC/CGI
370                      lib/BackupPC/Lang
371                      lib/BackupPC/Xfer
372                      lib/BackupPC/Zip
373                  ) ) {
374     next if ( -d "$Conf{InstallDir}/$dir" );
375     mkpath("$Conf{InstallDir}/$dir", 0, 0775);
376     if ( !-d "$Conf{InstallDir}/$dir"
377             || !chown($Uid, $Gid, "$Conf{InstallDir}/$dir") ) {
378         die("Failed to create or chown $Conf{InstallDir}/$dir\n");
379     } else {
380         print("Created $Conf{InstallDir}/$dir\n");
381     }
382 }
383
384 #
385 # Create CGI image directory
386 #
387 foreach my $dir ( ($Conf{CgiImageDir}) ) {
388     next if ( $dir eq "" || -d $dir );
389     mkpath($dir, 0, 0775);
390     if ( !-d $dir || !chown($Uid, $Gid, $dir) ) {
391         die("Failed to create or chown $dir");
392     } else {
393         print("Created $dir\n");
394     }
395 }
396
397 #
398 # Create $TopDir's top-level directories
399 #
400 foreach my $dir ( qw(. conf pool cpool pc trash log) ) {
401     mkpath("$Conf{TopDir}/$dir", 0, 0750) if ( !-d "$Conf{TopDir}/$dir" );
402     if ( !-d "$Conf{TopDir}/$dir"
403             || !chown($Uid, $Gid, "$Conf{TopDir}/$dir") ) {
404         die("Failed to create or chown $Conf{TopDir}/$dir\n");
405     } else {
406         print("Created $Conf{TopDir}/$dir\n");
407     }
408 }
409
410 printf("Installing binaries in $Conf{InstallDir}/bin\n");
411 foreach my $prog ( qw(BackupPC BackupPC_dump BackupPC_link BackupPC_nightly
412         BackupPC_sendEmail BackupPC_tarCreate BackupPC_trashClean
413         BackupPC_tarExtract BackupPC_compressPool BackupPC_zcat
414         BackupPC_archive BackupPC_archiveHost
415         BackupPC_restore BackupPC_serverMesg BackupPC_zipCreate ) ) {
416     InstallFile("bin/$prog", "$Conf{InstallDir}/bin/$prog", 0555);
417 }
418
419 #
420 # Remove unused binaries from older versions
421 #
422 unlink("$Conf{InstallDir}/bin/BackupPC_queueAll");
423
424 printf("Installing library in $Conf{InstallDir}/lib\n");
425 foreach my $lib ( qw(
426         BackupPC/Lib.pm
427         BackupPC/FileZIO.pm
428         BackupPC/Attrib.pm
429         BackupPC/PoolWrite.pm
430         BackupPC/View.pm
431         BackupPC/Xfer/Archive.pm
432         BackupPC/Xfer/Tar.pm
433         BackupPC/Xfer/Smb.pm
434         BackupPC/Xfer/Rsync.pm
435         BackupPC/Xfer/RsyncDigest.pm
436         BackupPC/Xfer/RsyncFileIO.pm
437         BackupPC/Zip/FileMember.pm
438         BackupPC/Lang/en.pm
439         BackupPC/Lang/fr.pm
440         BackupPC/Lang/es.pm
441         BackupPC/Lang/de.pm
442         BackupPC/Lang/it.pm
443         BackupPC/CGI/AdminOptions.pm
444         BackupPC/CGI/Archive.pm
445         BackupPC/CGI/ArchiveInfo.pm
446         BackupPC/CGI/Browse.pm
447         BackupPC/CGI/DirHistory.pm
448         BackupPC/CGI/EmailSummary.pm
449         BackupPC/CGI/GeneralInfo.pm
450         BackupPC/CGI/HostInfo.pm
451         BackupPC/CGI/Lib.pm
452         BackupPC/CGI/LOGlist.pm
453         BackupPC/CGI/Queue.pm
454         BackupPC/CGI/ReloadServer.pm
455         BackupPC/CGI/RestoreFile.pm
456         BackupPC/CGI/RestoreInfo.pm
457         BackupPC/CGI/Restore.pm
458         BackupPC/CGI/StartServer.pm
459         BackupPC/CGI/StartStopBackup.pm
460         BackupPC/CGI/StopServer.pm
461         BackupPC/CGI/Summary.pm
462         BackupPC/CGI/View.pm
463     ) ) {
464     InstallFile("lib/$lib", "$Conf{InstallDir}/lib/$lib", 0444);
465 }
466
467 if ( $Conf{CgiImageDir} ne "" ) {
468     printf("Installing images in $Conf{CgiImageDir}\n");
469     foreach my $img ( <images/*> ) {
470         (my $destImg = $img) =~ s{^images/}{};
471         InstallFile($img, "$Conf{CgiImageDir}/$destImg", 0444, 1);
472     }
473
474     #
475     # Install new CSS file, making a backup copy if necessary
476     #
477     my $cssBackup = "$Conf{CgiImageDir}/BackupPC_stnd.css.pre-__VERSION__";
478     if ( -f "$Conf{CgiImageDir}/BackupPC_stnd.css" && !-f $cssBackup ) {
479         rename("$Conf{CgiImageDir}/BackupPC_stnd.css", $cssBackup);
480     }
481     InstallFile("conf/BackupPC_stnd.css",
482                 "$Conf{CgiImageDir}/BackupPC_stnd.css", 0444, 0);
483 }
484
485 printf("Making init.d scripts\n");
486 foreach my $init ( qw(gentoo-backuppc gentoo-backuppc.conf linux-backuppc
487                       solaris-backuppc debian-backuppc suse-backuppc) ) {
488     InstallFile("init.d/src/$init", "init.d/$init", 0444);
489 }
490
491 printf("Installing docs in $Conf{InstallDir}/doc\n");
492 foreach my $doc ( qw(BackupPC.pod BackupPC.html) ) {
493     InstallFile("doc/$doc", "$Conf{InstallDir}/doc/$doc", 0444);
494 }
495
496 printf("Installing config.pl and hosts in $Conf{TopDir}/conf\n");
497 InstallFile("conf/hosts", "$Conf{TopDir}/conf/hosts", 0644)
498                     if ( !-f "$Conf{TopDir}/conf/hosts" );
499
500 #
501 # Now do the config file.  If there is an existing config file we
502 # merge in the new config file, adding any new configuration
503 # parameters and deleting ones that are no longer needed.
504 #
505 my $dest = "$Conf{TopDir}/conf/config.pl";
506 my ($newConf, $newVars) = ConfigParse("conf/config.pl");
507 my ($oldConf, $oldVars);
508 if ( -f $dest ) {
509     ($oldConf, $oldVars) = ConfigParse($dest);
510     $newConf = ConfigMerge($oldConf, $oldVars, $newConf, $newVars);
511 }
512 $Conf{EMailFromUserName}  ||= $Conf{BackupPCUser};
513 $Conf{EMailAdminUserName} ||= $Conf{BackupPCUser};
514
515 #
516 # Update various config parameters
517 #
518
519 #
520 # Guess $Conf{CgiURL}
521 #
522 if ( !defined($Conf{CgiURL}) ) {
523     if ( $Conf{CgiDir} =~ m{cgi-bin(/.*)} ) {
524         $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin$1/BackupPC_Admin'";
525     } else {
526         $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin/BackupPC_Admin'";
527     }
528 }
529
530 #
531 # The smbclient commands have moved from hard-coded to the config file.
532 # $Conf{SmbClientArgs} no longer exists, so merge it into the new
533 # commands if it still exists.
534 #
535 if ( defined($Conf{SmbClientArgs}) ) {
536     if ( $Conf{SmbClientArgs} ne "" ) {
537         foreach my $param ( qw(SmbClientRestoreCmd SmbClientFullCmd
538                                 SmbClientIncrCmd) ) {
539             $newConf->[$newVars->{$param}]{text}
540                             =~ s/(-E\s+-N)/$1 $Conf{SmbClientArgs}/;
541         }
542     }
543     delete($Conf{SmbClientArgs});
544 }
545
546 #
547 # CSS is now stored in a file rather than a big config variable.
548 #
549 delete($Conf{CSSstylesheet});
550
551 #
552 # The blackout timing settings are now stored in a list of hashes, rather
553 # than three scalar parameters.
554 #
555 if ( defined($Conf{BlackoutHourBegin}) ) {
556     $Conf{BlackoutPeriods} = [
557          {
558              hourBegin => $Conf{BlackoutHourBegin},
559              hourEnd   => $Conf{BlackoutHourEnd},
560              weekDays  => $Conf{BlackoutWeekDays},
561          } 
562     ];
563     delete($Conf{BlackoutHourBegin});
564     delete($Conf{BlackoutHourEnd});
565     delete($Conf{BlackoutWeekDays});
566 }
567
568 #
569 # $Conf{RsyncLogLevel} has been replaced by $Conf{XferLogLevel}
570 #
571 if ( defined($Conf{RsyncLogLevel}) ) {
572     $Conf{XferLogLevel} = $Conf{RsyncLogLevel};
573     delete($Conf{RsyncLogLevel});
574 }
575
576 #
577 # In 2.1.0 the default for $Conf{CgiNavBarAdminAllHosts} is now 1
578 #
579 $Conf{CgiNavBarAdminAllHosts} = 1;
580
581 #
582 # IncrFill should now be off
583 #
584 $Conf{IncrFill} = 0;
585
586 #
587 # Figure out sensible arguments for the ping command
588 #
589 if ( defined($Conf{PingArgs}) ) {
590     $Conf{PingCmd} = '$pingPath ' . $Conf{PingArgs};
591 } elsif ( !defined($Conf{PingCmd}) ) {
592     if ( $^O eq "solaris" || $^O eq "sunos" ) {
593         $Conf{PingCmd} = '$pingPath -s $host 56 1';
594     } elsif ( ($^O eq "linux" || $^O eq "openbsd" || $^O eq "netbsd")
595             && !system("$Conf{PingPath} -c 1 -w 3 localhost") ) {
596         $Conf{PingCmd} = '$pingPath -c 1 -w 3 $host';
597     } else {
598         $Conf{PingCmd} = '$pingPath -c 1 $host';
599     }
600     delete($Conf{PingArgs});
601 }
602
603 #
604 # Figure out sensible arguments for the df command
605 #
606 if ( !defined($Conf{DfCmd}) ) {
607     if ( $^O eq "solaris" || $^O eq "sunos" ) {
608         $Conf{DfCmd} = '$dfPath -k $topDir';
609     }
610 }
611
612 #
613 # $Conf{SmbClientTimeout} is now $Conf{ClientTimeout}
614 #
615 if ( defined($Conf{SmbClientTimeout}) ) {
616     $Conf{ClientTimeout} = $Conf{SmbClientTimeout};
617     delete($Conf{SmbClientTimeout});
618 }
619
620 my $confCopy = "$dest.pre-__VERSION__";
621 if ( -f $dest && !-f $confCopy ) {
622     #
623     # Make copy of config file, preserving ownership and modes
624     #
625     printf("Making backup copy of $dest -> $confCopy\n");
626     my @stat = stat($dest);
627     my $mode = $stat[2];
628     my $uid  = $stat[4];
629     my $gid  = $stat[5];
630     die("can't copy($dest, $confCopy)\n")  unless copy($dest, $confCopy);
631     die("can't chown $uid, $gid $confCopy\n")
632                                            unless chown($uid, $gid, $confCopy);
633     die("can't chmod $mode $confCopy\n")   unless chmod($mode, $confCopy);
634 }
635 open(OUT, ">", $dest) || die("can't open $dest for writing\n");
636 binmode(OUT);
637 my $blockComment;
638 foreach my $var ( @$newConf ) {
639     if ( length($blockComment)
640           && substr($var->{text}, 0, length($blockComment)) eq $blockComment ) {
641         $var->{text} = substr($var->{text}, length($blockComment));
642         $blockComment = undef;
643     }
644     $blockComment = $1 if ( $var->{text} =~ /^([\s\n]*#{70}.*#{70}[\s\n]+)/s );
645     $var->{text} =~ s/^\s*\$Conf\{(.*?)\}(\s*=\s*['"]?)(.*?)(['"]?\s*;)/
646                 defined($Conf{$1}) && ref($Conf{$1}) eq ""
647                                    && $Conf{$1} ne $OrigConf{$1}
648                                    ? "\$Conf{$1}$2$Conf{$1}$4"
649                                    : "\$Conf{$1}$2$3$4"/emg;
650     print OUT $var->{text};
651 }
652 close(OUT);
653 if ( !defined($oldConf) ) {
654     die("can't chmod 0640 mode $dest\n")  unless chmod(0640, $dest);
655     die("can't chown $Uid, $Gid $dest\n") unless chown($Uid, $Gid, $dest);
656 }
657
658 if ( $Conf{CgiDir} ne "" ) {
659     printf("Installing cgi script BackupPC_Admin in $Conf{CgiDir}\n");
660     mkpath("$Conf{CgiDir}", 0, 0755);
661     InstallFile("cgi-bin/BackupPC_Admin", "$Conf{CgiDir}/BackupPC_Admin",
662                 04554);
663 }
664
665 print <<EOF;
666
667 Ok, it looks like we are finished.  There are several more things you
668 will need to do:
669
670   - Browse through the config file, $Conf{TopDir}/conf/config.pl,
671     and make sure all the settings are correct.  In particular, you
672     will need to set the smb share password and user name, backup
673     policies and check the email message headers and bodies.
674
675   - Edit the list of hosts to backup in $Conf{TopDir}/conf/hosts.
676
677   - Read the documentation in $Conf{InstallDir}/doc/BackupPC.html.
678     Please pay special attention to the security section.
679
680   - Verify that the CGI script BackupPC_Admin runs correctly.  You might
681     need to change the permissions or group ownership of BackupPC_Admin.
682
683   - BackupPC should be ready to start.  Don't forget to run it
684     as user $Conf{BackupPCUser}!  The installation also contains an
685     init.d/backuppc script that can be copied to /etc/init.d
686     so that BackupPC can auto-start on boot.  This will also enable
687     administrative users to start the server from the CGI interface.
688     See init.d/README.
689
690 Enjoy!
691 EOF
692
693 if ( `$Conf{PerlPath} -V` =~ /uselargefiles=undef/ ) {
694     print <<EOF;
695
696 Warning: your perl, $Conf{PerlPath}, does not support large files.
697 This means BackupPC won't be able to backup files larger than 2GB.
698 To solve this problem you should build/install a new version of perl
699 with large file support enabled.  Use
700
701     $Conf{PerlPath} -V | egrep uselargefiles
702
703 to check if perl has large file support (undef means no support).
704 EOF
705 }
706
707 eval "use File::RsyncP;";
708 if ( !$@ && $File::RsyncP::VERSION < 0.50 ) {
709     print("\nWarning: you need to upgrade File::RsyncP;"
710         . " I found $File::RsyncP::VERSION and BackupPC needs 0.50\n");
711 }
712
713 exit(0);
714
715 ###########################################################################
716 # Subroutines
717 ###########################################################################
718
719 sub InstallFile
720 {
721     my($prog, $dest, $mode, $binary) = @_;
722     my $first = 1;
723     my($uid, $gid) = ($Uid, $Gid);
724
725     if ( -f $dest ) {
726         #
727         # preserve ownership and modes of files that already exist
728         #
729         my @stat = stat($dest);
730         $mode = $stat[2];
731         $uid  = $stat[4];
732         $gid  = $stat[5];
733     }
734     unlink($dest) if ( -f $dest );
735     if ( $binary ) {
736         die("can't copy($prog, $dest)\n") unless copy($prog, $dest);
737     } else {
738         open(PROG, $prog)     || die("can't open $prog for reading\n");
739         open(OUT, ">", $dest) || die("can't open $dest for writing\n");
740         binmode(PROG);
741         binmode(OUT);
742         while ( <PROG> ) {
743             s/__INSTALLDIR__/$Conf{InstallDir}/g;
744             s/__TOPDIR__/$Conf{TopDir}/g;
745             s/__BACKUPPCUSER__/$Conf{BackupPCUser}/g;
746             s/__CGIDIR__/$Conf{CgiDir}/g;
747             if ( $first && /^#.*bin\/perl/ ) {
748                 #
749                 # Fill in correct path to perl (no taint for >= 2.0.1).
750                 #
751                 print OUT "#!$Conf{PerlPath}\n";
752             } else {
753                 print OUT;
754             }
755             $first = 0;
756         }
757         close(PROG);
758         close(OUT);
759     }
760     die("can't chown $uid, $gid $dest") unless chown($uid, $gid, $dest);
761     die("can't chmod $mode $dest")      unless chmod($mode, $dest);
762 }
763
764 sub FindProgram
765 {
766     my($path, $prog) = @_;
767     foreach my $dir ( split(/:/, $path) ) {
768         my $file = File::Spec->catfile($dir, $prog);
769         return $file if ( -x $file );
770     }
771 }
772
773 sub ConfigParse
774 {
775     my($file) = @_;
776     open(C, $file) || die("can't open $file");
777     binmode(C);
778     my($out, @conf, $var);
779     my $comment = 1;
780     my $allVars = {};
781     my $endLine = undef;
782     while ( <C> ) {
783         if ( /^#/ && !defined($endLine) ) {
784             if ( $comment ) {
785                 $out .= $_;
786             } else {
787                 if ( $out ne "" ) {
788                     $allVars->{$var} = @conf if ( defined($var) );
789                     push(@conf, {
790                         text => $out,
791                         var => $var,
792                     });
793                 }
794                 $var = undef;
795                 $comment = 1;
796                 $out = $_;
797             }
798         } elsif ( /^\s*\$Conf\{([^}]*)/ ) {
799             $comment = 0;
800             if ( defined($var) ) {
801                 $allVars->{$var} = @conf if ( defined($var) );
802                 push(@conf, {
803                     text => $out,
804                     var => $var,
805                 });
806                 $out = $_;
807             } else {
808                 $out .= $_;
809             }
810             $var = $1;
811             $endLine = $1 if ( /^\s*\$Conf\{[^}]*} *= *<<(.*);/ );
812             $endLine = $1 if ( /^\s*\$Conf\{[^}]*} *= *<<'(.*)';/ );
813         } else {
814             $endLine = undef if ( defined($endLine) && /^\Q$endLine[\n\r]*$/ );
815             $out .= $_;
816         }
817     }
818     if ( $out ne "" ) {
819         $allVars->{$var} = @conf if ( defined($var) );
820         push(@conf, {
821             text => $out,
822             var  => $var,
823         });
824     }
825     close(C);
826     return (\@conf, $allVars);
827 }
828
829 sub ConfigMerge
830 {
831     my($old, $oldVars, $new, $newVars) = @_;
832     my $posn = 0;
833     my $res;
834
835     #
836     # Find which config parameters are not needed any longer
837     #
838     foreach my $var ( @$old ) {
839         next if ( !defined($var->{var}) || defined($newVars->{$var->{var}}) );
840         #print(STDERR "Deleting old config parameter $var->{var}\n");
841         $var->{delete} = 1;
842     }
843     #
844     # Find which config parameters are new
845     #
846     foreach my $var ( @$new ) {
847         next if ( !defined($var->{var}) );
848         if ( defined($oldVars->{$var->{var}}) ) {
849             $posn = $oldVars->{$var->{var}};
850         } else {
851             #print(STDERR "New config parameter $var->{var}: $var->{text}\n");
852             push(@{$old->[$posn]{new}}, $var);
853         }
854     }
855     #
856     # Create merged config file
857     #
858     foreach my $var ( @$old ) {
859         next if ( $var->{delete} );
860         push(@$res, $var);
861         foreach my $new ( @{$var->{new}} ) {
862             push(@$res, $new);
863         }
864     }
865     return $res;
866 }