{
#----------------------------------------------------------------------
# copyright (C) 1999-2003 Mitel Networks Corporation
#
# 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
#
# Technical support for this program is available from Mitel Networks
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------
# Migrate old config db singletons into "interface" specifications:
#
# DHCPClient=dhi    ExternalInterface=...|Configuration|DHCPHostname
# DHCPClient=(d|dh| ExternalInterface=...|Configuration|DHCPEthernetAddress
#
# EthernetDriver1=eepro100   LocalInterface=...|Name|eth0|Driver|eepro100
# EthernetDriver2=ne2k-pci   ExternalInterface=...|Name|eth1|Driver|ne2k-pci
# ExternalDHCP=off           ExternalInterface=...|Configuration|static
# ExternalNetmask=255.255.255.0   ExternalInterface=...|Netmask|255.255.255.0
# GatewayIP=192.168.116.1         ExternalInterface=...|Gateway|192.168.116.1
# LocalIP=192.168.116.2           LocalInterface=...|IPAddress|192.168.116.2
# LocalNetmask=255.255.255.0      LocalInterface=...|Netmask|255.255.255.0

    use esmith::util;

    my $internal = $DB->get("InternalInterface") ||
		   $DB->new_record("InternalInterface");

    # No need to run this unless we have valid settings
    return unless $DB->get_value("LocalIP");

    my $ethernet_assign = $DB->get_value('EthernetAssign') || "normal";
    my $diald = $DB->get("diald") || $DB->new_record("diald", { type => 'service' } );
    my $diald_status = 'disabled';

    my $dhcpcd = $DB->get("dhcpcd") || 
		 $DB->new_record("dhcpcd", { type => 'service' } );

    my %int_props =
    (
	type => "interface",
	Name => ($ethernet_assign eq 'swapped' ? "eth1" : "eth0"),
	Configuration => 'static',
	Driver => $DB->get_value("EthernetDriver1"),
	IPAddress => $DB->get_value("LocalIP"),
	Netmask => $DB->get_value("LocalNetmask")
    );


    ($int_props{Network}, $int_props{Broadcast}) =
	esmith::util::computeNetworkAndBroadcast($int_props{IPAddress},
						$int_props{Netmask} );

    $internal->merge_props(%int_props);

    my $mode = $DB->get_value("SystemMode") || "servergateway";

    my $external = $DB->get("ExternalInterface");

    if ( $mode eq "serveronly" )
    {
	$external->delete if defined $external;
	$diald->merge_props(status => 'disabled');
	$dhcpcd->merge_props(status => 'disabled');
	return;
    }

    $external ||= $DB->new_record("ExternalInterface");

    my $pppoe_status = $DB->get_prop('pppoe', 'status') || "disabled";
    my $access_type = $DB->get_value('AccessType') || "unknown";

    # Get the existing props
    my %ext_props = $external->props;

    # Delete ones which may no longer apply
    delete $ext_props{Driver};
    delete $ext_props{Configuration};

    # Set values which always apply
    $ext_props{type} = "interface";
    $ext_props{IPAddress} = $DB->get_value("ExternalIP");
    $ext_props{Netmask} = $DB->get_value("ExternalNetmask");
    $ext_props{Gateway} = $DB->get_value("GatewayIP");

    if (defined $ext_props{IPAddress} && defined $ext_props{Gateway})
    {
	($ext_props{Network}, $ext_props{Broadcast}) =
	    esmith::util::computeNetworkAndBroadcast($ext_props{IPAddress},
						$ext_props{Netmask} );
    }

    # Now determine others we need
    if ($access_type eq 'dialup')
    {
	my $isdn = $DB->get_prop('isdn', 'status') || "disabled";
	my $sync_isdn = $DB->get_prop('isdn', 'UseSyncPPP') || "no";

	$ext_props{Name} = ($isdn eq "enabled" and $sync_isdn eq "yes") ?
		"ippp0" : "ppp0";

       # XXX FIXME - we should probably have dialup vs. isdn here
	$ext_props{Configuration} = "dialup";
	$diald_status = 'enabled';
    }
    elsif ($pppoe_status eq 'enabled' )
    {
	$ext_props{Name} = "ppp0";
	$ext_props{Configuration} = "pppoe";

	my $pppoe = $DB->get('pppoe');
	unless ($pppoe)
	{
	    warn "pppoe record vanished\n";
	    return;
	}

	$pppoe->set_prop("PhysicalInterface",
		($ethernet_assign eq 'swapped') ? "eth0" : "eth1");
    }
    elsif ($ethernet_assign eq 'swapped')
    {
	$ext_props{Name} = "eth0";
	$ext_props{Driver} = $DB->get_value("EthernetDriver1");
    }
    else
    {
	$ext_props{Name} = "eth1";
	$ext_props{Driver} = $DB->get_value("EthernetDriver2");
    }

    my $external_dhcp = $DB->get_value("ExternalDHCP") || "off";


    my %dhcpcd_props = $dhcpcd->props;

    if ($external_dhcp eq "on")
    {
	my $dhcp_config = $DB->get_value("DHCPClient") || "d";

	$dhcpcd_props{status} = "enabled";

	if ($dhcp_config eq "dhi")
	{
	    # XXX FIXME - I think this should be "dhcpcd", which
	    # should be a new "service" type and the Hostname/MAC
	    # choice should be a property of that service
	    $ext_props{Configuration} = "DHCPHostname";
	}
	else
	{
	    $ext_props{Configuration} = "DHCPEthernetAddress";
	 }
    }
    else
    {
	$dhcpcd_props{status} = "disabled";
    }

    $ext_props{Configuration} ||= "static" ;

    # And write back the changes to the config db
    $external->merge_props(%ext_props);
    $dhcpcd->merge_props(%dhcpcd_props);
    $diald->merge_props(status => $diald_status);
}
