<?php
function urlExists($url=NULL)
{
	if($url == NULL) return false;
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_TIMEOUT, 5);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$data = curl_exec($ch);
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	if($httpcode>=200 && $httpcode<300){
		return true;
	} else {
		return false;
	}
}

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

if(urlExists("http://tuesday.csail.mit.edu:4242/ping")) {
  header("Location: http://tuesday.csail.mit.edu:4242");
  die();
} else if(urlExists("http://wednesday.csail.mit.edu:4242/ping")) {
  header("Location: http://wednesday.csail.mit.edu:4242");
  die();
} else if(urlExists("http://friday.csail.mit.edu:4242/ping")) {
  header("Location: http://friday.csail.mit.edu:4242");
  die();
} else {
	header('HTTP/1.1 502 Bad Gateway', true, 502);
	die();
}

?>
