#!/usr/bin/perl -w
####################################################################
###  This script will record the last 5 minutes setupfile        ###
###  It will log ESSID, channel and KEY(if have)                 ###
###  TEST distribution : Red Hat 7.3 kernel 2.4.18-3             ###
###  Interface : CardBus                                         ###
####################################################################

########### get essid value ###########
#check ethX from 0 to 2 
$_ = `iwconfig eth0`;
$eth = "eth0";
if ($_ !~ /eth0      IEEE 802.11-DS/)
{
	$_ = `iwconfig eth1`;
	$eth = "eth1";
	if ($_ !~ /eth1      IEEE 802.11-DS/)
	{
		$_ = `iwconfig eth2`;
		$eth = "eth2";
		if($_ !~ /eth2      IEEE 802.11-DS/)
		{	
		#	print STDERR "We can not find the proper WLAN\n";
			exit();	
		}
	}
}
if (/ESSID:"(.+)"  Nickname:/)
{
	$essid = $1;	
}else
{
	# do not log any thing
	exit();	
}
if(/Mode:Ad-Hoc/)
{
	#do not log any thing in Ad-Hoc mode
	exit();
}
########### get channel value ###########

$channel = 0;
if(/2\.412GHz/)
{
	$channel = 1;
}
elsif(/2\.417GHz/)
{
	$channel = 2;
}
elsif(/2\.422GHz/)
{
	$channel = 3;
}
elsif(/2\.427GHz/)
{
	$channel = 4;
}
elsif(/2\.432GHz/)
{
	$channel = 5;
}
elsif(/2\.437GHz/)
{
	$channel = 6;
}
elsif(/2\.442GHz/)
{
	$channel = 7;
}
elsif(/2\.447GHz/)
{
	$channel = 8;
}
elsif(/2\.452GHz/)
{
	$channel = 9;
}
elsif(/2\.457GHz/)
{
	$channel = 10;
}
elsif(/2\.462GHz/)
{
	$channel = 11;
}
elsif(/2\.467GHz/)
{
	$channel = 12;
}
elsif(/2\.472GHz/)
{
	$channel = 13;
}
elsif(/2\.484GHz/)
{
	$channel = 14;
}
else
{
	$channel = 0;
}

########### get key value ###########

$restric = 0;#open
$nokey = 0; #default had key

$_ = `iwlist $eth key`;

if (/\*\*\*\*/)
{
 $restric = 1;
 `iwconfig $eth key open`;
 $_ = `iwlist $eth key`;
}
if(/\[1\]: (.+) \(104 bits\)/)
{
	$key1 = $1;
}elsif(/\[1\]: (.+) \(40 bits\)/)
{
	$key1 = $1;
}

if(/\[2\]: (.+) \(104 bits\)/)
{
	$key2 = $1;
}elsif(/\[2\]: (.+) \(40 bits\)/)
{
	$key2 = $1;
}

if(/\[3\]: (.+) \(104 bits\)/)
{
	$key3 = $1;
}elsif(/\[3\]: (.+) \(40 bits\)/)
{
	$key3 = $1;
}


if(/\[4\]: (.+) \(104 bits\)/)
{
	$key4 = $1;
}elsif(/\[1\]: (.+) \(40 bits\)/)
{
	$key4 = $1;
}

if($_ !~/104/ || $_ !~ /40/)
{
	$nokey = 1;
}

### change key mode to original
if ($restric == 1)
{
 `iwconfig $eth key restricted`;
}

################ modify wireless.opts #################################
open(FH,"/etc/pcmcia/wireless.opts") ||
        die "Sorry , we can not open the wireless.opts file and  the config may not exist";
$flag = 1; # for check once 
while(<FH>)
{
	$tempfile .= $_;
	if(/# ADMtek example/ && $flag == 1)
	{
		$a = <FH>;	#read *,*,*,00:00:E8:*)
		$tempfile .= $a;
		$a = <FH>;	#read INFO="ADMtek example 
		$tempfile .= $a;
		$tempfile .= "    ESSID=\"$essid\"\n";
		<FH>;           #skip orig ESSID="any"
			
		$a = <FH>;	#read  MODE="Managed"
		$tempfile .= $a;
		$a = <FH>;	#read  RATE="11M"
		$tempfile .= $a;
		
		$tempfile .= "    CHANNEL=\"$channel\"\n";
		<FH>;           #skip orig channel="0"
		
		
		#key 
		if ($nokey == 1)
		{
		  $tempfile .= "    KEY=\"off\"\n"	
		}else
		{
		  $tempfile .="    KEY=\"$key1 [1] key $key2 [2] key $key3 [3] key $key4 [4]\"\n"	
		}
		<FH>;           #skip orig KEY="off" 
		$flag = 0;	
	}
}


close(FH) || die "close: $!\n";
`rm -f /etc/pcmcia/wireless.opts`;

open(WL,">/etc/pcmcia/wireless.opts");
print WL $tempfile;
close(WL);

