- 2.0.0 release. Minor tweaks to disable utf8.
[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-2003  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 );
135
136 foreach my $prog ( sort(keys(%Programs)) ) {
137     my $path;
138     foreach my $subProg ( split(/\//, $prog) ) {
139         $path ||= FindProgram("$ENV{PATH}:/bin:/usr/bin:/sbin:/usr/sbin",
140                               $subProg);
141     }
142     $Conf{$Programs{$prog}} ||= $path;
143 }
144
145 while ( 1 ) {
146     print <<EOF;
147
148 I found the following locations for these programs:
149
150 EOF
151     foreach my $prog ( sort(keys(%Programs)) ) {
152         printf("    %-11s => %s\n", $prog, $Conf{$Programs{$prog}});
153     }
154     print "\n";
155     last if (prompt('--> Are these paths correct?', 'y') =~ /^y/i);
156     foreach my $prog ( sort(keys(%Programs)) ) {
157         $Conf{$Programs{$prog}} = prompt("--> $prog path",
158                                          $Conf{$Programs{$prog}});
159     }
160 }
161
162 my $Perl56 = system($Conf{PerlPath}
163                         . q{ -e 'exit($^V && $^V ge v5.6.0 ? 1 : 0);'});
164
165 if ( !$Perl56 ) {
166     print <<EOF;
167
168 BackupPC needs perl version 5.6.0 or later.  $Conf{PerlPath} appears
169 to be an older version.  Please upgrade to a newer version of perl
170 and re-run this configure script.
171
172 EOF
173     exit(1);
174 }
175
176 print <<EOF;
177
178 Please tell me the hostname of the machine that BackupPC will run on.
179
180 EOF
181 chomp($Conf{ServerHost} = `$Conf{HostnamePath}`)
182         if ( defined($Conf{HostnamePath}) && !defined($Conf{ServerHost}) );
183 $Conf{ServerHost} = prompt("--> BackupPC will run on host", $Conf{ServerHost});
184
185 print <<EOF;
186
187 BackupPC should run as a dedicated user with limited privileges.  You
188 need to create a user.  This user will need read/write permission on
189 the main data directory and read/execute permission on the install
190 directory (these directories will be setup shortly).
191
192 The primary group for this user should also be chosen carefully.
193 By default the install directories will have group write permission.
194 The data directories and files will have group read permission but
195 no other permission.
196
197 EOF
198 my($name, $passwd, $Uid, $Gid);
199 while ( 1 ) {
200     $Conf{BackupPCUser} = prompt("--> BackupPC should run as user",
201                                  $Conf{BackupPCUser} || "backuppc");
202     ($name, $passwd, $Uid, $Gid) = getpwnam($Conf{BackupPCUser});
203     last if ( $name ne "" );
204     print <<EOF;
205
206 getpwnam() says that user $Conf{BackupPCUser} doesn't exist.  Please check the
207 name and verify that this user is in the passwd file.
208
209 EOF
210 }
211
212 print <<EOF;
213
214 Please specify an install directory for BackupPC.  This is where the
215 BackupPC scripts, library and documentation will be installed.
216
217 EOF
218
219 while ( 1 ) {
220     $Conf{InstallDir} = prompt("--> Install directory (full path)",
221                                $Conf{InstallDir});
222     last if ( $Conf{InstallDir} =~ /^\// );
223 }
224
225 print <<EOF;
226
227 Please specify a data directory for BackupPC.  This is where the
228 configuration files, LOG files and all the PC backups are stored.
229 This file system needs to be big enough to accommodate all the
230 PCs you expect to backup (eg: at least 1-2GB per machine).
231
232 EOF
233
234 while ( 1 ) {
235     $Conf{TopDir} = prompt("--> Data directory (full path)", $Conf{TopDir});
236     last if ( $Conf{TopDir} =~ /^\// );
237 }
238
239 if ( !defined($Conf{CompressLevel}) ) {
240     $Conf{CompressLevel} = BackupPC::FileZIO->compOk ? 3 : 0;
241     if ( $ConfigPath eq "" && $Conf{CompressLevel} ) {
242         print <<EOF;
243
244 BackupPC can compress pool files, providing around a 40% reduction in pool
245 size (your mileage may vary). Specify the compression level (0 turns
246 off compression, and 1 to 9 represent good/fastest to best/slowest).
247 The recommended values are 0 (off) or 3 (reasonable compression and speed).
248 Increasing the compression level to 5 will use around 20% more cpu time
249 and give perhaps 2-3% more compression.
250
251 EOF
252     } elsif ( $ConfigPath eq "" ) {
253         print <<EOF;
254
255 BackupPC can compress pool files, but it needs the Compress::Zlib
256 package installed (see www.cpan.org). Compression will provide around a
257 40% reduction in pool size, at the expense of cpu time.  You can leave
258 compression off and run BackupPC without compression, in which case you
259 should leave the compression level at 0 (which means off).  You could
260 install Compress::Zlib and turn compression on later, but read the
261 documentation first about how to do this.  Or the better choice is
262 to quit, install Compress::Zlib, and re-run configure.pl.
263
264 EOF
265     } elsif ( $Conf{CompressLevel} ) {
266         $Conf{CompressLevel} = 0;
267         print <<EOF;
268
269 BackupPC now supports pool file compression.  Since you are upgrading
270 BackupPC you probably have existing uncompressed backups.  You have
271 several choices if you want to turn on compression.  You can run
272 the script BackupPC_compressPool to convert everything to compressed
273 form.  Or you can simply turn on compression, so that new backups
274 will be compressed.  This will increase the pool storage requirement,
275 since both uncompressed and compressed copies of files will be stored.
276 But eventually the old uncompressed backups will expire, recovering
277 the pool storage.  Please see the documentation for more details.
278
279 If you are not sure what to do, leave the Compression Level at 0,
280 which disables compression.  You can always read the documentation
281 and turn it on later.
282
283 EOF
284     } else {
285         $Conf{CompressLevel} = 0;
286         print <<EOF;
287
288 BackupPC now supports pool file compression, but it needs the
289 Compress::Zlib module (see www.cpan.org).  For now, leave
290 the compression level set at 0 to disable compression.  If you
291 want you can install Compress::Zlib and turn compression on.
292 Please see the documentation for more details about converting
293 old backups to compressed form.
294
295 EOF
296     }
297     while ( 1 ) {
298         $Conf{CompressLevel}
299                     = prompt("--> Compression level", $Conf{CompressLevel});
300         last if ( $Conf{CompressLevel} =~ /^\d+$/ );
301     }
302 }
303
304 print <<EOF;
305
306 BackupPC has a powerful CGI perl interface that runs under Apache.
307 A single executable needs to be installed in a cgi-bin directory.
308 This executable needs to run as set-uid $Conf{BackupPCUser}, or
309 it can be run under mod_perl with Apache running as user $Conf{BackupPCUser}.
310
311 Leave this path empty if you don't want to install the CGI interface.
312
313 EOF
314
315 while ( 1 ) {
316     $Conf{CgiDir} = prompt("--> CGI bin directory (full path)", $Conf{CgiDir});
317     last if ( $Conf{CgiDir} =~ /^\// || $Conf{CgiDir} eq "" );
318 }
319
320 if ( $Conf{CgiDir} ne "" ) {
321
322     print <<EOF;
323
324 BackupPC's CGI script needs to display various GIF images that
325 should be stored where Apache can serve them.  They should be
326 placed somewher under Apache's DocumentRoot.  BackupPC also
327 needs to know the URL to access these images.  Example:
328
329     Apache image directory:  /usr/local/apache/htdocs/BackupPC
330     URL for image directory: /BackupPC
331
332 The URL for the image directory should start with a slash.
333
334 EOF
335     while ( 1 ) {
336         $Conf{CgiImageDir} = prompt("--> Apache image directory (full path)",
337                                         $Conf{CgiImageDir});
338         last if ( $Conf{CgiImageDir} =~ /^\// );
339     }
340     while ( 1 ) {
341         $Conf{CgiImageDirURL} = prompt("--> URL for image directory (omit http://host; starts with '/')",
342                                         $Conf{CgiImageDirURL});
343         last if ( $Conf{CgiImageDirURL} =~ /^\// );
344     }
345 }
346
347 print <<EOF;
348
349 Ok, we're about to:
350
351   - install the binaries, lib and docs in $Conf{InstallDir},
352   - create the data directory $Conf{TopDir},
353   - create/update the config.pl file $Conf{TopDir}/conf,
354   - optionally install the cgi-bin interface.
355
356 EOF
357
358 exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
359
360 #
361 # Create install directories
362 #
363 foreach my $dir ( qw(bin lib/BackupPC/Xfer lib/BackupPC/Zip
364                      lib/BackupPC/Lang doc) ) {
365     next if ( -d "$Conf{InstallDir}/$dir" );
366     mkpath("$Conf{InstallDir}/$dir", 0, 0775);
367     if ( !-d "$Conf{InstallDir}/$dir"
368             || !chown($Uid, $Gid, "$Conf{InstallDir}/$dir") ) {
369         die("Failed to create or chown $Conf{InstallDir}/$dir\n");
370     } else {
371         print("Created $Conf{InstallDir}/$dir\n");
372     }
373 }
374
375 #
376 # Create CGI image directory
377 #
378 foreach my $dir ( ($Conf{CgiImageDir}) ) {
379     next if ( $dir eq "" || -d $dir );
380     mkpath($dir, 0, 0775);
381     if ( !-d $dir || !chown($Uid, $Gid, $dir) ) {
382         die("Failed to create or chown $dir");
383     } else {
384         print("Created $dir\n");
385     }
386 }
387
388 #
389 # Create $TopDir's top-level directories
390 #
391 foreach my $dir ( qw(. conf pool cpool pc trash log) ) {
392     mkpath("$Conf{TopDir}/$dir", 0, 0750) if ( !-d "$Conf{TopDir}/$dir" );
393     if ( !-d "$Conf{TopDir}/$dir"
394             || !chown($Uid, $Gid, "$Conf{TopDir}/$dir") ) {
395         die("Failed to create or chown $Conf{TopDir}/$dir\n");
396     } else {
397         print("Created $Conf{TopDir}/$dir\n");
398     }
399 }
400
401 printf("Installing binaries in $Conf{InstallDir}/bin\n");
402 foreach my $prog ( qw(BackupPC BackupPC_dump BackupPC_link BackupPC_nightly
403         BackupPC_sendEmail BackupPC_tarCreate BackupPC_trashClean
404         BackupPC_tarExtract BackupPC_compressPool BackupPC_zcat
405         BackupPC_restore BackupPC_serverMesg BackupPC_zipCreate ) ) {
406     InstallFile("bin/$prog", "$Conf{InstallDir}/bin/$prog", 0555);
407 }
408
409 #
410 # Remove unused binaries from older versions
411 #
412 unlink("$Conf{InstallDir}/bin/BackupPC_queueAll");
413
414 printf("Installing library in $Conf{InstallDir}/lib\n");
415 foreach my $lib ( qw(BackupPC/Lib.pm BackupPC/FileZIO.pm BackupPC/Attrib.pm
416         BackupPC/PoolWrite.pm BackupPC/View.pm BackupPC/Xfer/Tar.pm
417         BackupPC/Xfer/Smb.pm BackupPC/Xfer/Rsync.pm
418         BackupPC/Xfer/RsyncFileIO.pm BackupPC/Zip/FileMember.pm
419         BackupPC/Lang/en.pm BackupPC/Lang/fr.pm BackupPC/Lang/es.pm
420         BackupPC/Lang/de.pm
421     ) ) {
422     InstallFile("lib/$lib", "$Conf{InstallDir}/lib/$lib", 0444);
423 }
424
425 if ( $Conf{CgiImageDir} ne "" ) {
426     printf("Installing images in $Conf{CgiImageDir}\n");
427     foreach my $img ( <images/*> ) {
428         (my $destImg = $img) =~ s{^images/}{};
429         InstallFile($img, "$Conf{CgiImageDir}/$destImg", 0444, 1);
430     }
431 }
432
433 printf("Making init.d scripts\n");
434 foreach my $init ( qw(gentoo-backuppc gentoo-backuppc.conf linux-backuppc
435                       solaris-backuppc debian-backuppc suse-backuppc) ) {
436     InstallFile("init.d/src/$init", "init.d/$init", 0444);
437 }
438
439 printf("Installing docs in $Conf{InstallDir}/doc\n");
440 foreach my $doc ( qw(BackupPC.pod BackupPC.html) ) {
441     InstallFile("doc/$doc", "$Conf{InstallDir}/doc/$doc", 0444);
442 }
443
444 printf("Installing config.pl and hosts in $Conf{TopDir}/conf\n");
445 InstallFile("conf/hosts", "$Conf{TopDir}/conf/hosts", 0644)
446                     if ( !-f "$Conf{TopDir}/conf/hosts" );
447
448 #
449 # Now do the config file.  If there is an existing config file we
450 # merge in the new config file, adding any new configuration
451 # parameters and deleting ones that are no longer needed.
452 #
453 my $dest = "$Conf{TopDir}/conf/config.pl";
454 my ($newConf, $newVars) = ConfigParse("conf/config.pl");
455 my ($oldConf, $oldVars);
456 if ( -f $dest ) {
457     ($oldConf, $oldVars) = ConfigParse($dest);
458     $newConf = ConfigMerge($oldConf, $oldVars, $newConf, $newVars);
459 }
460 $Conf{EMailFromUserName}  ||= $Conf{BackupPCUser};
461 $Conf{EMailAdminUserName} ||= $Conf{BackupPCUser};
462
463 #
464 # Update various config parameters
465 #
466
467 #
468 # Guess $Conf{CgiURL}
469 #
470 if ( !defined($Conf{CgiURL}) ) {
471     if ( $Conf{CgiDir} =~ m{cgi-bin(/.*)} ) {
472         $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin$1/BackupPC_Admin'";
473     } else {
474         $Conf{CgiURL} = "'http://$Conf{ServerHost}/cgi-bin/BackupPC_Admin'";
475     }
476 }
477
478 #
479 # The smbclient commands have moved from hard-coded to the config file.
480 # $Conf{SmbClientArgs} no longer exists, so merge it into the new
481 # commands if it still exists.
482 #
483 if ( defined($Conf{SmbClientArgs}) ) {
484     if ( $Conf{SmbClientArgs} ne "" ) {
485         foreach my $param ( qw(SmbClientRestoreCmd SmbClientFullCmd
486                                 SmbClientIncrCmd) ) {
487             $newConf->[$newVars->{$param}]{text}
488                             =~ s/(-E\s+-N)/$1 $Conf{SmbClientArgs}/;
489         }
490     }
491     delete($Conf{SmbClientArgs});
492 }
493
494 #
495 # IncrFill should now be off
496 #
497 $Conf{IncrFill} = 0;
498
499 #
500 # Figure out sensible arguments for the ping command
501 #
502 if ( defined($Conf{PingArgs}) ) {
503     $Conf{PingCmd} = '$pingPath ' . $Conf{PingArgs};
504 } elsif ( !defined($Conf{PingCmd}) ) {
505     if ( $^O eq "solaris" || $^O eq "sunos" ) {
506         $Conf{PingCmd} = '$pingPath -s $host 56 1';
507     } elsif ( ($^O eq "linux" || $^O eq "openbsd" || $^O eq "netbsd")
508             && !system("$Conf{PingClientPath} -c 1 -w 3 localhost") ) {
509         $Conf{PingCmd} = '$pingPath -c 1 -w 3 $host';
510     } else {
511         $Conf{PingCmd} = '$pingPath -c 1 $host';
512     }
513     delete($Conf{PingArgs});
514 }
515
516 #
517 # Figure out sensible arguments for the df command
518 #
519 if ( !defined($Conf{DfCmd}) ) {
520     if ( $^O eq "solaris" || $^O eq "sunos" ) {
521         $Conf{DfCmd} = '$dfPath -k $topDir';
522     }
523 }
524
525 #
526 # $Conf{SmbClientTimeout} is now $Conf{ClientTimeout}
527 #
528 if ( defined($Conf{SmbClientTimeout}) ) {
529     $Conf{ClientTimeout} = $Conf{SmbClientTimeout};
530     delete($Conf{SmbClientTimeout});
531 }
532
533 my $confCopy = "$dest.pre-__VERSION__";
534 if ( -f $dest && !-f $confCopy ) {
535     #
536     # Make copy of config file, preserving ownership and modes
537     #
538     printf("Making backup copy of $dest -> $confCopy\n");
539     my @stat = stat($dest);
540     my $mode = $stat[2];
541     my $uid  = $stat[4];
542     my $gid  = $stat[5];
543     die("can't copy($dest, $confCopy)\n")  unless copy($dest, $confCopy);
544     die("can't chown $uid, $gid $confCopy\n")
545                                            unless chown($uid, $gid, $confCopy);
546     die("can't chmod $mode $confCopy\n")   unless chmod($mode, $confCopy);
547 }
548 open(OUT, ">", $dest) || die("can't open $dest for writing\n");
549 binmode(OUT);
550 my $blockComment;
551 foreach my $var ( @$newConf ) {
552     if ( length($blockComment)
553           && substr($var->{text}, 0, length($blockComment)) eq $blockComment ) {
554         $var->{text} = substr($var->{text}, length($blockComment));
555         $blockComment = undef;
556     }
557     $blockComment = $1 if ( $var->{text} =~ /^([\s\n]*#{70}.*#{70}[\s\n]+)/s );
558     $var->{text} =~ s/^\s*\$Conf\{(.*?)\}(\s*=\s*['"]?)(.*?)(['"]?\s*;)/
559                 defined($Conf{$1}) && ref($Conf{$1}) eq ""
560                                    && $Conf{$1} ne $OrigConf{$1}
561                                    ? "\$Conf{$1}$2$Conf{$1}$4"
562                                    : "\$Conf{$1}$2$3$4"/emg;
563     print OUT $var->{text};
564 }
565 close(OUT);
566 if ( !defined($oldConf) ) {
567     die("can't chmod 0640 mode $dest\n")  unless chmod(0640, $dest);
568     die("can't chown $Uid, $Gid $dest\n") unless chown($Uid, $Gid, $dest);
569 }
570
571 if ( $Conf{CgiDir} ne "" ) {
572     printf("Installing cgi script BackupPC_Admin in $Conf{CgiDir}\n");
573     mkpath("$Conf{CgiDir}", 0, 0755);
574     InstallFile("cgi-bin/BackupPC_Admin", "$Conf{CgiDir}/BackupPC_Admin",
575                 04554);
576 }
577
578 print <<EOF;
579
580 Ok, it looks like we are finished.  There are several more things you
581 will need to do:
582
583   - Browse through the config file, $Conf{TopDir}/conf/config.pl,
584     and make sure all the settings are correct.  In particular, you
585     will need to set the smb share password and user name, backup
586     policies and check the email message headers and bodies.
587
588   - Edit the list of hosts to backup in $Conf{TopDir}/conf/hosts.
589
590   - Read the documentation in $Conf{InstallDir}/doc/BackupPC.html.
591     Please pay special attention to the security section.
592
593   - Verify that the CGI script BackupPC_Admin runs correctly.  You might
594     need to change the permissions or group ownership of BackupPC_Admin.
595
596   - BackupPC should be ready to start.  Don't forget to run it
597     as user $Conf{BackupPCUser}!  The installation also contains an
598     init.d/backuppc script that can be copied to /etc/init.d
599     so that BackupPC can auto-start on boot.  See init.d/README.
600
601 Enjoy!
602 EOF
603
604 if ( $ENV{LANG} =~ /utf/i && $^V ge v5.8.0 ) {
605     print <<EOF;
606
607 WARNING: Your LANG environment variable is set to $ENV{LANG}, which
608 doesn't behave well with this version of perl.  Please set the
609 LANG environment variable to en_US before running BackupPC.
610
611 On RH-8 this setting is in the file /etc/sysconfig/i18n, or you
612 could set it in BackupPC's init.d script.
613 EOF
614 }
615
616 exit(0);
617
618 ###########################################################################
619 # Subroutines
620 ###########################################################################
621
622 sub InstallFile
623 {
624     my($prog, $dest, $mode, $binary) = @_;
625     my $first = 1;
626     my($uid, $gid) = ($Uid, $Gid);
627
628     if ( -f $dest ) {
629         #
630         # preserve ownership and modes of files that already exist
631         #
632         my @stat = stat($dest);
633         $mode = $stat[2];
634         $uid  = $stat[4];
635         $gid  = $stat[5];
636     }
637     unlink($dest) if ( -f $dest );
638     if ( $binary ) {
639         die("can't copy($prog, $dest)\n") unless copy($prog, $dest);
640     } else {
641         open(PROG, $prog)     || die("can't open $prog for reading\n");
642         open(OUT, ">", $dest) || die("can't open $dest for writing\n");
643         binmode(PROG);
644         binmode(OUT);
645         while ( <PROG> ) {
646             s/__INSTALLDIR__/$Conf{InstallDir}/g;
647             s/__TOPDIR__/$Conf{TopDir}/g;
648             s/__BACKUPPCUSER__/$Conf{BackupPCUser}/g;
649             s/__CGIDIR__/$Conf{CgiDir}/g;
650             if ( $first && /^#.*bin\/perl/ ) {
651                 if ( $Perl56 ) {
652                     #
653                     # perl56 and later is taint ok
654                     #
655                     print OUT "#!$Conf{PerlPath} -T\n";
656                 } else {
657                     #
658                     # prior to perl56, File::Find fails taint checks,
659                     # so we run without -T.  It's still safe.
660                     #
661                     print OUT "#!$Conf{PerlPath}\n";
662                 }
663             } else {
664                 print OUT;
665             }
666             $first = 0;
667         }
668         close(PROG);
669         close(OUT);
670     }
671     die("can't chown $uid, $gid $dest") unless chown($uid, $gid, $dest);
672     die("can't chmod $mode $dest")      unless chmod($mode, $dest);
673 }
674
675 sub FindProgram
676 {
677     my($path, $prog) = @_;
678     foreach my $dir ( split(/:/, $path) ) {
679         my $file = File::Spec->catfile($dir, $prog);
680         return $file if ( -x $file );
681     }
682 }
683
684 sub ConfigParse
685 {
686     my($file) = @_;
687     open(C, $file) || die("can't open $file");
688     binmode(C);
689     my($out, @conf, $var);
690     my $comment = 1;
691     my $allVars = {};
692     while ( <C> ) {
693         if ( /^#/ ) {
694             if ( $comment ) {
695                 $out .= $_;
696             } else {
697                 if ( $out ne "" ) {
698                     $allVars->{$var} = @conf if ( defined($var) );
699                     push(@conf, {
700                         text => $out,
701                         var => $var,
702                     });
703                 }
704                 $var = undef;
705                 $comment = 1;
706                 $out = $_;
707             }
708         } elsif ( /^\s*\$Conf\{([^}]*)/ ) {
709             $comment = 0;
710             if ( defined($var) ) {
711                 $allVars->{$var} = @conf if ( defined($var) );
712                 push(@conf, {
713                     text => $out,
714                     var => $var,
715                 });
716                 $out = $_;
717             } else {
718                 $out .= $_;
719             }
720             $var = $1;
721         } else {
722             $out .= $_;
723         }
724     }
725     if ( $out ne "" ) {
726         $allVars->{$var} = @conf if ( defined($var) );
727         push(@conf, {
728             text => $out,
729             var  => $var,
730         });
731     }
732     close(C);
733     return (\@conf, $allVars);
734 }
735
736 sub ConfigMerge
737 {
738     my($old, $oldVars, $new, $newVars) = @_;
739     my $posn = 0;
740     my $res;
741
742     #
743     # Find which config parameters are not needed any longer
744     #
745     foreach my $var ( @$old ) {
746         next if ( !defined($var->{var}) || defined($newVars->{$var->{var}}) );
747         #print(STDERR "Deleting old config parameter $var->{var}\n");
748         $var->{delete} = 1;
749     }
750     #
751     # Find which config parameters are new
752     #
753     foreach my $var ( @$new ) {
754         next if ( !defined($var->{var}) );
755         if ( defined($oldVars->{$var->{var}}) ) {
756             $posn = $oldVars->{$var->{var}};
757         } else {
758             #print(STDERR "New config parameter $var->{var}: $var->{text}\n");
759             push(@{$old->[$posn]{new}}, $var);
760         }
761     }
762     #
763     # Create merged config file
764     #
765     foreach my $var ( @$old ) {
766         next if ( $var->{delete} );
767         push(@$res, $var);
768         foreach my $new ( @{$var->{new}} ) {
769             push(@$res, $new);
770         }
771     }
772     return $res;
773 }