2 # $Id: sysconftool.in,v 1.1 2000/12/10 01:19:48 mrsam Exp $
3 # Copyright 2000 Double Precision, Inc. See COPYING for
4 # distribution information.
18 exit 1 unless GetOptions
("v" => \
$ver, "n" => \
$noclobber,
19 "f" => \
$force, "r=s" => \
$require);
21 print "$myversion\n" if $ver;
23 die "$0: Version not supported.\n"
24 if $require && versioncmp
($myversion, $require) < 0;
28 my $filename=shift @ARGV;
30 $filename =~ s/\.dist$//;
35 $rc=sysconftool
($filename, $noclobber, $force);
42 $@
.= "\n" unless $@
=~ /\n/s;
46 $exitcode=$rc if $rc > $exitcode;
56 my $distfile=new IO
::File
;
58 die "$filename.dist: $!\n" if ! $distfile->open("< $filename.dist");
60 my ($distheader, $distver);
62 ($distheader, $distver)= sysconftool_readver
($distfile);
64 die "$filename.dist: configuration header not found.\n" unless $distver;
66 my $oldfile=new IO
::File
;
68 if ( ! $oldfile->open($filename))
74 my ($dummy, $configver);
76 ($dummy, $configver)= sysconftool_readver
($oldfile);
80 $oldfile=undef; # Legacy config file
82 elsif ($configver eq $distver)
84 return 0 unless $force;
91 # If there's an old file, read old settings.
100 while (defined ($line=<$oldfile>))
104 $configname=$configversion="" if $resetflag;
107 if ($line =~ /^\#\#NAME:(.*):(.*)/)
109 ($configname, $configversion)=($1, $2);
111 $configname =~ s/[ \t]//g;
112 $configversion =~ s/[ \t]//g;
114 $old_version{$configname}=$configversion;
120 $old_settings{$configname} .= $line
127 my $newfile=new IO
::File
;
129 die "$filename.new: $!\n"
130 if ! $newfile->open($noclobber ?
">/dev/null":">$filename.new");
136 $f =~ s
:^.*/([^/]*)$:$1:;
141 # Try to carry over ownership and perms
143 my @inode=stat($distfile);
145 die $! unless $#inode > 0;
149 chown $inode[4], $inode[5], "$filename.new";
150 chmod $inode[2], "$filename.new";
153 (print $newfile $distheader) || die $!;
155 sysconftool_writeout
($newfile, $distfile, \
%old_settings,
156 \
%old_version, "$filename.dist");
162 unlink "$filename.new";
163 die "$filename.new: $@";
168 rename "$filename", "$filename.bak" unless $noclobber;
169 rename "$filename.new", "$filename" unless $noclobber;
173 # Read the version header from the file.
175 sub sysconftool_readver
{
185 last if ! defined $line || ++$cnt > 20;
189 return ($header, $line) if $line =~ /^\#\#VERSION:/;
196 # Read the dist file, write out the config file, and try to piece it back
197 # from the old config file.
199 sub sysconftool_writeout
{
202 my $old_settings=shift;
203 my $old_version=shift;
208 my $prefix_comment=0;
211 my $last_setting=undef;
212 my $prev_setting=undef;
214 while (defined($line=<$oldfile>))
216 if (! ($line =~ /^\#/))
220 # Before the first line of a new configuration setting
221 # print the obsoleted config setting (commented out).
223 (print $newfile $prev_setting) || die $!;
226 if ($prefix_comment > 0)
228 # Keeping old config setting, comment out the new dist
231 if ($prefix_comment < 2)
234 (print $newfile "#\n# DEFAULT SETTING from $filename:\n") || die $!;
239 elsif ($line =~ /^\#\#NAME:(.*):(.*)/)
241 ($configname, $configversion)=($1, $2);
243 $configname =~ s/[ \t]//g;
244 $configversion =~ s/[ \t]//g;
248 if (defined $last_setting)
250 # Write out old config setting before we go to the next
251 # setting in the dist file.
253 (print $newfile $last_setting) || die $!;
257 if ( defined $$old_settings{$configname})
259 if ($$old_version{$configname} eq $configversion)
261 # Setting didn't change in the dist file, keep
264 print " $configname: unchanged\n";
266 $last_setting=$$old_settings{$configname};
270 # Must install updated setting. Carefully comment
271 # out the current setting.
273 print " $configname: UPDATED\n";
276 split (/\n/s,"$$old_settings{$configname}\n");
278 push @lines, "" if $#lines < 0;
280 grep (s/^/\# /, @lines);
282 $prev_setting= "#\n# Previous setting (inserted by sysconftool):\n#\n" .
283 join("\n", @lines) . "\n#\n";
288 print " $configname: new\n";
292 (print $newfile $line) || die $!;
295 # Write out any pending settings.
297 if (defined $last_setting)
299 (print $newfile $last_setting) || die $!;
305 (print $newfile $prev_setting) || die $!;
311 # Not everyone has Sort::Version, so we roll our own here. It's not that bad.
314 my @a=split (/\./, shift);
315 my @b=split (/\./, shift);
322 last if (! defined $a) && (! defined $b);
324 return -1 if ! defined $a;
325 return 1 if ! defined $b;
327 my @ap=versionsplitclass
($a);
328 my @bp=versionsplitclass
($b);
335 last if (! defined $a) && (! defined $b);
337 return -1 if ! defined $a;
338 return 1 if ! defined $b;
357 sub versionsplitclass
{
363 if ($v =~ /^([0-9]+)(.*)/)
370 die unless $v =~ /^([^0-9]+)(.*)/;