#!/usr/bin/perl

# Process the portmap.cfg file
open( IN, "/etc/sysconfig/portmap.cfg" );		# Open the file
while (<IN>) {						# While there are lines in the file...
	if( $_ =~ /port/ ) {				# If this line is the line with the interface...
		s/ //g;					# Remove spaces
		chomp($_);				# Remove whitespace
		s/port://g;				# Remove "port:"
		$interface = $_				# Save it 
	}						#
	elsif ( $_ =~ /path/ ) {			# If this line is the line with a PCI address...
		s/ //g;					# Remove spaces
		chomp($_);				# Remove whitespace
		s/path:\[//g;				# Remove "path:["
		s/\]//g;				# Remove "]"
		@parts = split(":", $_);		# Pick apart the PCI address
		$address = sprintf("%02X:%02X.%X", $parts[0], $parts[1], $parts[2]);
							#
		$portmap{$address} = "PORT$interface";	#
	}						#
}							#
close( IN );						#

# Process the network device PCI to interface file	
open( IN, "/proc/net/dev_map" );			# Open the file
while (<IN>) {						# While there are lines in the file...
	@parts = split(" ", $_);			# Pick apart the PCI address
	$interface{$portmap{$parts[0]}} = $parts[1];	# Map the PCI address to the interface
}							#
close( IN );

foreach $key (keys (%interface)) {
        print "KEY: $key,  VALUE: $interface{$key}\n";
}

# Fixup the ipchains file
open( IN, "/etc/sysconfig/ipchains.in" );		# Open the file
open( OUT, ">/etc/sysconfig/ipchains" );		# Open the chains file
while (<IN>) {						# Process all lines
	foreach $key (keys (%interface)) {		# Check for each key and substitute
		if ( $key eq '' ) {			# Skip blank keys
			next;				#
		}					#
		s/$key/$interface{$key}/;		#
	}						#
	print OUT $_;					# Write the file
}							#
close( IN );						#
close( OUT );

