Added create of lib/BackupPC/Lang to configure.pl
[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  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 __VERSION__, released __RELEASEDATE__.
37 #
38 # See http://backuppc.sourceforge.net.
39 #
40 #========================================================================
41
42 use strict;
43 use vars qw(%Conf %OrigConf);
44 use lib "./lib";
45
46 my @Packages = qw(ExtUtils::MakeMaker File::Path File::Spec File::Copy
47                   DirHandle Digest::MD5 Data::Dumper Getopt::Std
48                   BackupPC::Lib BackupPC::FileZIO);
49
50 foreach my $pkg ( @Packages ) {
51     eval "use $pkg";
52     next if ( !$@ );
53     die <<EOF;
54
55 BackupPC needs the package $pkg.  Please install $pkg
56 before installing BackupPC.
57
58 EOF
59 }
60
61 if ( $< != 0 ) {
62     print <<EOF;
63
64 This configure script should be run as root, rather than uid $<.
65 Provided uid $< has sufficient permissions to create the data and
66 install directories, then it should be ok to proceed.  Otherwise,
67 please quit and restart as root.
68
69 EOF
70     exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
71 }
72
73 print <<EOF;
74
75 Is this a new installation or upgrade for BackupPC?  If this is
76 an upgrade please tell me the full path of the existing BackupPC
77 configuration file (eg: /xxxx/conf/config.pl).  Otherwise, just
78 hit return.
79
80 EOF
81
82 #
83 # Check if this is an upgrade, in which case read the existing
84 # config file to get all the defaults.
85 #
86 my $ConfigPath = "";
87 while ( 1 ) {
88     $ConfigPath = prompt("--> Full path to existing conf/config.pl",
89                                     $ConfigPath);
90     last if ( $ConfigPath eq ""
91             || ($ConfigPath =~ /^\// && -r $ConfigPath && -w $ConfigPath) );
92     my $problem = "is not an absolute path";
93     $problem = "is no writable"  if ( !-w $ConfigPath );
94     $problem = "is not readable" if ( !-r $ConfigPath );
95     $problem = "doesn't exist"   if ( !-f $ConfigPath );
96     print("The file '$ConfigPath' $problem.\n");
97 }
98 my $bpc;
99 if ( $ConfigPath ne "" && -r $ConfigPath ) {
100     (my $topDir = $ConfigPath) =~ s{/[^/]+/[^/]+$}{};
101     die("BackupPC::Lib->new failed\n")
102             if ( !($bpc = BackupPC::Lib->new($topDir)) );
103     %Conf = $bpc->Conf();
104     %OrigConf = %Conf;
105     $Conf{TopDir} = $topDir;
106     my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}, 1); 
107     if ( $err eq "" ) {
108         print <<EOF;
109
110 BackupPC is running on $Conf{ServerHost}.  You need to stop BackupPC
111 before you can upgrade the code.  Depending upon your installation,
112 you could run "/etc/init.d/backuppc stop".
113
114 EOF
115         exit(1);
116     }
117 }
118
119 #
120 # These are the programs whose paths we need to find
121 #
122 my %Programs = (
123     perl       => "PerlPath",
124     'gtar/tar' => "TarClientPath",
125     smbclient  => "SmbClientPath",
126     nmblookup  => "NmbLookupPath",
127     ping       => "PingPath",
128     df         => "DfPath",
129     'ssh2/ssh' => "SshPath",
130     sendmail   => "SendmailPath",
131     hostname   => "HostnamePath",
132 );
133
134 foreach my $prog ( sort(keys(%Programs)) ) {
135     my $path;
136     foreach my $subProg ( split(/\//, $prog) ) {
137         $path ||= FindProgram("$ENV{PATH}:/bin:/usr/bin:/sbin:/usr/sbin",
138                               $subProg);
139     }
140     $Conf{$Programs{$prog}} ||= $path;
141 }
142
143 while ( 1 ) {
144     print <<EOF;
145
146 I found the following locations for these programs:
147
148 EOF
149     foreach my $prog ( sort(keys(%Programs)) ) {
150         printf("    %-11s => %s\n", $prog, $Conf{$Programs{$prog}});
151     }
152     print "\n";
153     last if (prompt('--> Are these paths correct?', 'y') =~ /^y/i);
154     foreach my $prog ( sort(keys(%Programs)) ) {
155         $Conf{$Programs{$prog}} = prompt("--> $prog path",
156                                          $Conf{$Programs{$prog}});
157     }
158 }
159
160 my $Perl56 = system($Conf{PerlPath}
161                         . q{ -e 'exit($^V && $^V ge v5.6.0 ? 1 : 0);'});
162
163 if ( !$Perl56 ) {
164     print <<EOF;
165
166 BackupPC needs perl version 5.6.0 or later.  $Conf{PerlPath} appears
167 to be an older version.  Please upgrade to a newer version of perl
168 and re-run this configure script.
169
170 EOF
171     exit(1);
172 }
173
174 print <<EOF;
175
176 Please tell me the hostname of the machine that BackupPC will run on.
177
178 EOF
179 chomp($Conf{ServerHost} = `$Conf{HostnamePath}`)
180         if ( defined($Conf{HostnamePath}) && !defined($Conf{ServerHost}) );
181 $Conf{ServerHost} = prompt("--> BackupPC will run on host", $Conf{ServerHost});
182
183 print <<EOF;
184
185 BackupPC should run as a dedicated user with limited privileges.  You
186 need to create a user.  This user will need read/write permission on
187 the main data directory and read/execute permission on the install
188 directory (these directories will be setup shortly).
189
190 The primary group for this user should also be chosen carefully.
191 By default the install directories will have group write permission.
192 The data directories and files will have group read permission but
193 no other permission.
194
195 EOF
196 my($name, $passwd, $Uid, $Gid);
197 while ( 1 ) {
198     $Conf{BackupPCUser} = prompt("--> BackupPC should run as user",
199                                  $Conf{BackupPCUser} || "backuppc");
200     ($name, $passwd, $Uid, $Gid) = getpwnam($Conf{BackupPCUser});
201     last if ( $name ne "" );
202     print <<EOF;
203
204 getpwnam() says that user $Conf{BackupPCUser} doesn't exist.  Please check the
205 name and verify that this user is in the passwd file.
206
207 EOF
208 }
209
210 print <<EOF;
211
212 Please specify an install directory for BackupPC.  This is where the
213 BackupPC scripts, library and documentation will be installed.
214
215 EOF
216
217 while ( 1 ) {
218     $Conf{InstallDir} = prompt("--> Install directory (full path)",
219                                $Conf{InstallDir});
220     last if ( $Conf{InstallDir} =~ /^\// );
221 }
222
223 print <<EOF;
224
225 Please specify a data directory for BackupPC.  This is where the
226 configuration files, LOG files and all the PC backups are stored.
227 This file system needs to be big enough to accommodate all the
228 PCs you expect to backup (eg: at least 1-2GB per machine).
229
230 EOF
231
232 while ( 1 ) {
233     $Conf{TopDir} = prompt("--> Data directory (full path)", $Conf{TopDir});
234     last if ( $Conf{TopDir} =~ /^\// );
235 }
236
237 if ( !defined($Conf{CompressLevel}) ) {
238     $Conf{CompressLevel} = BackupPC::FileZIO->compOk ? 3 : 0;
239     if ( $ConfigPath eq "" && $Conf{CompressLevel} ) {
240         print <<EOF;
241
242 BackupPC can compress pool files, providing around a 40% reduction in pool
243 size (your mileage may vary). Specify the compression level (0 turns
244 off compression, and 1 to 9 represent good/fastest to best/slowest).
245 The recommended values are 0 (off) or 3 (reasonable compression and speed).
246 Increasing the compression level to 5 will use around 20% more cpu time
247 and give perhaps 2-3% more compression.
248
249 EOF
250     } elsif ( $ConfigPath eq "" ) {
251         print <<EOF;
252
253 BackupPC can compress pool files, but it needs the Compress::Zlib
254 package installed (see www.cpan.org). Compression will provide around a
255 40% reduction in pool size, at the expense of cpu time.  You can leave
256 compression off and run BackupPC without compression, in which case you
257 should leave the compression level at 0 (which means off).  You could
258 install Compress::Zlib and turn compression on later, but read the
259 documentation first about how to do this.  Or the better choice is
260 to quit, install Compress::Zlib, and re-run configure.pl.
261
262 EOF
263     } elsif ( $Conf{CompressLevel} ) {
264         $Conf{CompressLevel} = 0;
265         print <<EOF;
266
267 BackupPC now supports pool file compression.  Since you are upgrading
268 BackupPC you probably have existing uncompressed backups.  You have
269 several choices if you want to turn on compression.  You can run
270 the script BackupPC_compressPool to convert everything to compressed
271 form.  Or you can simply turn on compression, so that new backups
272 will be compressed.  This will increase the pool storage requirement,
273 since both uncompressed and compressed copies of files will be stored.
274 But eventually the old uncompressed backups will expire, recovering
275 the pool storage.  Please see the documentation for more details.
276
277 If you are not sure what to do, leave the Compression Level at 0,
278 which disables compression.  You can always read the documentation
279 and turn it on later.
280
281 EOF
282     } else {
283         $Conf{CompressLevel} = 0;
284         print <<EOF;
285
286 BackupPC now supports pool file compression, but it needs the
287 Compress::Zlib module (see www.cpan.org).  For now, leave
288 the compression level set at 0 to disable compression.  If you
289 want you can install Compress::Zlib and turn compression on.
290 Please see the documentation for more details about converting
291 old backups to compressed form.
292
293 EOF
294     }
295     while ( 1 ) {
296         $Conf{CompressLevel}
297                     = prompt("--> Compression level", $Conf{CompressLevel});
298         last if ( $Conf{CompressLevel} =~ /^\d+$/ );
299     }
300 }
301
302 print <<EOF;
303
304 BackupPC has a powerful CGI perl interface that runs under Apache.
305 A single executable needs to be installed in a cgi-bin directory.
306 This executable needs to run as set-uid $Conf{BackupPCUser}, or
307 it can be run under mod_perl with Apache running as user $Conf{BackupPCUser}.
308
309 Leave this path empty if you don't want to install the CGI interface.
310
311 EOF
312
313 while ( 1 ) {
314     $Conf{CgiDir} = prompt("--> CGI bin directory (full path)", $Conf{CgiDir});
315     last if ( $Conf{CgiDir} =~ /^\// || $Conf{CgiDir} eq "" );
316 }
317
318 if ( $Conf{CgiDir} ne "" ) {
319
320     print <<EOF;
321
322 BackupPC's CGI script needs to display various GIF images that
323 should be stored where Apache can serve them.  They should be
324 placed somewher under Apache's DocumentRoot.  BackupPC also
325 needs to know the URL to access these images.  Example:
326
327     Apache image directory:  /usr/local/apache/htdocs/BackupPC
328     URL for image directory: /BackupPC
329
330 EOF
331     while ( 1 ) {
332         $Conf{CgiImageDir} = prompt("--> Apache image directory (full path)",
333                                         $Conf{CgiImageDir});
334         last if ( $Conf{CgiImageDir} =~ /^\// );
335     }
336     while ( 1 ) {
337         $Conf{CgiImageDirURL} = prompt("--> URL for image directory (omit http://host)",
338                                         $Conf{CgiImageDirURL});
339         last if ( $Conf{CgiImageDirURL} ne "" );
340     }
341 }
342
343 print <<EOF;
344
345 Ok, we're about to:
346
347   - install the binaries, lib and docs in $Conf{InstallDir},
348   - create the data directory $Conf{TopDir},
349   - create/update the config.pl file $Conf{TopDir}/conf,
350   - optionally install the cgi-bin interface.
351
352 EOF
353
354 exit unless prompt("--> Do you want to continue?", "y") =~ /y/i;
355
356 #
357 # Create install directories
358 #
359 foreach my $dir ( qw(bin lib/BackupPC/Xfer lib/BackupPC/Zip
360                      lib/BackupPC/Lang doc) ) {
361     next if ( -d "$Conf{InstallDir}/$dir" );
362     mkpath("$Conf{InstallDir}/$dir", 0, 0775);
363     if ( !-d "$Conf{InstallDir}/$dir"
364             || !chown($Uid, $Gid, "$Conf{InstallDir}/$dir") ) {
365         die("Failed to create or chown $Conf{InstallDir}/$dir\n");
366     } else {
367         print("Created $Conf{InstallDir}/$dir\n");
368     }
369 }
370
371 #
372 # Create CGI image directory
373 #
374 foreach my $dir ( ($Conf{CgiImageDir}) ) {
375     next if ( $dir eq "" || -d $dir );
376     mkpath($dir, 0, 0775);
377     if ( !-d $dir || !chown($Uid, $Gid, $dir) ) {
378         die("Failed to create or chown $dir");
379     } else {
380         print("Created $dir\n");
381     }
382 }
383
384 #
385 # Create $TopDir's top-level directories
386 #
387 foreach my $dir ( qw(. conf pool cpool pc trash log) ) {
388     mkpath("$Conf{TopDir}/$dir", 0, 0750) if ( !-d "$Conf{TopDir}/$dir" );
389     if ( !-d "$Conf{TopDir}/$dir"
390             || !chown($Uid, $Gid, "$Conf{TopDir}/$dir") ) {
391         die("Failed to create or chown $Conf{TopDir}/$dir\n");
392     } else {
393         print("Created $Conf{TopDir}/$dir\n");
394     }
395 }
396
397 printf("Installing binaries in $Conf{InstallDir}/bin\n");
398 foreach my $prog ( qw(BackupPC BackupPC_dump BackupPC_link BackupPC_nightly
399         BackupPC_sendEmail BackupPC_tarCreate BackupPC_trashClean
400         BackupPC_tarExtract BackupPC_compressPool BackupPC_zcat
401         BackupPC_restore BackupPC_serverMesg BackupPC_zipCreate ) ) {
402     InstallFile("bin/$prog", "$Conf{InstallDir}/bin/$prog", 0555);
403 }
404
405 #
406 # Remove unused binaries from older versions
407 #
408 unlink("$Conf{InstallDir}/bin/BackupPC_queueAll");
409
410 printf("Installing library in $Conf{InstallDir}/lib\n");
411 foreach my $lib ( qw(BackupPC/Lib.pm BackupPC/FileZIO.pm BackupPC/Attrib.pm
412         BackupPC/PoolWrite.pm BackupPC/Xfer/Tar.pm BackupPC/Xfer/Smb.pm
413         BackupPC/Zip/FileMember.pm
414         BackupPC/Lang/en.pm BackupPC/Lang/fr.pm
415     ) ) {
416     InstallFile("lib/$lib", "$Conf{InstallDir}/lib/$lib", 0444);
417 }
418
419 if ( $Conf{CgiImageDir} ne "" ) {
420     printf("Installing images in $Conf{CgiImageDir}\n");
421     foreach my $img ( <images/*> ) {
422         (my $destImg = $img) =~ s{^images/}{};
423         InstallFile($img, "$Conf{CgiImageDir}/$destImg", 0444, 1);
424     }
425 }
426
427 printf("Making init.d scripts\n");
428 foreach my $init ( qw(linux-backuppc solaris-backuppc) ) {
429     InstallFile("init.d/src/$init", "init.d/$init", 0444);
430 }
431
432 printf("Installing docs in $Conf{InstallDir}/doc\n");
433 foreach my $doc ( qw(BackupPC.pod BackupPC.html) ) {
434     InstallFile("doc/$doc", "$Conf{InstallDir}/doc/$doc", 0444);
435 }
436
437 printf("Installing config.pl and hosts in $Conf{TopDir}/conf\n");
438 InstallFile("conf/hosts", "$Conf{TopDir}/conf/hosts", 0644)
439                     if ( !-f "$Conf{TopDir}/conf/hosts" );
440
441 #
442 # Now do the config file.  If there is an existing config file we
443 # merge in the new config file, adding any new configuration
444 # parameters and deleting ones that are no longer needed.
445 #
446 my $dest = "$Conf{TopDir}/conf/config.pl";
447 my ($newConf, $newVars) = ConfigParse("conf/config.pl");
448 my ($oldConf, $oldVars);
449 if ( -f $dest ) {
450     ($oldConf, $oldVars) = ConfigParse($dest);
451     $newConf = ConfigMerge($oldConf, $oldVars, $newConf, $newVars);
452 }
453 $Conf{EMailFromUserName}  ||= $Conf{BackupPCUser};
454 $Conf{EMailAdminUserName} ||= $Conf{BackupPCUser};
455 #
456 # IncrFill should now be off
457 #
458 $Conf{IncrFill} = 0;
459 #
460 # Figure out sensible arguments for the ping command
461 #
462 if ( $^O eq "solaris" || $^O eq "sunos" ) {
463     $Conf{PingArgs} ||= '-s $host 56 1';
464 } elsif ( $^O eq "linux" || $^O eq "openbsd" || $^O eq "netbsd" ) {
465     $Conf{PingArgs} ||= '-c 1 -w 3 $host';
466 } else {
467     $Conf{PingArgs} ||= '-c 1 $host';
468 }
469
470 my $confCopy = "$dest.pre-1.4.0.b1";
471 if ( -f $dest && !-f $confCopy ) {
472     #
473     # Make copy of config file, preserving ownership and modes
474     #
475     printf("Making backup copy of $dest -> $confCopy\n");
476     my @stat = stat($dest);
477     my $mode = $stat[2];
478     my $uid  = $stat[4];
479     my $gid  = $stat[5];
480     die("can't copy($dest, $confCopy)\n")  unless copy($dest, $confCopy);
481     die("can't chown $uid, $gid $confCopy\n")
482                                            unless chown($uid, $gid, $confCopy);
483     die("can't chmod $mode $confCopy\n")   unless chmod($mode, $confCopy);
484 }
485 open(OUT, ">$dest") || die("can't open $dest for writing\n");
486 my $blockComment;
487 foreach my $var ( @$newConf ) {
488     if ( length($blockComment)
489           && substr($var->{text}, 0, length($blockComment)) eq $blockComment ) {
490         $var->{text} = substr($var->{text}, length($blockComment));
491         $blockComment = undef;
492     }
493     $blockComment = $1 if ( $var->{text} =~ /^([\s\n]*#{70}.*#{70}[\s\n]+)/s );
494     $var->{text} =~ s/^\s*\$Conf\{(.*?)\}(\s*=\s*['"]?)(.*?)(['"]?\s*;)/
495                 defined($Conf{$1}) && ref($Conf{$1}) eq ""
496                                    && $Conf{$1} ne $OrigConf{$1}
497                                    ? "\$Conf{$1}$2$Conf{$1}$4"
498                                    : "\$Conf{$1}$2$3$4"/emg;
499     print OUT $var->{text};
500 }
501 close(OUT);
502 if ( !defined($oldConf) ) {
503     die("can't chmod 0640 mode $dest\n")  unless chmod(0640, $dest);
504     die("can't chown $Uid, $Gid $dest\n") unless chown($Uid, $Gid, $dest);
505 }
506
507 if ( $Conf{CgiDir} ne "" ) {
508     printf("Installing cgi script BackupPC_Admin in $Conf{CgiDir}\n");
509     mkpath("$Conf{CgiDir}", 0, 0755);
510     InstallFile("cgi-bin/BackupPC_Admin", "$Conf{CgiDir}/BackupPC_Admin",
511                 04554);
512 }
513
514 print <<EOF;
515
516 Ok, it looks we are finished.  There are several more things you
517 will need to do:
518
519   - Browse through the config file, $Conf{TopDir}/conf/config.pl,
520     and make sure all the settings are correct.  In particular, you
521     will need to set the smb share password and user name, backup
522     policies and check the email message headers and bodies.
523
524   - Edit the list of hosts to backup in $Conf{TopDir}/conf/hosts.
525
526   - Read the documentation in $Conf{InstallDir}/doc/BackupPC.html.
527     Please pay special attention to the security section.
528
529   - Verify that the CGI script BackupPC_Admin runs correctly.  You might
530     need to change the permissions or group ownership of BackupPC_Admin.
531
532   - BackupPC should be ready to start.  Don't forget to run it
533     as user $Conf{BackupPCUser}!  The installation also contains an
534     init.d/backuppc script that can be copied to /etc/init.d
535     so that BackupPC can auto-start on boot.  See init.d/README.
536
537 Enjoy!
538 EOF
539
540 ###########################################################################
541 # Subroutines
542 ###########################################################################
543
544 sub InstallFile
545 {
546     my($prog, $dest, $mode, $binary) = @_;
547     my $first = 1;
548     my($uid, $gid) = ($Uid, $Gid);
549
550     if ( -f $dest ) {
551         #
552         # preserve ownership and modes of files that already exist
553         #
554         my @stat = stat($dest);
555         $mode = $stat[2];
556         $uid  = $stat[4];
557         $gid  = $stat[5];
558     }
559     unlink($dest) if ( -f $dest );
560     if ( $binary ) {
561         die("can't copy($prog, $dest)\n") unless copy($prog, $dest);
562     } else {
563         open(PROG, $prog)   || die("can't open $prog for reading\n");
564         open(OUT, ">$dest") || die("can't open $dest for writing\n");
565         while ( <PROG> ) {
566             s/__INSTALLDIR__/$Conf{InstallDir}/g;
567             s/__TOPDIR__/$Conf{TopDir}/g;
568             s/__BACKUPPCUSER__/$Conf{BackupPCUser}/g;
569             s/__CGIDIR__/$Conf{CgiDir}/g;
570             if ( $first && /^#.*bin\/perl/ ) {
571                 if ( $Perl56 ) {
572                     #
573                     # perl56 and later is taint ok
574                     #
575                     print OUT "#!$Conf{PerlPath} -T\n";
576                 } else {
577                     #
578                     # prior to perl56, File::Find fails taint checks,
579                     # so we run without -T.  It's still safe.
580                     #
581                     print OUT "#!$Conf{PerlPath}\n";
582                 }
583             } else {
584                 print OUT;
585             }
586             $first = 0;
587         }
588         close(PROG);
589         close(OUT);
590     }
591     die("can't chown $uid, $gid $dest") unless chown($uid, $gid, $dest);
592     die("can't chmod $mode $dest")      unless chmod($mode, $dest);
593 }
594
595 sub FindProgram
596 {
597     my($path, $prog) = @_;
598     foreach my $dir ( split(/:/, $path) ) {
599         my $file = File::Spec->catfile($dir, $prog);
600         return $file if ( -x $file );
601     }
602 }
603
604 sub ConfigParse
605 {
606     my($file) = @_;
607     open(C, $file) || die("can't open $file");
608     my($out, @conf, $var);
609     my $comment = 1;
610     my $allVars = {};
611     while ( <C> ) {
612         if ( /^\s*#/ ) {
613             if ( $comment ) {
614                 $out .= $_;
615             } else {
616                 if ( $out ne "" ) {
617                     $allVars->{$var} = @conf if ( defined($var) );
618                     push(@conf, {
619                         text => $out,
620                         var => $var,
621                     });
622                 }
623                 $var = undef;
624                 $comment = 1;
625                 $out = $_;
626             }
627         } elsif ( /^\s*\$Conf\{([^}]*)/ ) {
628             $comment = 0;
629             if ( defined($var) ) {
630                 $allVars->{$var} = @conf if ( defined($var) );
631                 push(@conf, {
632                     text => $out,
633                 var => $var,
634                 });
635                 $out = $_;
636             } else {
637                 $out .= $_;
638             }
639             $var = $1;
640         } else {
641             $out .= $_;
642         }
643     }
644     if ( $out ne "" ) {
645         $allVars->{$var} = @conf if ( defined($var) );
646         push(@conf, {
647             text => $out,
648             var  => $var,
649         });
650     }
651     close(C);
652     return (\@conf, $allVars);
653 }
654
655 sub ConfigMerge
656 {
657     my($old, $oldVars, $new, $newVars) = @_;
658     my $posn = 0;
659     my $res;
660
661     #
662     # Find which config parameters are not needed any longer
663     #
664     foreach my $var ( @$old ) {
665         next if ( !defined($var->{var}) || defined($newVars->{$var->{var}}) );
666         #print(STDERR "Deleting old config parameter $var->{var}\n");
667         $var->{delete} = 1;
668     }
669     #
670     # Find which config parameters are new
671     #
672     foreach my $var ( @$new ) {
673         next if ( !defined($var->{var}) );
674         if ( defined($oldVars->{$var->{var}}) ) {
675             $posn = $oldVars->{$var->{var}};
676         } else {
677             #print(STDERR "New config parameter $var->{var}: $var->{text}\n");
678             push(@{$old->[$posn]{new}}, $var);
679         }
680     }
681     #
682     # Create merged config file
683     #
684     foreach my $var ( @$old ) {
685         next if ( $var->{delete} );
686         push(@$res, $var);
687         foreach my $new ( @{$var->{new}} ) {
688             push(@$res, $new);
689         }
690     }
691     return $res;
692 }