<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" title="Default" href="main.css" type="text/css" />
</head>
<?
	$lname = $request->get("name","ERROR");
?>
<body>
<h2 align="center">Launcher <?print $lname; ?> Details</h2>
<hr/>
<?

my $act = $request->get("action", "none");
if($act eq "Add") {
	my $dynamics = $request->getInt("dynamics", "0");
	if($lname eq "" || $lname eq "ERROR") {
		print "Missing launcher name in Add action.";
	} elsif($dynamics < 0) {
		print "Invalid dynamics count in Add action.";
	} else {
		print "Added launcher '$lname' with $dynamics dynamic zones.";
		$EQW->CreateLauncher($lname, $dynamics);
	}
	print "<hr/>";
}

$config = $EQW->GetLauncher($lname);
if(!$config) {
	print "<h2>Unable to find launcher $lname</h2>";
}


if($act eq "Boot") {
	my $zone = $request->get("zone", "none");
	my $port = $request->getInt("port", 0);
	if($lname eq "ERROR" || $zone eq "none" || $zone eq "" || $port < 0 || $port > 65535) {
		print "Invalid zone, port, or launcher name in Boot action.";
	} else {
		if(!$config->BootStaticZone($zone, $port)) {
			print "Failed to boot '$zone' on launcher $lname with port $port. Invalid zone?";
		} else {
			print "Booting new zone $zone on launcher $lname with port $port.";
		}
	}
	print "<hr/>";
} elsif($act eq "Change") {
	my $count = $request->getInt("count", 0);
	if($lname eq "ERROR" || $count < 0) {
		print "Invalid launcher name or count";
	} else {
		$config->SetDynamicCount($count);
		print "Changed dynamic count to $count. Notifying launcher.";
	}
	print "<hr/>";
} elsif($act eq "remove") {
	my $zone = $request->get("zone", "none");
	if($lname eq "ERROR" || $zone eq "none" || $zone eq "") {
		print "Invalid zone or launcher name in $act action.";
	} else {
		if(!$config->DeleteStaticZone($zone)) {
			print "Failed to remove '$zone' on launcher $lname. Invalid zone?";
		} else {
			print "Removed $zone from launcher $lname";
		}
	}
	print "<hr/>";
} elsif($act eq "restart") {
	my $zone = $request->get("zone", "none");
	if($lname eq "ERROR" || $zone eq "none" || $zone eq "") {
		print "Invalid zone or launcher name in $act action.";
	} else {
		$config->RestartZone($zone);
		print "Sent restart for $zone to launcher $lname";
	}
	print "<hr/>";
} elsif($act eq "start") {
	my $zone = $request->get("zone", "none");
	if($lname eq "ERROR" || $zone eq "none" || $zone eq "") {
		print "Invalid zone or launcher name in $act action.";
	} else {
		$config->StartZone($zone);
		print "Sent start for $zone to launcher $lname";
	}
	print "<hr/>";
} elsif($act eq "kill") {
	my $zone = $request->get("zone", "none");
	if($lname eq "ERROR" || $zone eq "none" || $zone eq "") {
		print "Invalid zone or launcher name in $act action.";
	} else {
		$config->StopZone($zone);
		print "Sent stop for $zone to launcher $lname";
	}
	print "<hr/>";
} elsif($act eq "rebootall") {
	foreach my $z($config->ListZones()) {
		$config->RestartZone($z);
	}
	print "Restarted all zones.\n<hr/>";
} elsif($act eq "stopall") {
	foreach my $z($config->ListZones()) {
		$config->StopZone($z);
	}
	print "Stopped all zones.\n<hr/>";
} elsif($act eq "startall") {
	foreach my $z($config->ListZones()) {
		$config->StartZone($z);
	}
	print "Started all zones.\n<hr/>";
}

?>
<div align="center">
<a href="?name=<? print $lname; ?>">Reload</a> -
<a href="?name=<? print $lname; ?>&action=rebootall">Reboot All Zones</a> -
<a href="?name=<? print $lname; ?>&action=stopall">Stop All Zones</a> -
<a href="?name=<? print $lname; ?>&action=startall">Start All Zones</a>
</div>
<hr/>
<table border="0">
<tr>
	<td>
	<form action="?" method="POST">
	<input type='hidden' name='name' value='<?print $lname; ?>'>
	Add:
	Zone Short Name: <input type='text' name='zone'>
	Port: <input type='text' name='port' value='0' size='5'>
	<input type='submit' name='action' value='Boot'>
	</form>
	</td>
</tr>
<tr>
	<td>
	<form action="launcher.html" method="POST">
	<input type='hidden' name='name' value='<?print $lname; ?>'>
	Change Dynamic Count: <input type='text' name='count' value='<? print $config->GetDynamicCount(); ?>' size='5'>
	<input type='submit' name='action' value='Change'>
	</form>
	</td>
</tr>
</table>
<hr/>
<table width="100%"  border="1" cellspacing="2" cellpadding="3" class="zonelist">
  <tr>
    <th scope="col">Zone Name</th>
    <th scope="col">State</th>
    <th scope="col">Port</th>
    <th scope="col">Start Count</th>
    <th scope="col">Actions</th>
  </tr>
<?
  my @zones = $config->ListZones();

  for my $zone (sort @zones) {
		my $z = $config->GetZoneDetails($zone);
		if(!$z) {
			$zone->{name} = "ERROR: no launcher";
		} elsif($z->{error}) {
			$zone->{name} = "ERROR: $z->{error}";
		}

		print "<tr>\n";
		print "\t<td><a href='zone.html?zone=$z->{name}'>$z->{name}</a></td>";
		if($z->{up}) {
			print "<td><font color='#009900'>UP</font></td>";
		} else {
			print "<td><font color='#CC0000'>DOWN</font></td>";
		}
		if($z->{port} == 0) {
			print "<td>Dynamic</td>";
		} else {
			print "<td>$z->{port}</td>";
		}
		print "<td>$z->{starts}</td>";
		print "<td>";
		if($config->IsConnected()) {
			print "<a href='?action=restart&name=$lname&zone=$z->{name}'>Restart</a> - ";
			if($z->{up}) {
				print "<a href='?action=kill&name=$lname&zone=$z->{name}'>Stop</a>";
			} else {
				print "<a href='?action=start&name=$lname&zone=$z->{name}'>Start</a>";
			}
			if($z->{name} !~ /dynamic/) {
				print " - ";
			}
		}
		if($z->{name} !~ /dynamic/) {
			print "<a href='?action=remove&name=$lname&zone=$z->{name}'>Remove</a>";
		}
		print "</td>\n";
		print "</tr>\n";
  }
?>
</table>

</body>
</html>
