#!/usr/pkg/bin/perl

my $http="";
my $total="";

sub print_altq
{
	($http,$total)=@_;
print "<br><font color=\"red\">Changes saved</font></br>";
open F,">/etc/pf.conf";
print F <<"EOF";
altq on dc0 bandwidth 1Mb cbq queue { http, ftp, ssh }
queue http bandwidth $http% cbq(borrow)
queue ftp bandwidth 20% cbq(borrow)
queue ssh bandwidth 100% cbq(default)
#queue default bandwidth $total% cbq(default)

EOF
close F;
system("cat /etc/pf.conf.base >> /etc/pf.conf");
system("pfctl -f /etc/pf.conf 2>&1");
# Save config
open F,">/etc/pf.conf.qos";
print F "$http $total\n";
close F;
}

if (    (! $ENV{ARG_total} eq "" ) &&
	(! $ENV{ARG_http} eq "" ))
{
	$http=$ENV{ARG_http};
	$total=$ENV{ARG_total};
	if (    ($http<=0 || $http>100 )||
		($total<=0 || $total>100) )
	{
		print "<br><font color=\"red\">Invalid bw ($http $total)</font><br>";
	} else {
	# Actualitzar QoS
	print_altq($http,$total);
	}
} else {
	open F,"</etc/pf.conf.qos";
	$line=<F>;
	$line=~/(.*) (.*)/;
	$http=$1;
	$total=$2;
	close F;
}

sub print_select
{
	($name)=@_;
	if ($name eq "http")
	{
		$lim=$http;
	}
	if ($name eq "total")
	{
		$lim=$total;
	}
	print "<select name=\"$name\">\n";
	for ($i=0;$i<=100;$i+=10)
	{
	print "  <option ";
	if ($lim == $i)
	{	print "selected"; }
	print " value=\"$i\">$i</option>\n";
	}
	print "</select>\n";
}

print<<"EOF";
<html>
  <head>
    <title>Bandwidth Selector</title>
  </head>
<body>
 <h1>Bandwidth Selector</h1>
  <hr> Connection is about 1Mbits.<br><center>
  <form action="/index.x" method="get">
  <table>
  <tr><td>Total bandwidth:</td><td>
EOF

print_select("total");
print "</td></tr>";

print "<tr><td>HTTP bandwidth:</td><td>";
print_select("http");
print "</td></tr>";
print "<tr><td><input type=\"reset\"></td>";
print "<td><input type=\"submit\"></td></tr>";
print "</table>\n</form>";
print "</body></html>\n";
