#!/usr/bin/perl -w
#----------------------------------------------------------------------
# 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.
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use esmith::templates;
use esmith::ConfigDB;

#------------------------------------------------------------
# Update ethernet interface files. We always have at least
# one ethernet adapter (eth0) for the internal network. If
# running in server and gateway mode with a dedicated connection,
# a second adapter (eth1) is used for the external connection.
# If running in "swapped mode", then the roles of the two
# adapters are swapped, but these rules still apply.
#------------------------------------------------------------

my $db = esmith::ConfigDB->open or die "Couldn't open ConfigDB\n";

unlink ("/etc/sysconfig/network-scripts/ifcfg-eth0");
unlink ("/etc/sysconfig/network-scripts/ifcfg-eth1");
unlink ("/etc/sysconfig/network-scripts/ifcfg-lo:0");

my $driver1 = $db->get_value('EthernetDriver1');

if ( defined ($driver1) and ($driver1 ne "unknown") )
{
    processTemplate(
    {
	TEMPLATE_PATH => "/etc/sysconfig/network-scripts/ifcfg-ethX",
	OUTPUT_FILENAME => "/etc/sysconfig/network-scripts/ifcfg-eth0",
	MORE_DATA => { THIS_DEVICE => "eth0" },
    } );
}

if ( ($db->get_value('SystemMode') =~ /servergateway/)
	and ( $db->get_value('AccessType') eq "dedicated"))
{
    my $driver2 = $db->get_value('EthernetDriver2');

    if ( defined $driver2 and ($driver2 ne "unknown") )
    {
	processTemplate(
	{
	    TEMPLATE_PATH => "/etc/sysconfig/network-scripts/ifcfg-ethX",
	    OUTPUT_FILENAME => "/etc/sysconfig/network-scripts/ifcfg-eth1",
	    MORE_DATA => { THIS_DEVICE => "eth1" },
	} );
    }
    #------------------------------------------------------------
    # Configure DHCP client.
    #------------------------------------------------------------

    my $dhcp_client = $db->get('DHCPClient');

    if ($dhcp_client and $dhcp_client->value eq 'dh')
    {
	$dhcp_client->set_value('d');
    }

    processTemplate ( { 
			  TEMPLATE_PATH	=> "/etc/dhcpc/dhcpcd.exe",
			  PERMS		=> 0755,
			  UID		=> "root",
			  GID		=> "root"
			} );

    unlink "/etc/dhcpc/dhcpcd-eth0.cache";
    unlink "/etc/dhcpc/dhcpcd-eth1.cache";
}

if ( $db->get_value('SystemMode') eq "servergateway-publicserver" )
{
    processTemplate (
		{ 
		  TEMPLATE_PATH => "/etc/sysconfig/network-scripts/ifcfg-lo:0",
		  PERMS		=> 0755,
		  UID		=> "root",
		  GID		=> "daemon",
		} );
}

#------------------------------------------------------------
# Configure networking config files.
#------------------------------------------------------------

processTemplate ({ TEMPLATE_PATH=>"/etc/sysconfig/network" });
processTemplate ({ TEMPLATE_PATH=>"/etc/nsswitch.conf" });
processTemplate ({ TEMPLATE_PATH=>"/etc/hosts" });
processTemplate ({ TEMPLATE_PATH=>"/etc/resolv.conf" });
processTemplate ({ TEMPLATE_PATH=>"/etc/sysctl.conf" });

system('/sbin/sysctl', '-p') == 0 or warn "sysctl exited non-zero\n";

exit (0);
