<?php
	require("settings.php");
	require_once("helper.php");
	require_once("test_helper.php");
	require_once("compile_test.php");
	require_once("run_test.php");

	session_start();
	html_start("Run test");

	if(!session_is_registered("id") || $_SESSION["id"] == "") {
		echo "Not logged in";
		login_form("index.php");
		die();
	}

	echo "<H1>Run test</H1>\n";
	ob_implicit_flush(true);
	@ob_end_flush();

	$link = db_connect();
	$sid = intval($_REQUEST["sid"]);

	$out = prepare_test($sid, $link);
	if($out["interror"] != 0) {
		echo "Setup of test enviroment failed with: {$out["stderr"]}<BR>\n";
	} else {
		echo "Compile test:<BR>\n";
		$compile_out = compile_test($out["dirname"], $out["sourcefile"], 1, $sid, $link);

		if(($compile_out["interror"] == 0) && ($compile_out["retval"] == 0)) {
			echo "Run tests:<BR>\n";
			$tid = tid_from_sid($sid);
			$query = "SELECT testno FROM tests WHERE tid = $tid";
			$result = mysql_query($query, $link) or die("Can't pick run tests: ".mysql_error($link));
			while($line = mysql_fetch_array($result)) {
				run_test($out["dirname"], $out["classname"], $line["testno"], $sid, $link);
				flush();
			}
		}

		cleanup_dir($out["dirname"]);
	}
	mysql_close($link);

	$tid = tid_from_sid($sid);
	echo "<A href=\"assignment.php?tid=$tid\">View assignment</A><BR>\n";

	echo "<DIV class=\"footer\">\n";
	echo "<A href=\"index.php\"><IMG src=\"icons/house.png\" alt=\"home\">Home</A>\n";
	echo "<A href=\"logout.php\"><IMG src=\"icons/door_in.png\" alt=\"logout\">Logout</A>\n";
	echo "<DIV>\n";
?>
 </BODY>
</HTML>
