#!/usr/bin/perl

my $DEBUG=0;
my $AP_CONTACT="/etc/default/contact.html";
my $AP_MESSAGE="/var/etc/www/ap_message.html";

# Read configuration information
my $config='/etc/default/description';
my $template='/var/www/router.inc.html';

my $cmd_iwconfig='/sbin/iwconfig';
my $cmd_ip_addr='/sbin/ip addr';
my $cmd_ip_neigh='/sbin/ip neigh';

my $hostname;
my %values;
my $essid;
my $wlan_name;
my $wlan2_name;
my $wlan_ip;
my $client_ip;
my $wlan_netmask;
my $client_iface_addr;
my $client_iface_mask;

my $ip_addresses;

# Initializes variables from the environment or from the debugging environment
sub init() {
    if ($DEBUG) {
	$client_ip = "192.168.152.6";
	$hostname = "billa";
	$config = './description';
	$cmd_iwconfig = 'cat test-iwconfig';
	$cmd_ip_addr = 'cat test-ip-addr';
	$cmd_ip_neigh = 'cat test-ip-neigh';
    } else {
	$client_ip = $ENV{REMOTE_ADDR};
	$hostname = `hostname`;
    }
}

sub readConfig {
    my $config;
    ( $config ) = @_;
    open CONFIG, "< $config" or die;
    while (<CONFIG>) {
	my $line=$_;
	if ($line =~ /(\w+)=(\"(.+)\"|([^\"])+)/) {
	    $values{$1}="$3";
        }
    }
    close CONFIG;
}

sub matches($$$) {
    my ($iface, $mask, $ip) = @_;
    
    my @iface_addr = split /\./, $iface;
    my @client_addr = split /\./, $ip;
    
    my $netmask_string = "";
    
    for ($i = 0; $i < @iface_addr; $i++) {
	my $part_mask;
	
	if ($mask >= 8) {
	    $part_mask = 255;
	} elsif ($mask <= 0) {
	    $part_mask = 0;
	} else {
	    $part_mask = (255 << (8 - $mask)) & 255;
	}
	if ($netmask_string ne "") {
	    $netmask_string .= "." .$part_mask;
	} else {
	    $netmask_string = $part_mask;
	}
	$mask -= 8;
	my $iface_part = (0 + $iface_addr[$i]) & (0 + $part_mask);
	my $client_part = (0 + $client_addr[$i]) & (0 + $part_mask);
	
	if ($iface_part != $client_part) {
	    return 0;
	}	
    }
    $client_iface_mask = $netmask_string;
    return 1;
}

sub capitalize (\$) {
   ${$_[0]} = ucfirst (lc ${$_[0]});
}


#################################################################
# MAIN
init();
readConfig($config);
$essid = $values{'client_essid'};
$essid2 = $values{'client_essid2'};

# Determine wlan's name for clients
`$cmd_iwconfig 2> /dev/null`=~/(wlan.).*\"$essid\"/;
$wlan_name=$1;

# Add bridge suffix, if there's a client bridge
if (`$cmd_ip_addr` =~ /($wlan_name)br/) {
    $wlan_name .= 'br';
}

if ($essid2 ne "") {
    `$cmd_iwconfig 2> /dev/null`=~/(wlan.).*\"$essid2\"/;
    $wlan2_name=$1;
}

# Determine the IP address:
my $wname;

open IP_ADDR, "$cmd_ip_addr |";
while (<IP_ADDR>) {
    my $line = $_;
    if ($line =~ /^[0-9]+: (\w+):/) {
	$wname = $1;
    } elsif ($line =~ /inet *([0-9.]+)\/([0-9]+)/) {
	my $ip=$1;
	my $mask=$2;
	
	if (matches($ip, $mask, $client_ip)) {
	    $client_iface_addr = $ip;
	    last;
	}
    }
}

my $head='head.html';
my $template='router.inc.html';
my $capname=`hostname`;
my $hostname=`hostname`;

capitalize ($capname);
$values{"router_name"} = $capname;
$values{"dns_name"} = "$hostname.klfree.czf";
$values{"hostname"} = $hostname;
$values{"client_ip"} = $client_ip;
$values{"client_netmask"} = $client_iface_mask;
$values{"client_gateway"} = $client_iface_addr;
$values{"client_essid"} = $client_essid;
$values{"ap_traffic_6"} = "${hostname}_klfree_czf_$wlan_name-6h.gif";
if (defined $wlan2_name) {
    $values{"ap_traffic2_6"} = "${hostname}_klfree_czf_$wlan2_name-6h.gif";
}
if (-r $AP_CONTACT) {
    $values{"ap_contact_html"} = `cat $AP_CONTACT`;
} else {
    $values{"ap_contact_html"} = $values{ap_master};
}
if (-r $AP_MESSAGE) {
    $values{"message_html"} = `cat $AP_MESSAGE`;
}
print "Content-Type: text/html\n\n";

open HEAD, "< $head";
LINES: while (<HEAD>) {
    my $line=$_;
    while ($line =~ /\$(\w+)/) {
	my $name=$1;
	
	if ($name =~ /^if_(.*)/) {
	    if ($1 eq "end") {
		$suppress = 0;
	    } else {
		$suppress = ! $values{$1};
	    }
	    next LINES;
	}
    	my $val=$values{$name};
	$line =~ s/\$$name/$val/g;
    }
    if (!$suppress) {
        print $line;
    }
}

open TEMPLATE, "< $template";
my $suppress = 0;
LINES: while (<TEMPLATE>) {
    my $line=$_;
    while ($line =~ /\$(\w+)/) {
	my $name=$1;
	
	if ($name =~ /^if_(.*)/) {
	    if ($1 eq "end") {
		$suppress = 0;
	    } else {
		$suppress = ! $values{$1};
	    }
	    next LINES;
	}
    	my $val=$values{$name};
	$line =~ s/\$$name/$val/g;
    }
    if (!$suppress) {
        print $line;
    }
}
close TEMPLATE;
