#!/usr/local/bin/perl
# edit_pool.cgi
# Edit ranges and other options in an address pool

require './dhcpd-lib.pl';
require './params-lib.pl';
&ReadParse();
$conf = &get_config();
if ($in{'sidx'} ne "") {
	$sha = $conf->[$in{'sidx'}]; 
	$sub = $sha->{'members'}->[$in{'uidx'}];
	}
else { $sub = $conf->[$in{'uidx'}]; }

# check acls
%access = &get_module_acl();
&error_setup("<blink><font color=red>$text{'eacl_aviol'}</font></blink>");
&error("$text{'eacl_np'} $text{'eacl_pss'}") if !&can('r',\%access,$sub);

# display
if ($in{'new'}) {
	&header($text{'pool_create'}, "");
	}
else {
	&header($text{'pool_edit'}, "");
	$pool = $sub->{'members'}->[$in{'idx'}];
	}
if ($sub->{'name'} eq 'subnet') {
	print "<center><font size=+1>",&text('ehost_subnet',
	    $sub->{'values'}->[0], $sub->{'values'}->[2]),"</font></center>\n";
	}
else {
	print "<center><font size=+1>",&text('ehost_shared',
		    $sub->{'values'}->[0]),"</font></center>\n";
	}
print "<hr>\n";

print "<form action=save_pool.cgi method=post>\n";
print "<input type=hidden name=new value='$in{'new'}'>\n";
print "<input type=hidden name=idx value='$in{'idx'}'>\n";
print "<input type=hidden name=uidx value='$in{'uidx'}'>\n";
print "<input type=hidden name=sidx value='$in{'sidx'}'>\n";
print "<table border width=100%>\n";
print "<tr $tb> <td><b>$text{'pool_header'}</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

@range = $pool ? &find("range", $pool->{'members'}) : ();
print "<tr> <td valign=top><b>$text{'esub_arange'}</b></td> <td colspan=3>\n";
for($i=0; $i<=@range; $i++) {
	$r = $range[$i];
	local $dyn = ($r->{'values'}->[0] eq "dynamic-bootp");
	printf "<input name=range_low_$i size=15 value=\"%s\"> - \n",
		$r->{'values'}->[$dyn];
	printf "<input name=range_hi_$i size=15 value=\"%s\">&nbsp;\n",
		$r->{'values'}->[$dyn+1];
	printf "<input type=checkbox name=range_dyn_$i value=1 %s>\n",
		$dyn ? "checked" : "";
	print "$text{'esub_dbooptpq'}<br>\n";
	}
print "</td> </tr>\n";

print "<tr> <td valign=top><b>$text{'pool_allow'}</b></td>\n";
print "<td><textarea name=allow rows=4 cols=25>",
	join("\n", map { $_->{'text'} } &find("allow", $pool->{'members'})),
	"</textarea></td>\n";

print "<td valign=top><b>$text{'pool_deny'}</b></td>\n";
print "<td><textarea name=deny rows=4 cols=25>",
	join("\n", map { $_->{'text'} } &find("deny", $pool->{'members'})),
	"</textarea></td> </tr>\n";

&display_params($pool->{'members'});

print "</table></td></tr></table>\n";
print "<table width=100%><tr>\n";
if ($in{'new'}) {
	print "<td><input type=submit value='$text{'create'}'></td>\n"
		if &can('rw',\%access,$sub);
	}
else {
	print "<td><input type=submit value='$text{'save'}'></td>\n"
		if &can('rw',\%access,$sub);
	print "<td align=right><input type=submit name=delete ",
	      "value='$text{'delete'}'></td>\n" if &can('rw',\%access,$sub);
	}
print "</tr></table>\n";
print "</form><hr>\n";
&footer("edit_subnet.cgi?sidx=$in{'sidx'}&idx=$in{'uidx'}",
	$text{'pool_return'});

