Handle hostnames with upper-case letters
[webmin.git] / fix-english.pl
1 #!/usr/local/bin/perl
2 # Convert words in lang/en files from UK to US spelling.
3 # Create lang/en_GB files containing words that are different.
4
5 if ($ARGV[0] eq "--svn" || $ARGV[0] eq "-svn" ||
6     $ARGV[0] eq "--git" || $ARGV[0] eq "-git") {
7         shift(@ARGV);
8         $svn = shift(@ARGV);
9         }
10
11 chdir("/usr/local/webadmin");
12 if (@ARGV) {
13         @modules = @ARGV;
14         }
15 else {
16         @modules = ( "." );
17         opendir(DIR, ".");
18         foreach $d (readdir(DIR)) {
19                 push(@modules, $d) if (-r "$d/module.info");
20                 }
21         closedir(DIR);
22         }
23
24 # Get the words
25 open(MAPPING, "english-mappings.txt") ||
26         die "Failed to open english-mappings.txt";
27 while(<MAPPING>) {
28         s/\r|\n//g;
29         s/#.*$//;
30         my ($us, $uk) = split(/\t+/, $_);
31         if ($us && $uk) {
32                 push(@us_mappings, [ $us, $uk ]);
33                 }
34         }
35 close(MAPPING);
36 @uk_mappings = map { [ $_->[1], $_->[0] ] } @us_mappings;
37 print STDERR "Found ",scalar(@uk_mappings)," mappings\n";
38
39 # Do all the given modules
40 @rv = ( );
41 foreach $m (@modules) {
42         print STDERR "Doing module $m\n";
43         push(@rv, &fix_english_file("$m/lang/en", "$m/lang/en_GB", 1));
44         push(@rv, &fix_english_file("$m/config.info",
45                                     "$m/config.info.en_GB", 1));
46         opendir(HELP, "$m/help");
47         foreach $h (readdir(HELP)) {
48                 if ($h =~ /^([^\.]+)\.html$/) {
49                         push(@rv, &fix_english_file("$m/help/$h",
50                                           "$m/help/$1.en_GB.html", 0));
51                         }
52                 }
53         closedir(HELP);
54         }
55
56 # Print and commit the files
57 foreach $f (@rv) {
58         print $f,"\n";
59         if ($svn) {
60                 ($dir, $rest) = split(/\//, $f, 2);
61                 system("cd $dir ; git add $rest ; git commit -m '$svn' $rest ; git push");
62                 }
63         }
64
65 sub fix_english_file
66 {
67 local ($us, $uk, $linefmt) = @_;
68 return ( ) if (!-r $us);
69 local @rv;
70 if ($linefmt) {
71         # Webmin = separated line file
72
73         # First fix up any UK spellings in the US file
74         local %uslines;
75         &read_file($us, \%uslines);
76         local $changed_us;
77         foreach my $k (keys %uslines) {
78                 $v = $uslines{$k};
79                 $usv = &convert_to_us($v);
80                 if ($usv ne $v) {
81                         $uslines{$k} = $usv;
82                         $changed_us++;
83                         }
84                 }
85         if ($changed_us) {
86                 &write_file($us, \%uslines);
87                 push(@rv, $us);
88                 }
89
90         # Then create a UK file with only lines that need changing
91         local %uklines;
92         &read_file($uk, \%uklines);
93         local $changed_uk;
94         foreach my $k (keys %uslines) {
95                 $v = $uslines{$k};
96                 $ukv = &convert_to_uk($v);
97                 if ($ukv ne $v && $uklines{$k} ne $ukv) {
98                         $uklines{$k} = $ukv;
99                         $changed_uk++;
100                         }
101                 }
102         if ($changed_uk) {
103                 &write_file($uk, \%uklines);
104                 push(@rv, $uk);
105                 }
106         }
107 else {
108         # Big blob of text
109
110         # First fix up any UK spellings in the US file
111         local $ustext = &read_file_contents($us);
112         $usv = &convert_to_us($ustext);
113         if ($usv ne $ustext) {
114                 &write_file_contents($us, $usv);
115                 push(@rv, $us);
116                 }
117
118         # Then create a UK file
119         $uktext = &read_file_contents($uk);
120         $ukv = &convert_to_uk($usv);
121         if ($uktext ne $ukv && $ukv ne $usv) {
122                 &write_file_contents($uk, $ukv);
123                 push(@rv, $uk);
124                 }
125         }
126 return @rv;
127 }
128
129 sub convert_to_us
130 {
131 local ($str) = @_;
132 return &convert_mapping($str, \@uk_mappings);
133 }
134
135 sub convert_to_uk
136 {
137 local ($str) = @_;
138 return &convert_mapping($str, \@us_mappings);
139 }
140
141 sub convert_mapping
142 {
143 local ($str, $fromto) = @_;
144 foreach my $w (@$fromto) {
145         my ($from, $to) = @$w;
146         $str =~ s/(\s|^)\Q$from\E(\s|$)/$1$to$2/g;
147         $from = ucfirst($from);
148         $to = ucfirst($to);
149         $str =~ s/(\s|^)\Q$from\E(\s|$)/$1$to$2/g;
150         }
151 return $str;
152 }
153
154 # read_file(file, &assoc, [&order], [lowercase])
155 # Fill an associative array with name=value pairs from a file
156 sub read_file
157 {
158 open(ARFILE, $_[0]) || return 0;
159 while(<ARFILE>) {
160         s/\r|\n//g;
161         if (!/^#/ && /^([^=]+)=(.*)$/) {
162                 $_[1]->{$_[3] ? lc($1) : $1} = $2;
163                 push(@{$_[2]}, $1) if ($_[2]);
164                 }
165         elsif (!/\S/) {
166                 push(@{$_[2]}, undef) if ($_[2]);
167                 }
168         }
169 close(ARFILE);
170 return 1;
171 }
172  
173 # write_file(file, array)
174 # Write out the contents of an associative array as name=value lines
175 sub write_file
176 {
177 local(%old, @order);
178 &read_file($_[0], \%old, \@order);
179 open(ARFILE, ">$_[0]");
180 foreach $k (@order) {
181         if (!defined($k)) {
182                 print ARFILE "\n";
183                 }
184         elsif (exists($_[1]->{$k})) {
185                 print ARFILE $k,"=",$_[1]->{$k},"\n";
186                 }
187         }
188 foreach $k (keys %{$_[1]}) {
189         print ARFILE $k,"=",$_[1]->{$k},"\n" if (!exists($old{$k}));
190         }
191 close(ARFILE);
192 }
193
194 sub read_file_contents
195 {
196 open(FILE, $_[0]) || return undef;
197 local $/ = undef;
198 local $rv = <FILE>;
199 close(FILE);
200 return $rv;
201 }
202
203 sub write_file_contents
204 {
205 open(FILE, ">$_[0]") || return undef;
206 print FILE $_[1];
207 close(FILE);
208 }
209