2 # Copyright 2000 Double Precision, Inc. See COPYING for
3 # distribution information.
17 exit 1 unless GetOptions
("v" => \
$ver, "n" => \
$noclobber,
18 "f" => \
$force, "r=s" => \
$require);
20 print "$myversion\n" if $ver;
22 die "$0: Version not supported.\n"
23 if $require && versioncmp
($myversion, $require) < 0;
27 my $filename=shift @ARGV;
29 $filename =~ s/\.dist$//;
34 $rc=sysconftool
($filename, $noclobber, $force);
41 $@
.= "\n" unless $@
=~ /\n/s;
45 $exitcode=$rc if $rc > $exitcode;
55 my $distfile=new IO
::File
;
57 die "$filename.dist: $!\n" if ! $distfile->open("< $filename.dist");
59 my ($distheader, $distver);
61 ($distheader, $distver)= sysconftool_readver
($distfile);
63 die "$filename.dist: configuration header not found.\n" unless $distver;
65 my $oldfile=new IO
::File
;
67 if ( ! $oldfile->open($filename))
73 my ($dummy, $configver);
75 ($dummy, $configver)= sysconftool_readver
($oldfile);
79 $oldfile=undef; # Legacy config file
81 elsif ($configver eq $distver)
83 return 0 unless $force;
90 # If there's an old file, read old settings.
99 while (defined ($line=<$oldfile>))
103 $configname=$configversion="" if $resetflag;
106 if ($line =~ /^\#\#NAME:(.*):(.*)/)
108 ($configname, $configversion)=($1, $2);
110 $configname =~ s/[ \t]//g;
111 $configversion =~ s/[ \t]//g;
113 $old_version{$configname}=$configversion;
119 $old_settings{$configname} .= $line
126 my $newfile=new IO
::File
;
128 die "$filename.new: $!\n"
129 if ! $newfile->open($noclobber ?
">/dev/null":">$filename.new");
135 $f =~ s
:^.*/([^/]*)$:$1:;
140 # Try to carry over ownership and perms
142 my @inode=stat($distfile);
144 die $! unless $#inode > 0;
148 chown $inode[4], $inode[5], "$filename.new";
149 chmod $inode[2], "$filename.new";
152 (print $newfile $distheader) || die $!;
154 sysconftool_writeout
($newfile, $distfile, \
%old_settings,
155 \
%old_version, "$filename.dist");
161 unlink "$filename.new";
162 die "$filename.new: $@";
167 rename "$filename", "$filename.bak" unless $noclobber;
168 rename "$filename.new", "$filename" unless $noclobber;
172 # Read the version header from the file.
174 sub sysconftool_readver
{
184 last if ! defined $line || ++$cnt > 20;
188 return ($header, $line) if $line =~ /^\#\#VERSION:/;
195 # Read the dist file, write out the config file, and try to piece it back
196 # from the old config file.
198 sub sysconftool_writeout
{
201 my $old_settings=shift;
202 my $old_version=shift;
207 my $prefix_comment=0;
210 my $last_setting=undef;
211 my $prev_setting=undef;
213 while (defined($line=<$oldfile>))
215 if (! ($line =~ /^\#/))
219 # Before the first line of a new configuration setting
220 # print the obsoleted config setting (commented out).
222 (print $newfile $prev_setting) || die $!;
225 if ($prefix_comment > 0)
227 # Keeping old config setting, comment out the new dist
230 if ($prefix_comment < 2)
233 (print $newfile "#\n# DEFAULT SETTING from $filename:\n") || die $!;
238 elsif ($line =~ /^\#\#NAME:(.*):(.*)/)
240 ($configname, $configversion)=($1, $2);
242 $configname =~ s/[ \t]//g;
243 $configversion =~ s/[ \t]//g;
247 if (defined $last_setting)
249 # Write out old config setting before we go to the next
250 # setting in the dist file.
252 (print $newfile $last_setting) || die $!;
256 if ( defined $$old_settings{$configname})
258 if ($$old_version{$configname} eq $configversion)
260 # Setting didn't change in the dist file, keep
263 print " $configname: unchanged\n";
265 $last_setting=$$old_settings{$configname};
269 # Must install updated setting. Carefully comment
270 # out the current setting.
272 print " $configname: UPDATED\n";
275 split (/\n/s,"$$old_settings{$configname}\n");
277 push @lines, "" if $#lines < 0;
279 grep (s/^/\# /, @lines);
281 $prev_setting= "#\n# Previous setting (inserted by sysconftool):\n#\n" .
282 join("\n", @lines) . "\n#\n";
287 print " $configname: new\n";
291 (print $newfile $line) || die $!;
294 # Write out any pending settings.
296 if (defined $last_setting)
298 (print $newfile $last_setting) || die $!;
304 (print $newfile $prev_setting) || die $!;
310 # Not everyone has Sort::Version, so we roll our own here. It's not that bad.
313 my @a=split (/\./, shift);
314 my @b=split (/\./, shift);
321 last if (! defined $a) && (! defined $b);
323 return -1 if ! defined $a;
324 return 1 if ! defined $b;
326 my @ap=versionsplitclass
($a);
327 my @bp=versionsplitclass
($b);
334 last if (! defined $a) && (! defined $b);
336 return -1 if ! defined $a;
337 return 1 if ! defined $b;
356 sub versionsplitclass
{
362 if ($v =~ /^([0-9]+)(.*)/)
369 die unless $v =~ /^([^0-9]+)(.*)/;