#!/usr/bin/perl

my $CFG_DIR 	= '/etc/nw';
my @ESSIDS 	= ('wlanWORK', 'wlanHOME'); 

my $match	= '';


print "[NW] NetWrapper v.99beta\n";

my $output = `iwlist scan 2>/dev/null | grep ESSID`;
while ($output =~ /ESSID:"(.*?)"/g){
	my $essid = $1;

	for (@ESSIDS){
		if ($essid eq $_){
			$match = $essid;
			break;
		}
	}
}

$match = 'default' unless $match;

print "[NW] \@$match\n";
print "[NW] Executing script $CFG_DIR/$match...\n";
# run script in CFG_DIR
system ("$CFG_DIR/$match");

if ($? == -1) {
    print "[MW] Failed to execute: $!\n";
}else{
	print "[NW] Done. Enjoy!\n";
}