#!/usr/bin/perl -w
use strict;

###########################################################################
# (c) 2005 Brett T. Warden
# http://www.wgz.org/bwarden/
#
# License:
# Perl Artistic License
# http://www.perl.com/language/misc/Artistic.html
#
# Other licensing arrangements available upon request.
#
# Note specifically:
#  THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
#  MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
###########################################################################

use Net::Netrc;
use LWP::Simple;
use Template::Extract;
use Date::Parse;
#use Data::Dumper;

use POSIX qw(strftime);
my $date_format = '%Y-%m-%d %H:%M:%S'; #'%a %b %e %H:%M:%S %Y';

my $machine = shift(@ARGV) || 'vapor.gue.st.wgz.org'; 
my $auth = Net::Netrc->lookup($machine);
my $login = $auth->login();
my $password = $auth->password();

my $obj = Template::Extract->new;

my $template = << '.';
<table[% ... %]>[% FOREACH host %]
[% ... %]<tr[% ... %]><td[% ... %]><font[% ... %]>[% /\s*/ %][% date %][% /\s*/ %]</font></td><td[% ... %]><font[% ... %]>[% mac %]</font></td></tr>
[% ... %]
[% END %]</table>
.

my $document = get('http://'.$login.':'.$password.'@'.$machine.'/st_wireless.html');

my $clients = $obj->extract($template, $document);

#print Data::Dumper::Dumper(
#				$clients
#			   );

my $print_template = "%-30s%-17s\n";
print sprintf($print_template, 'Date', 'Mac');
for my $row (@{$clients->{host}}) {
#	print Data::Dumper::Dumper($row);
	$row->{date} =~ tr!/! !;
	$row->{date}.= ' UTC';
	$row->{date} = str2time($row->{date});
#	print sprintf($print_template, scalar(localtime($row->{date})), $row->{mac});
	print sprintf($print_template, strftime($date_format, localtime($row->{date})), $row->{mac});
}
