#! /usr/bin/perl

#                                                         -*- Perl -*-
# Copyright (c) 1997, 98, 2000, 01
#    Motoyuki Kasahara
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

#
# This program is a Perl package running on Perl 4.036 or later.
# The package provides routines to process command line options like
# as GNU getopt_long().
#
# Version:
#     2.0.2
#
# Interface:
#
#   &getopt_initialize(LIST)
#     Set a list of command line options and initialize internal data
#     for &getopt_long.
#     You must call the routine before calling &getopt_long.
#     Format of each element in the LIST is:
#
#         `CANONICAL-OPTION-NAME [ALIAS-OPTION-NAME...] ARGUMENT-FLAG'
#
#     CANONICAL-OPTION-NAME, ALIAS-OPTION-NAME and ARGUMENT-FLAG fields
#     are separated by spaces or tabs.
#
#     CANONICAL-OPTION-NAME and ALIAS-OPTION-NAME must be either a single
#     character option including preceding `-' (e.g. `-v'), or a long
#     name option including preceding `--' (e.g. `--version').  Whether
#     CANONICAL-OPTION-NAME is single character option or long name
#     option is not significant.
#
#     ARGUMENT-FLAG must be `no-argument', `required-argument' or 
#     `optional-argument'.  If it is set to `required-argument', the
#     option always takes an argument.  If set to `optional-argument',
#     an argument to the option is optional.
#
#     You can put a special element `+' or `-' at the first element in
#     LIST.  See `Details about Option Processing:' for details.
#     If succeeded to initialize, 1 is returned.  Otherwise 0 is
#     returned.
#
#   &getopt_long
#     Get a option name, and if exists, its argument of the leftmost
#     option in @ARGV.
#
#     An option name and its argument are returned as a list with two
#     elements; the first element is CANONICAL-OPTION-NAME of the option,
#     and second is its argument.
#     Upon return, the option and its argument are removed from @ARGV.
#     When you have already got all options in @ARGV, an empty list is
#     returned.  In this case, only non-option elements are left in
#     @ARGV.
#
#     When an error occurs, an error message is output to standard
#     error, and the option name in a returned list is set to `?'.
#
# Example:
#
#     &getopt_intialize('--help -h no-argument', '--version -v no-argument')
#         || die;
#
#     while (($name, $arg) = &getopt_long) {
#         die "For help, type \`$0 --help\'\n" if ($name eq '?');
#         $opts{$name} = $arg;
#     }
#
# Details about Option Processing:
#
#   * There are three processing modes:
#     1. PERMUTE
#        It permutes the contents of ARGV as it scans, so that all the
#        non-option ARGV-elements are at the end.  This mode is default.
#     2. REQUIRE_ORDER
#        It stops option processing when the first non-option is seen.
#        This mode is chosen if the environment variable POSIXLY_CORRECT
#        is defined, or the first element in the option list is `+'.
#     3. RETURN_IN_ORDER
#        It describes each non-option ARGV-element as if it were the
#        argument of an option with an empty name.
#        This mode is chosen if the first element in the option list is
#        `-'.
#
#   * An argument starting with `-' and not exactly `-', is a single
#     character option.
#     If the option takes an argument, it must be specified at just
#     behind the option name (e.g. `-f/tmp/file'), or at the next
#     ARGV-element of the option name (e.g. `-f /tmp/file').
#     If the option doesn't have an argument, other single character
#     options can be followed within an ARGV-element.  For example,
#     `-l -g -d' is identical to `-lgd'.
#     
#   * An argument starting with `--' and not exactly `--', is a long
#     name option.
#     If the option has an argument, it can be specified at behind the
#     option name preceded by `=' (e.g. `--option=argument'), or at the
#     next ARGV-element of the option name (e.g. `--option argument').
#     Long name options can be abbreviated as long as the abbreviation
#     is unique.
#
#   * The special argument `--' forces an end of option processing.
#

{
    package getopt_long;

    $initflag = 0;
    $REQUIRE_ORDER = 0;
    $PERMUTE = 1;
    $RETURN_IN_ORDER = 2;
}


#
# Initialize the internal data.
#
sub getopt_initialize {
    local(@fields);
    local($name, $flag, $canon);
    local($_);

    #
    # Determine odering.
    #
    if ($_[$[] eq '+') {
	$getopt_long'ordering = $getopt_long'REQUIRE_ORDER;
	shift(@_);
    } elsif ($_[$[] eq '-') {
	$getopt_long'ordering = $getopt_long'RETURN_IN_ORDER;
	shift(@_);
    } elsif (defined($ENV{'POSIXLY_CORRECT'})) {
	$getopt_long'ordering = $getopt_long'REQUIRE_ORDER;
    } else {
	$getopt_long'ordering = $getopt_long'PERMUTE;
    }

    #
    # Parse an option list.
    #
    %getopt_long'optnames = ();
    %getopt_long'argflags = ();

    foreach (@_) {
	@fields = split(/[ \t]+/, $_);
	if (@fields < 2) {
	    warn "$0: (getopt_initialize) too few fields \`$arg\'\n";
	    return 0;
	}
	$flag = pop(@fields);
	if ($flag ne 'no-argument' && $flag ne 'required-argument'
	    && $flag ne 'optional-argument') {
	    warn "$0: (getopt_initialize) invalid argument flag \`$flag\'\n";
	    return 0;
	}

	$canon = '';
	foreach $name (@fields) {
	    if ($name !~ /^-([^-]|-.+)$/) {
		warn "$0: (getopt_initialize) invalid option name \`$name\'\n";
		return 0;
	    } elsif (defined($getopt_long'optnames{$name})) {
		warn "$0: (getopt_initialize) redefined option \`$name\'\n";
		return 0;
	    }
	    $canon = $name if ($canon eq '');
	    $getopt_long'optnames{$name} = $canon;
	    $getopt_long'argflags{$name} = $flag;
	}
    }

    $getopt_long'endflag = 0;
    $getopt_long'shortrest = '';
    @getopt_long'nonopts = ();

    $getopt_long'initflag = 1;
}


#
# When it comes to the end of options, restore PERMUTEd non-option
# arguments to @ARGV.
#
sub getopt_end {
    $getopt_long'endflag = 1;
    unshift(@ARGV, @getopt_long'nonopts);
}


#
# Scan elements of @ARGV for getting an option.
#
sub getopt_long {
    local($name, $arg) = ('?', '');
    local($key, $pattern, $match_count, $ch);
    local($_);

    &getopt_initialize(@_) if (!$getopt_long'initflag);
    return () if ($getopt_long'endflag);

    #
    # Get next option argument.
    #
    if ($getopt_long'shortrest ne '') {
	$_ = '-'.$getopt_long'shortrest;
    } elsif (@ARGV == 0) {
	&getopt_end;
	return ();
    } elsif ($getopt_long'ordering == $getopt_long'PERMUTE) {
	while (0 < @ARGV && $ARGV[$[] !~ /^-./) {
	    push(@getopt_long'nonopts, shift(@ARGV));
	}
	if (@ARGV == 0) {
	    &getopt_end;
	    return ();
	}
	$_ = shift(@ARGV);
    } elsif ($getopt_long'ordering == $getopt_long'REQUIRE_ORDER) {
	$_ = shift(@ARGV);
	if (!/^-./) {
	    push(@getopt_long'nonopts, $_);
	    &getopt_end;
	    return ();
	}
    } else {
	# $getopt_long'ordering == RETURN_IN_ORDER
	$_ = shift(@ARGV);
    }

    #
    # Check the special argument `--'.
    # `--' indicates the end of the option list.
    #
    if ($_ eq '--' && $getopt_long'shortrest eq '') {
	#
	# `--' indicates the end of the option list.
	#
	&getopt_end;
	return ();
    }

    #
    # Check for long and short options.
    #
    if (/^(--[^=]+)/ && $getopt_long'shortrest eq '') {
	#
	# This is a long style option, which start with `--'.
	#
	$pattern = $1;
	if (defined($getopt_long'optnames{$pattern})) {
	    $name = $pattern;
	} else {
	    #
	    # The option `name' is not registered in `@optnames'.
	    # It may be an abbreviated
	    #
	    $match_count = 0;
	    foreach $key (keys(%getopt_long'optnames)) {
		if (index($key, $pattern) == 0) {
		    $name = $key;
		    $match_count++;
		}
	    }
	    if (2 <= $match_count) {
		warn "$0: option \`$_\' is ambiguous\n";
		return ('?', '');
	    } elsif ($match_count == 0) {
		warn "$0: unrecognized option \`$_\'\n";
		return ('?', '');
	    }
	}

	#
	# Check an argument to the option.
	#
	if ($getopt_long'argflags{$name} eq 'required-argument') {
	    if (/=(.*)$/) {
		$arg = $1;
	    } elsif (0 < @ARGV) {
		$arg = shift(@ARGV);
	    } else {
		warn "$0: option \`$_\' requires an argument\n";
		return ('?', '');
	    }
	} elsif ($getopt_long'argflags{$name} eq 'optional-argument') {
	    if (/=(.*)$/) {
		$arg = $1;
	    } elsif (0 < @ARGV && $ARGV[$[] !~ /^-./) {
		$arg = shift(@ARGV);
	    } else {
		$arg = '';
	    }
	} elsif (/=(.*)$/) {
	    warn "$0: option \`$name\' doesn't allow an argument\n";
	    return ('?', '');
	}
    } elsif (/^(-(.))(.*)/) {
	#
	# This is a short style option, which start with `-' (not `--').
	# Short options may be catinated (e.g. `-l -g' is equivalent to
	# `-lg').
	#
	($name, $ch, $getopt_long'shortrest) = ($1, $2, $3);

	if (defined($getopt_long'optnames{$name})) {
	    #
	    # The option `name' is found in `@optnames'.
	    # Check its argument.
	    #
	    if ($getopt_long'argflags{$name} eq 'required-argument') {
		if ($getopt_long'shortrest ne '') {
		    $arg = $getopt_long'shortrest;
		    $getopt_long'shortrest = '';
		} elsif (0 < @ARGV) {
		    $arg = shift(@ARGV);
		} else {
		    # 1003.2 specifies the format of this message.
		    warn "$0: option requires an argument -- $ch\n";
		    return ('?', '');
		}
	    } elsif ($getopt_long'argflags{$name} eq 'optional-argument') {
		if ($getopt_long'shortrest ne '') {
		    $arg = $getopt_long'shortrest;
		    $getopt_long'shortrest = '';
		} elsif (0 < @ARGV && $ARGV[$[] !~ /^-./) {
		    $arg = shift(@ARGV);
		} else {
		    $arg = '';
		}
	    }
	} else {
	    #
	    # This is an invalid option.
	    # 1003.2 specifies the format of this message.
	    #
	    if (defined($ENV{'POSIXLY_CORRECT'})) {
		warn "$0: illegal option -- $ch\n";
		return ('?', '');
	    } else {
		warn "$0: invalid option -- $ch\n";
		return ('?', '');
	    }
	}
    } else {
	#
	# This is a non-option argument.
	# Only RETURN_IN_ORDER falled into here.
	#
	return ('', $_);
    }

    return ($getopt_long'optnames{$name}, $arg);
}

1;
eval 'exec @PERL@ -S $0 ${1+"$@"}'
    if 0;

#                                                         -*- Perl -*-
# Copyright (c) 2000  Motoyuki Kasahara
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

# Program name, program version and mailing address.
$progname ='ndtpupgrade';
$version = '3.1.4';
$mailing_address = 'm-kasahr@sra.co.jp';

# Default configuration files.
$old_file = '/usr/local/etc/ndtpd.conf';
$new_file = '';

# Test mode flag.
$test_mode = 0;

# Quiet mode flag.
$quiet_mode = 0;

# Help message.
$help = "Usage: $progname [option...]
Options:
  -c FILE  --configuration-file FILE
                             input old configuration from FILE
                             (default: $old_file)
  -h  --help                 display this help, then exit
  -o FILE  --output-file FILE
                             output new configuration to FILE
                             (default: $old_file.new)
  -q  --quiet  --silence     suppress report messages
  -t  --test                 only check for input file
  -v  --version              display version number, then exit

Report bugs to $mailing_address.
";

# Copyright message.
$copyright = "Copyright (c) 2000  Motoyuki Kasahara

This is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
";

$tryhelp = "try \`$0 --help\' for more information\n";

# Command line options.
@long_options = ('-c --configuration-file required-argument',
		 '-o --output-file        required-argument',
		 '-h --help               no-argument',
		 '-q --quiet --silence    no-argument',
		 '-t --test               no-argument',
		 '-v --version no-argument');

# Netmask conversion table.
%netmask_table = ('0.0.0.0',         0,
		  '128.0.0.0',       1,
		  '192.0.0.0',       2,
		  '224.0.0.0',       3,
		  '240.0.0.0',       4,
		  '248.0.0.0',       5,
		  '252.0.0.0',       6,
		  '254.0.0.0',       7,
		  '255.0.0.0',       8,
		  '128.128.0.0',     9,
		  '192.192.0.0',     10,
		  '224.224.0.0',     11,
		  '240.240.0.0',     12,
		  '248.248.0.0',     13,
		  '252.252.0.0',     14,
		  '254.254.0.0',     15,
		  '255.255.0.0',     16,
		  '128.255.128.0',   17,
		  '192.255.192.0',   18,
		  '224.255.224.0',   19,
		  '240.255.240.0',   20,
		  '248.255.248.0',   21,
		  '252.255.252.0',   22,
		  '254.255.254.0',   23,
		  '255.255.255.0',   24,
		  '128.255.255.128', 25,
		  '192.255.255.192', 26,
		  '224.255.255.224', 27,
		  '240.255.255.240', 28,
		  '248.255.255.248', 29,
		  '252.255.255.252', 30,
		  '254.255.255.254', 31,
		  '255.255.255.255', 32);

#
# Parse command line options.
#
&getopt_initialize(@long_options);
while (($option_name, $option_argument) = &getopt_long) {
    if ($option_name eq '-c') {
	$old_file = $option_argument;
    } elsif ($option_name eq '-h') {
        print $help;
        exit(0);
    } elsif ($option_name eq '-o') {
        $new_file = $option_argument;
    } elsif ($option_name eq '-q') {
        $quiet_mode = 1;
    } elsif ($option_name eq '-t') {
        $test_mode = 1;
    } elsif ($option_name eq '-v') {
        print "$progname (NDTPD) version $version\n";
        print $copyright;
        exit(0);
    } else {
        die $tryhelp;
    }
}

if (@ARGV != 0) {
    warn "$0: too many arguments\n";
    die $tryhelp;
}

#
# Open a configuration file.
#
if ($new_file eq '') {
    if ($old_file eq '' || $old_file eq '-') {
	$new_file = "ndtpd.conf.new";
    } else {
	$new_file = "$old_file.new";
    }
}

if ($old_file eq '-') {
    if (!open(OLD_FILE, "<& STDIN")) {
	die "$0: failed to duplicate standard in, $!\n";
    }
    $old_file = '(stdin)';
} else {
    if (!open(OLD_FILE, "< $old_file")) {
	die "$0: failed to open the file, $!: $old_file\n";
    }
}

if (!$test_mode) {
    if ($new_file eq '-') {
	if (!open(NEW_FILE, ">& STDOUT")) {
	    die "$0: failed to duplicate standard in, $!\n";
	}
    } else {
	if (!open(NEW_FILE, "> $new_file")) {
	    die "$0: failed to open the file, $!: $new_file\n";
	}
    }
}

#
# Add a header.
#
print NEW_FILE "\#\# Converted by $progname $version\n\n" if (!$test_mode);

while (<OLD_FILE>) {
    #
    # Remove spaces in the head and tail of the line.
    #
    chop if (/\n/);
    s/\s*$//;
    s/^(\s*)//;
    $indent = $1;

    #
    # Skip the line if it is empty or comment line.
    #
    if (/^\#/ || /^$/) {
	print NEW_FILE "$indent$_\n" if (!$test_mode);
	next;
    }

    #
    # Rewrite the line.
    #
    ($directive_name, $separator, $directive_value) = /^(\S+)(\s+)(\S.*)$/;

    if ($directive_name eq 'pid-file'
	|| $directive_name eq 'server-name'
	|| $directive_name eq 'ident-hosts'
	|| $directive_name eq 'ident-timeout') {
	#
	# They are obsolete directives.  Comment out.
	#
	printf(STDERR "%s: %d: $directive_name' is obsoleted (comment out)\n",
	       $old_file, $.) if (!$quiet_mode);
	print NEW_FILE "$indent\# $_\n" if (!$test_mode);

    } elsif ($directive_name eq 'hosts'
	     && $directive_value =~ /^([0-9.]+)\/(\d+\.\d+\.\d+\.\d+)$/) {
	#
	# Substitute a netmask notation.
	#
	$network = $1;
	$netmask = $2;
	printf(STDERR "%s: %d: netmask must be an integer (fixed)\n",
	       $old_file, $.) if (!$quiet_mode);
	if (defined($netmask_table{$netmask})) {
	    $directive_value = "$network/$netmask_table{$netmask}";
	    printf(NEW_FILE "%s%s%s%s\n", $indent, $directive_name,
		   $separator, $directive_value) if (!$test_mode);
	} else {
	    printf(STDERR "%s: %d: cannot convert the netmask (comment out)\n",
		   $old_file, $.) if (!$quiet_mode);
	    print NEW_FILE "$indent\# $_\n" if (!$test_mode);
	}

    } elsif ($directive_name eq 'name' && $directive_value =~ /[A-Z]/) {
	#
	# Covert upper letters in a book name to lower letters.
	#
	printf(STDERR
	       "%s: %d: letters in book name must be lower case (fixed)\n",
	       $old_file, $.) if (!$quiet_mode);
	$directive_value = "\L$directive_value";
	printf(NEW_FILE "%s%s%s%s\n", $indent, $directive_name,
	       $separator, $directive_value) if (!$test_mode);
	
    } else {
	#
	# Other lines are output as it is.
	#
	print NEW_FILE "$indent$_\n" if (!$test_mode);
    }
}

#
# Close the configuration file.
#
close(OLD_FILE);
close(NEW_FILE);

# Local Variables: 
# mode: perl
# End: 
