#!/usr/bin/perl -w

###############################################################################
#
# Copyright 2002 Point Clark Networks.
#
# 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.
#
###############################################################################

use strict;
use Getopt::Long;
use lib '/usr/share/system/scripts';
require 'functions';

# Usual security measures... see "perlsec" manual page for details.
#------------------------------------------------------------------

$ENV {'PATH'} = '/sbin:/usr/sbin';
$ENV {'SHELL'} = '/bin/bash';
delete $ENV {'ENV', 'BASH_ENV'};

if ($< != 0) {
	print("You must be root to update system files... exiting.\n");
	exit;
}

my $device;
my $bootproto;
my $ip;
my $netmask;
my $onboot;
my $gateway;
my %opts;

GetOptions(
	"device=s" => \$device,
	"bootproto=s" => \$bootproto,
	"ip=s" => \$ip,
	"netmask=s" => \$netmask,
	"gateway=s" => \$gateway,
	"onboot=s" => \$onboot,
);

# Sanity checking... we do not want to startup up interactive mode
#-----------------------------------------------------------------

print("Setting network\n");

if (!$device) {
	print("... skipping, no device specified\n");
	exit;
}

if (!$bootproto || (($bootproto ne "static") && ($bootproto ne "dhcp"))) {
	print("... skipping, no/invalid boot protocol specified\n");
	exit;
}

if ($bootproto eq "static") {
	if (!$ip || !$netmask) {
		print("... skipping - IP, netmask and gateway must be specified\n");
		exit;
	}

	my $f_network = "/etc/sysconfig/network-scripts/ifcfg-$device";
	my $macaddr = `/sbin/ifconfig $device | /bin/grep HWaddr | /bin/sed 's/.*HWaddr[[:space:]]*//' | /usr/bin/tr [:upper:] [:lower:]`;
	$macaddr =~ s/\s*$//;

	open(F_NETWORK, ">$f_network") or die "File open error $f_network: $!";
	print F_NETWORK "DEVICE=$device
TYPE=\"Ethernet\"
ONBOOT=\"yes\"
USERCTL=\"no\"
BOOTPROTO=\"static\"
IPADDR=\"$ip\"
NETMASK=\"$netmask\"
HWADDR=\"$macaddr\"
";

	if ($gateway) {
		print("... setting $device to $ip/$netmask with gateway $gateway\n");
		print F_NETWORK "GATEWAY=\"$gateway\"\n";
	} else {
		print("... setting $device to $ip/$netmask\n");
	}

	close(F_NETWORK);
} elsif ($bootproto eq "dhcp") {
	print("... setting $device to DHCP\n");
	my $f_network = "/etc/sysconfig/network-scripts/ifcfg-$device";
	my $macaddr = `/sbin/ifconfig $device | /bin/grep HWaddr | /bin/sed 's/.*HWaddr[[:space:]]*//' | /usr/bin/tr [:upper:] [:lower:]`;
	$macaddr =~ s/\s*$//;

	open(F_NETWORK, ">$f_network") or die "File open error $f_network: $!";
	print F_NETWORK "DEVICE=$device
TYPE=\"Ethernet\"
ONBOOT=\"yes\"
USERCTL=\"no\"
BOOTPROTO=\"$bootproto\"
HWADDR=\"$macaddr\"
";
	close(F_NETWORK);
}

if ($onboot && ($onboot eq "no")) {
	print("... setting $device to noboot (usually for PPPoE)\n");
	KeyReplace("/etc/sysconfig/network-scripts/ifcfg-$device", "ONBOOT", "=", "no");
}
