root/releases/1.6/1p0/update-cnf.pl

Revision 51, 5.6 KB (checked in by pennmush, 3 years ago)

PennMUSH 1.6.0p3 Archival

  • Property svn:executable set to *
Line 
1#!/usr/local/bin/perl
2#
3# You make have to change the path to perl above, unless you call this
4# script with 'make update'
5#
6# update-cnf.pl - integrate previous mush.cnf settings with new
7#                  mush.cnf.dist. Results appear in mush.cnf
8#
9# Usage: update-cnf.pl old-file new-file
10#  e.g.: update-cnf.pl game/mush.cnf game/mush.cnf.dist
11#
12# 'make update' calls this program as in the example above.
13#
14# Here's how it works.
15# First, we make a backup of your old-file to old-file.bak
16# Then we read all the directives in the old-file, and their
17#  associated comments. Associated comments are those which
18#  appear on lines preceding the directive.
19#  We store the names of all the directives,
20#  their comments, and how they're defined.
21# Then we do the same for the new-file. If we find a directive
22#  that wasn't in the old-file, we show the user the comment
23#  and ask them how they want it set. Every time we write out
24#  a directive, we delete it from the list of directives from old-file
25# Finally, if there's anything left from old-file that's not in
26#  new-file, we ask if the user would like to retain each one.
27#  Presumably users want to retain their custom directives, but don't
28#  want to retain obsoleted directives. Retained directives appear at
29#  the end of the file.
30
31die "Usage: update-cnf.pl old-file new-file\n" unless $#ARGV == 1;
32
33$old = $ARGV[0];
34$bak = $old . ".bak";
35$new = $ARGV[1];
36
37
38# Part 1 - back up the old file (inefficient but reliable method)
39if (-r $old) {
40    print "*** Backing up $old to $bak...\n";
41    die "update-cnf.pl: Unable to open $old\n" unless open(OLD,"$old"); 
42    die "update-cnf.pl: Unable to open $bak\n" unless open(BAK,">$bak");
43    print BAK <OLD>;
44    close(BAK);
45    close(OLD);
46} else {
47    # Heck, let's just copy the new file to the old one and quit!
48    print "*** Creating $old from $new...\n";
49    die "update-cnf.pl: Unable to open $old\n" unless open(OLD,">$old"); 
50    die "update-cnf.pl: Unable to open $new\n" unless open(NEW,"$new"); 
51    print OLD <NEW>;
52    close(OLD);
53    close(NEW);
54    exit 0;
55}
56 
57
58# Part 2 - read the settings from the old file and store them
59if (-r $old) {
60    print "*** Reading your settings from $old...\n";
61    die "update-cnf.pl: Unable to open $old\n" unless open(OLD,"$old"); 
62    while (<OLD>) {
63    # We can have comments, which start with #,
64        # or directives, which start with anything else.
65    if (/^#/) {
66      # A comment
67      push(@comment,$_);
68    } elsif (/^(\S+)\s+(.+)$/) {
69      # A directive
70      $key = $1; $val = $2;
71      chop;
72      if ($directive{$key}) {
73          # This is a repeatable directive!
74          $num{$key}++;
75          $directive{$key."/".$num{$key}} = $val;
76          $comment{$key."/".$num{$key}} = join("",@comment) if @comment;
77      } else {
78          $directive{$key} = $val; 
79          $comment{$key} = join("",@comment) if @comment;
80      }
81      undef @comment;
82        } elsif (/^$/) {
83      # A blank line. Ignore comments so far
84      undef @comment;
85    }
86    }
87    close(OLD);
88}
89
90# Part 3 - read in the new file, modifying its definition lines to
91#          match the old file. If we come across a definition that
92#          isn't in the old file, ask the user about it.
93print "*** Updating $old from $new...\n";
94die "update-cnf.pl: Unable to open $old\n" unless open(OLD,">$old"); 
95die "update-cnf.pl: Unable to open $new\n" unless open(NEW,"$new"); 
96while (<NEW>) {
97    # We can have comments, which start with #,
98    # or directives, which start with anything else.
99    if (/^#/) {
100    # A comment
101    push(@comment,$_);
102    } elsif (/^(\S+)\s+(.+)$/) {
103    # Not a comment
104    chop;
105    m/^(\S+)\s+(.+)$/;
106    $key = $1;
107    if ($num{$key}) {
108        if ($num{$key} > 0) {
109        # A repeatable directive!
110        # Spew them all here. It's probably the best we can do.
111        print OLD @comment;
112        print OLD "$key\t$directive{$key}\n\n";
113        delete $comment{$key};
114        for ($i = 1; $i <= $num{$key}; $i++) {
115            print OLD $comment{$key."/".$i};
116            print OLD "$key\t",$directive{$key."/".$i},"\n\n";
117            delete $comment{$key."/".$i};
118            delete $directive{$key."/".$i};
119        } 
120        $num{$key} = -1; # Don't spew again
121        }
122    } else {
123        print OLD @comment;
124        if (!defined($directive{$key})) {
125        # It's new, just add it
126        print OLD $_,"\n";
127        push(@newoptions,$key);
128        } else {
129        # It's old
130        print OLD "$key\t$directive{$key}\n";
131        # Remove its comment.
132        delete $comment{$key};
133        }
134    }
135    undef @comment;
136    } elsif (/^$/) {
137    print OLD @comment;
138    undef @comment;
139    print OLD;
140    } else {
141    print OLD;
142    }
143}
144close(NEW);
145
146# Part 4 - if there are any definitions left from the old file,
147#          offer to delete them (or not)
148print "\n*** Checking for leftover defines from $old...\n";
149foreach $d (keys %comment) {
150    print "\nI found:\n";
151    print $comment{$d};
152    print "$d\t$directive{$d}\n";
153    print "\n";
154    print "If this is a directive that you hacked in, you probably should retain it.\n";
155    print "If not, it's probably an obsolete directive from an earlier release,\n";
156    print "and you need not retain it.\n";
157    print "Do you want to retain this in your $old file? [y] ";
158    $yn = <STDIN>;
159    if ($yn !~ /^[Nn]/) {
160    print "Retaining directive. It will appear at the end of $old.\n";
161        @retained = (@retained, $d);
162    print OLD $comment{$d};
163    print OLD "$d\t$directive{$d}\n";
164    print OLD "\n";
165    } else {
166    print "Deleting definition.\n";
167    @deleted = (@deleted, $d);
168    }
169}
170close(OLD);
171
172print "\nSummary of changes:\n";
173print "New options from $new: ",join(" ",@newoptions),"\n";
174print "Old options retained: ",join(" ",@retained),"\n";
175print "Old options deleted: ",join(" ",@deleted),"\n";
176print "If this is wrong, you can recover $old from $bak.\n";
177print "Done!\n";
178exit 0;
Note: See TracBrowser for help on using the browser.