#!/usr/bin/perl

# Copyright 2012 - BSDGuides.org
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use strict;
use warnings;

my $config_file = "/etc/wireless.cfg";
sub configure_wlan;
sub dhclient;
sub ifdown;

die("You need root privileges to run $0\n") unless $> eq 0;
die("Could not open $config_file\n") unless -e -r $config_file;
die("$0 wants to run under OpenBSD sorry\n") unless $^O eq "openbsd";

system('clear');

my %configuration;
my $current_config;
my $current_wlan;
my @allowed_config_keys = ("interface","wired_interface","wireless_interface","nwid","wpakey","name","type");

open FILE,"<",$config_file or die("Could not open $config_file\n");

while(<FILE>){
	chomp;
	next if($_ =~ m/^\s*$/);
	next if($_ =~ m/^#/);

	if($_ eq "[config]"){
		$current_config = "config";
		next;
	}
	if($_ eq "[network]"){
		$current_config = "network";
		$current_wlan += 1;
		next;
	}

	die("Config error: We need a format of key=value on Line $.\n") unless $_ =~ m/^\w+=.+$/;

	my ($key,$value) = split("=",$_);
	die("Config error: Unknown key: $key on Line $.\n") unless grep $_ eq $key, @allowed_config_keys;

	if($current_config eq "config"){
		$configuration{$current_config}{$key} = $value;
	} elsif($current_config eq "network"){
		$configuration{$current_config}{$current_wlan}{$key} = $value;
	}
}
close(FILE);

my %config = %{$configuration{'config'}};
my %networks = %{$configuration{'network'}};

my $wireless_interface = $config{'wireless_interface'} || $config{'interface'}
	or die("Could not find name of wireless interface");
my $wired_interface = $config{'wired_interface'};

print "Listing available networks:\n";
print "-" x 50 . "\n\n";

printf("%-20s %-20s %-10s\n","Network Name","SSID","Type");
foreach my $index (keys %networks){
	my %network = %{$networks{$index}};
	printf("%-20s %-20s %-10s\n",$network{"name"},($network{"nwid"} || "-"),$network{"type"});
}

print "\n";
print "-" x 50 . "\n";
print "Use ./wireless YOUR_NETWORK_NAME to connect\n\n";

if(@ARGV){
	my $command = shift(@ARGV);
	my %connect;
	foreach my $index (keys %networks){
		my %network = %{$networks{$index}};
		if(uc($network{"name"}) eq uc($command)){
			%connect = %network;
		} else {
			next;
		}
	}
	if(scalar(keys %connect) > 0){
		if ($connect{'type'} eq 'wired') {
			die ("Could not find name of wired interface") unless $wired_interface;
			ifdown($wireless_interface);
			dhclient($wired_interface);
		} else {
			ifdown($wired_interface) if $wired_interface;
			configure_wlan($wireless_interface,$connect{'nwid'},$connect{'wpakey'},$connect{'type'});
			dhclient($wireless_interface);
		}
	} else {
		print "Network $command not found\n";
	}
}

sub configure_wlan {
	my ($interface, $nwid, $wpakey,$type) = @_; 
	print "Configuring SSID $nwid on $interface\n";
	if(lc($type) eq 'open'){
		system("ifconfig $interface nwid \"$nwid\" -wpakey");
	} elsif(lc($type) eq 'wpa'){
		system("ifconfig $interface nwid \"$nwid\" wpakey \"$wpakey\"");
	} else {
		die("Unknown Type: $type\n");
	}
}

sub dhclient {
	my $interface = shift;
	print "Getting an IP address on $interface via DHCP\n";
	system("dhclient $interface");
}

sub ifdown {
	my $interface = shift;
	print "Bringing $interface down\n";
	system("ifconfig $interface down");
}
