#!/usr/bin/perl

require '/var/smoothwall/header.pl';

@configtypenames = ( 'GREEN (ISDN/Modem on RED)',
	'GREEN + ORANGE (ISDN/Modem on RED)', 'GREEN + RED',
	'GREEN + ORANGE + RED' );
$netaddresschange = 0;
$netdriverchange = 0;
%nics = 
(
	'3c509' => '3Com EtherLink III',
	'3c501' => '3Com 3c501',
        '3x515' => '3Com ISA EtherLink XL',
	'3c503' => '3Com 3c503 and 3c503/16',
	'3c505' => '3Com EtherLink Plus (3c505)',
	'3c507' => '3Com EtherLink 16',
	'3c59x' => '\"Corkscrew\" 3Com EtherLink PCI III/XL; Vortex (3c59x); Boomerage (3c90x)',
	'ne-2kpci' => 'PCI NE2000 cards, RealTEk RTL-8029, Winbond 89C940, etc',
	'ne' => 'NE1000 / NE2000 (non-pci)',
	'de4x5' => 'EtherWORKS DE425 TP/COAX EISA, DE434 TP PCI, DE435/450, DE500 10/100 PCI',
	'tulip' => 'Digital 21x4x Tulip PCI ethernet cards, etc.',
	'eepro100' => 'Intel i82557/i82558 PCI EtherExpressPro',
	'eepro' => 'Intel i82595 ISA EtherExpressPro10/10+ driver',
	'eexpress' => 'Intel EtherExpress 16 (i82586)',
	'lance' => 'AMD LANCE/PCnetAllied Telesis AT1500, HP J2405A, NE2100, NE2500',
	'pcnet32' => 'AMD PCnet32 and AMD PCnetPCI',
	'82596' => 'Apricot 680x0 VME, 82596 chipset',
        'e2100' => 'Cabletron E2100 seies ethercards',
	'cs89x0' => 'Crystal LAN CS8900/CS8920',
	'tlan' => 'Compaq Netelligent 10/100 TX PCI UTP, etc',
	'hp100' => '"100VG-AnyLan Network Adapters, HP J2585B, J2585A, J2970, J2973, J2573, etc',
	'via-rhine' => 'VIA Rhine PCI Fast Ethernet, etc',
	'depca' => 'DIGITAL DEPCA & EtherWORKS,DEPCA, DE100, etc',
	'dgrs' => 'Digi Intl. RightSwitch SE-X EISA and PCI',
	'ewrk3' => 'EtherWORKS 3 (DE203, DE204and DE205',
	'fmv18x' => 'Fujitsu FMV-181/182/183/184',
	'hp-plus' => 'HP PCLAN/plus',
	'hp' => 'HP LAN ethernet',
	'eth16i' => 'ICL EtherTeam 16i/32',
	'ni5010' => 'MiCom-Interlan NI5010 ethercard',
	'ni52' => 'NI5210 card (i82586 Ethernet chip)',
	'ni65' => 'NI6510, ni6510 EtherBlaster',
	'rtl8139' => 'RealTek cards using RTL8129 or RTL8139 Fast Ethernet chipsets',
	'smc-ultra' => 'SMC Ultra and SMC EtherEZ ISA ethercard (8K, 83c790)',
	'smc-ultra32' => 'SMC Ultra32 EISA ethernet card (32K)',
	'smc9194' => 'SMC 9000 series of ethernet cards',
	'wd' => 'PureData PDUC8028,WD8003 and WD8013 \"compatible\" ethercards'
);

while ()
{
	$choice = &firstmenu();

	if ($choice == 1) {
		&configtypemenu(); }
	elsif ($choice == 2) {
		&greenaddressmenu(); }
	elsif ($choice == 3) {
		&otheraddressmenu(); }
	elsif ($choice == 4) {
		&drivermenu(); }
	elsif ($choice == 0) {
		last; }
}

if ($netaddresschange)
{
	system('/etc/rc.d/rc.netaddress.down');
	system('/etc/rc.d/rc.netaddress.up');
}

exit 0;

sub firstmenu
{
	my $choice;
	my %settings;

	&readhash("${swroot}/ethernet/settings", \%settings);

	print <<END

MAIN MENU

Mode: $configtypenames[$settings{'CONFIG_TYPE'}]
Address changes: $netaddresschange
Driver changes: $netdriverchange

1. Choose network card configuration type
2. GREEN IP address settings
3. Address information for additional cards
4. Additional card drivers and card assignments


0. QUIT

END
;

	print "Select: ";

	$choice = <>;
	chomp $choice;

	return $choice;
}

sub configtypemenu
{
	my %settings;
	my $c;

	&readhash("${swroot}/ethernet/settings", \%settings);

	print "\n\n\nCONFIGURATION TYPE MENU\n\n";

	for ($c = 0; $c < 4; $c++) {
		print "$c. $configtypenames[$c]\n"; }

	print "\nSelect: ";

	$choice = <>;
	chomp $choice;

	$settings{'CONFIG_TYPE'} = $choice;

	&writehash("${swroot}/ethernet/settings", \%settings);
}

sub greenaddressmenu
{
 	my %settings;

	&readhash("${swroot}/ethernet/settings", \%settings);

	print "\n\nGREEN IP address settings\n\n";

	if (&changeaddress(\%settings, 'GREEN', 0)) {
		$netaddresschange = 1; }

	&writehash("${swroot}/ethernet/settings", \%settings);
}

sub otheraddressmenu
{
	my %settings;
	my $choice;

	&readhash("${swroot}/ethernet/settings", \%settings);

	while ()
	{
		print "\n\nADDITIONAL CARD ADDRESS SETTINGS\n\n";

		if ($settings{'CONFIG_TYPE'} == 1 ||
			$settings{'CONFIG_TYPE'} == 3)
		{
			print "1. Orange network\n";
		}
		if ($settings{'CONFIG_TYPE'} == 2 ||
			$settings{'CONFIG_TYPE'} == 3)
		{
			print "2. Red address\n";
		}
		print "0. Done\n";
		print "\nSelect: ";
	
		$choice = <>; chomp $choice;

		if ($choice == 1)
		{
			print "\nORANGE IP address settings\n\n";
	
			if (&changeaddress(\%settings, 'ORANGE', 0)) {
				$netaddresschange = 1; }
		}
		elsif ($choice == 2)
		{
			print "\nRED IP address settings\n\n";
			
			if (&changeaddress(\%settings, 'RED', 1)) {
				$netaddresschange = 1; }
		}
		elsif ($choice == 0) {
			last; }
	}

	&writehash("${swroot}/ethernet/settings", \%settings);
}

sub changeaddress
{
        my $settings = $_[0];
	my $colour = $_[1];
	my $dhcpflag = $_[2];
	my $ret = 0;
	my $temp;
	my $colouraddress;
	my $colournetaddress;
	my $colournetmask;
	my $colourdhcp;

	$colouraddress = "${colour}_ADDRESS";
	$colournetaddress = "${colour}_NETADDRESS";
	$colournetmask = "${colour}_NETMASK";
	$colourdhcp = "${colour}_DHCP";

	print "IP address [$settings->{${colouraddress}}] : ";
	$temp = <>; chomp $temp;
	if ($temp) { $settings->{${colouraddress}} = $temp; $ret = 1; }

	print "Network address [$settings->{${colournetaddress}}] : ";
	$temp = <>; chomp $temp;
	if ($temp) { $settings->{${colournetaddress}} = $temp; $ret = 1; }

	print "Netmask [$settings->{${colournetmask}}] : ";
	$temp = <>; chomp $temp;
	if ($temp) { $settings->{${colournetmask}} = $temp; $ret = 1; }

	if ($dhcpflag)
	{
		print "DHCP [$settings->{${colourdhcp}}] : "; 
		$temp = <>; chomp $temp;
		if ($temp) { $settings->{${colourdhcp}} = $temp; $ret = 1; } 
	}
	
	return $ret;
}

sub drivermenu
{
	my $settings;
	my $choice;
	my $colourdriver;

	&readhash("${swroot}/ethernet/settings", \%settings);

	print "\n\nDRIVER MENU\n\n";

	print "Current settings:\n\n";

	&shownicdetails(\%settings, 'GREEN');
	if ($settings{'CONFIG_TYPE'} == 1 ||
		$settings{'CONFIG_TYPE'} == 3) 
	{
		&shownicdetails(\%settings, 'ORANGE'); 
	}
	if ($settings{'CONFIG_TYPE'} == 2 ||
		$settings{'CONFIG_TYPE'} == 3)
	{
		&shownicdetails(\%settings, 'RED');
	}

	print "\n";
	print "1. Change drivers\n";
	print "0. Done\n\n";
	print "Select: ";

	$choice = <>;

	if ($choice == 1)
	{
		&changedrivers();
		$netdriverchange = 1;
	}
}

sub shownicdetails
{
	my $settings = $_[0];
	my $colour = $_[1];

	$colourdriver = "${colour}_DRIVER";

	print "$colour driver: ";
	print "$nics{$settings{$colourdriver}} ($settings{$colourdriver})\n";
}

sub changedrivers
{
	my $settings;
	my $key;
	my $countofcards;

	&readhash("${swroot}/ethernet/settings", \%settings);

	system('/etc/rc.d/rc.netaddress.down', 'NOTGREEN');

	print "Removing ORANGE and RED modules...\n";
	
	for $key (keys %nics)
	{
		if ($key ne $settings{'GREEN_DRIVER'})
		{
			if (&ismoduleloaded($key))
			{
				print "Removing $key\n";
				system('/sbin/rmmod', $key);
			}
		}
	}

	print "Done removing uneeded modules...\n";
	print "Availabile interfaces: $countofcards\n";
}

sub countcards
{
	my $cardcount = 0;

	open (FILE, '/proc/net/dev');

	while (<FILE>)
	{
		if (/^\W*eth[0-9]+:/) {
			$cardcount++; }
	}

	close FILE;

	return $cardcount;
}

sub ismoduleloaded
{
	my $module = $_[0];
	my $ret = 0; 

	open (FILE, '/proc/modules');

	while (<FILE>)
	{
		/^(\w+)/;
		if ($module eq $1)
		{
			$ret = 1;
			last;
		}
	}

	close FILE;

	return $ret;
}