1#! @PERL@
2
3eval 'exec @PERL@ -S $0 ${1+"$@"}'
4    if 0;
5
6#                                                         -*- Perl -*-
7# Copyright (c) 2000 - 2003  Motoyuki Kasahara
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2, or (at your option)
12# any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19
20# Program name, program version and mailing address.
21$progname ='ebnupgrade';
22$version = '@VERSION@';
23$mailing_address = '@MAILING_ADDRESS@';
24
25# Default configuration files.
26$old_file = '@sysconfdir@/ebnetd.conf';
27$new_file = '';
28
29# Test mode flag.
30$test_mode = 0;
31
32# Quiet mode flag.
33$quiet_mode = 0;
34
35# Help message.
36$help = "Usage: $progname [option...]
37Options:
38  -c FILE  --configuration-file FILE
39                             input old configuration from FILE
40                             (default: $old_file)
41  -h  --help                 display this help, then exit
42  -o FILE  --output-file FILE
43                             output new configuration to FILE
44                             (default: $old_file.new)
45  -q  --quiet  --silence     suppress report messages
46  -t  --test                 only check for input file
47  -v  --version              display version number, then exit
48
49Report bugs to $mailing_address.
50";
51
52# Copyright message.
53$copyright = "Copyright (c) 2000 - 2003
54   Motoyuki Kasahara
55";
56
57$tryhelp = "try \`$0 --help\' for more information\n";
58
59# Command line options.
60@long_options = ('-c --configuration-file required-argument',
61		 '-o --output-file        required-argument',
62		 '-h --help               no-argument',
63		 '-q --quiet --silence    no-argument',
64		 '-t --test               no-argument',
65		 '-v --version no-argument');
66
67# Netmask conversion table.
68%netmask_table = ('0.0.0.0',         0,
69		  '128.0.0.0',       1,
70		  '192.0.0.0',       2,
71		  '224.0.0.0',       3,
72		  '240.0.0.0',       4,
73		  '248.0.0.0',       5,
74		  '252.0.0.0',       6,
75		  '254.0.0.0',       7,
76		  '255.0.0.0',       8,
77		  '128.128.0.0',     9,
78		  '192.192.0.0',     10,
79		  '224.224.0.0',     11,
80		  '240.240.0.0',     12,
81		  '248.248.0.0',     13,
82		  '252.252.0.0',     14,
83		  '254.254.0.0',     15,
84		  '255.255.0.0',     16,
85		  '128.255.128.0',   17,
86		  '192.255.192.0',   18,
87		  '224.255.224.0',   19,
88		  '240.255.240.0',   20,
89		  '248.255.248.0',   21,
90		  '252.255.252.0',   22,
91		  '254.255.254.0',   23,
92		  '255.255.255.0',   24,
93		  '128.255.255.128', 25,
94		  '192.255.255.192', 26,
95		  '224.255.255.224', 27,
96		  '240.255.255.240', 28,
97		  '248.255.255.248', 29,
98		  '252.255.255.252', 30,
99		  '254.255.255.254', 31,
100		  '255.255.255.255', 32);
101
102#
103# Parse command line options.
104#
105&getopt_initialize(@long_options);
106while (($option_name, $option_argument) = &getopt_long) {
107    if ($option_name eq '-c') {
108	$old_file = $option_argument;
109    } elsif ($option_name eq '-h') {
110        print $help;
111        exit(0);
112    } elsif ($option_name eq '-o') {
113        $new_file = $option_argument;
114    } elsif ($option_name eq '-q') {
115        $quiet_mode = 1;
116    } elsif ($option_name eq '-t') {
117        $test_mode = 1;
118    } elsif ($option_name eq '-v') {
119        print "$progname (EBNETD) version $version\n";
120        print $copyright;
121        exit(0);
122    } else {
123        die $tryhelp;
124    }
125}
126
127if (@ARGV != 0) {
128    warn "$0: too many arguments\n";
129    die $tryhelp;
130}
131
132#
133# Open a configuration file.
134#
135if ($new_file eq '') {
136    if ($old_file eq '' || $old_file eq '-') {
137	$new_file = "ebnetd.conf.new";
138    } else {
139	$new_file = "$old_file.new";
140    }
141}
142
143if ($old_file eq '-') {
144    if (!open(OLD_FILE, "<& STDIN")) {
145	die "$0: failed to duplicate standard in, $!\n";
146    }
147    $old_file = '(stdin)';
148} else {
149    if (!open(OLD_FILE, "< $old_file")) {
150	die "$0: failed to open the file, $!: $old_file\n";
151    }
152}
153
154if (!$test_mode) {
155    if ($new_file eq '-') {
156	if (!open(NEW_FILE, ">& STDOUT")) {
157	    die "$0: failed to duplicate standard in, $!\n";
158	}
159    } else {
160	if (!open(NEW_FILE, "> $new_file")) {
161	    die "$0: failed to open the file, $!: $new_file\n";
162	}
163    }
164}
165
166#
167# Add a header.
168#
169print NEW_FILE "\#\# Converted by $progname $version\n\n" if (!$test_mode);
170
171while (<OLD_FILE>) {
172    #
173    # Remove spaces in the head and tail of the line.
174    #
175    chop if (/\n/);
176    s/\s*$//;
177    s/^(\s*)//;
178    $indent = $1;
179
180    #
181    # Skip the line if it is empty or comment line.
182    #
183    if (/^\#/ || /^$/) {
184	print NEW_FILE "$indent$_\n" if (!$test_mode);
185	next;
186    }
187
188    #
189    # Rewrite the line.
190    #
191    ($directive_name, $separator, $directive_value) = /^(\S+)(\s+)(\S.*)$/;
192
193    if ($directive_name eq 'pid-file'
194	|| $directive_name eq 'server-name'
195	|| $directive_name eq 'ident-hosts'
196	|| $directive_name eq 'ident-timeout'
197	|| $directive_name =~ /^alias-/) {
198	#
199	# They are obsolete directives.  Comment out.
200	#
201	printf(STDERR "%s: %d: $directive_name' is obsoleted (comment out)\n",
202	       $old_file, $.) if (!$quiet_mode);
203	print NEW_FILE "$indent\# $_\n" if (!$test_mode);
204
205    } elsif ($directive_name eq 'hosts'
206	     && $directive_value =~ /^([0-9.]+)\/(\d+\.\d+\.\d+\.\d+)$/) {
207	#
208	# Substitute a netmask notation.
209	#
210	$network = $1;
211	$netmask = $2;
212	printf(STDERR "%s: %d: netmask must be an integer (fixed)\n",
213	       $old_file, $.) if (!$quiet_mode);
214	if (defined($netmask_table{$netmask})) {
215	    $directive_value = "$network/$netmask_table{$netmask}";
216	    printf(NEW_FILE "%s%s%s%s\n", $indent, $directive_name,
217		   $separator, $directive_value) if (!$test_mode);
218	} else {
219	    printf(STDERR "%s: %d: cannot convert the netmask (comment out)\n",
220		   $old_file, $.) if (!$quiet_mode);
221	    print NEW_FILE "$indent\# $_\n" if (!$test_mode);
222	}
223
224    } elsif ($directive_name eq 'name' && $directive_value =~ /[A-Z]/) {
225	#
226	# Covert upper letters in a book name to lower letters.
227	#
228	printf(STDERR
229	       "%s: %d: letters in book name must be lower case (fixed)\n",
230	       $old_file, $.) if (!$quiet_mode);
231	$directive_value = "\L$directive_value";
232	printf(NEW_FILE "%s%s%s%s\n", $indent, $directive_name,
233	       $separator, $directive_value) if (!$test_mode);
234
235    } else {
236	#
237	# Other lines are output as it is.
238	#
239	print NEW_FILE "$indent$_\n" if (!$test_mode);
240    }
241}
242
243#
244# Close the configuration file.
245#
246close(OLD_FILE);
247close(NEW_FILE);
248
249# Local Variables:
250# mode: perl
251# End:
252