#!/usr/bin/perl

# cvt-pppconfig - utility to convert Debian pppconfig settings to diald
# config files.

# Copyright (c) 2000 Software in the Public Interest, Inc.

# 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 of the License, 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.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# Written by Jeff Licquia <licquia@debian.org>.

# This program acts as a filter; it reads information from the
# pppconfig peers file referenced on the command line (or from stdin
# if none are specified) and writes a diald.options file on stdout.

# Changelog:

# 04/09/2000 Initial version.
# 08/20/2000 Fixed problem w/ hashes in usernames; added ability to 
#            specify a different default local and remote IP.

# Some of the pppd settings are controlled by diald instead of pppd;
# others should be passed on to pppd.  All options not specified here
# get passed to pppd.  (The port and speed settings get passed to
# diald as well.)

@diald_options = ("connect", "crtscts", "defaultroute",
		  "netmask", "modem", "mtu", "proxyarp");

# These pppd options should be suppressed if they appear in a
# pppconfig file for diald's purposes.

@diald_disallow = ("detach", "local", "xonxoff", "lock");

# These options should always be passed to pppd.

@pppd_options = ("nopersist");

# The default local/remote IP addresses may need to be changed.  If
# the file exists that contains different addresses, change the
# defaults.

$deflocal = "192.168.0.1";
$defremote = "192.168.0.2";
if (-f "/etc/diald/cvtaddrs")
  {
    open(DEFADDRS, "/etc/diald/cvtaddrs");
    ($deflocal, $defremote) = <DEFADDRS>;
    close DEFADDRS;
    chomp $deflocal, $defremote;
  }

# Now, to the nitty gritty.  First, read the options file.

@pcoptions = <>;

# Write an introductory header to the options file, explaining where
# it came from.

print "# This file is autogenerated.  Use 'dpkg-reconfigure diald' to change it.\n\n";

# Parse the options, writing diald-style options when necessary.

foreach $pcoption (@pcoptions)
  {
    # Skip & strip comments
    next if $pcoption =~ /^\s*\#/;
    $pcoption =~ s/\s*\#[^#]*$//;

    # Snag the first keyword
    $keyword = (split(/\s+/, $pcoption))[0];

    # Skip if the option simply isn't allowed
    next if grep(/$keyword/, @diald_disallow);

    if (grep(/$keyword/, @diald_options))
      {
	# Diald option - include directly in diald config
	print "$pcoption\n";
	next;
      }

    if ($keyword =~ /^\d+/)
      {
	# Speed parameter for diald
	print "speed $keyword\n";
	next;
      }

    if ($keyword =~ /^\/dev/)
      {
	# Device parameter for diald
	print "device $keyword\n";
	next;
      }

    if ($keyword =~ /^(\d*):(\d*)/)
      {
	# IP addresses
	next if ($1 !~ /\d/) and ($2 !~ /\d/);
	if ($1 =~ /\d/)
	  {
	    print "local $1\n";
	    $nolocal = 1;
	  }
	if ($2 =~ /\d/)
	  {
	    print "remote $2\n";
	    $noremote = 1;
	  }
	next;
      }

    # Nothing else matches - include it as a pppd option
    chomp $pcoption;
    push @pppd_options, $pcoption;
  }

# Write local and remote address if not already given
print "local $deflocal\n" unless $nolocal;
print "remote $defremote\n" unless $noremote;

# Write the pppd options to the diald config file
print "pppd-options ", join(" ", @pppd_options), "\n";

# Make sure the standard defaults files are also included
print "include /etc/diald/diald.defaults\n";

# All done!
