<?php
ini_set("display_startup_errors","1");
ini_set("display_errors","1");
error_reporting(E_ALL);

require "mysqlconnect.inc.php";
require "tools.php";

/******* Init the interface  ******/
session_start(); //web side
constructor();

/******* initialize the php interface on the compute-side *********/
function constructor(){

	$dbhost = $GLOBALS['dbhost'];
	$dbuser = $GLOBALS['dbuser'];
	$dbpass = $GLOBALS['dbpass'];
	$database = $GLOBALS['database'];
	$jobtable = $GLOBALS['jobtable'];
	$commandtable = $GLOBALS['commandtable'];
	$machinetable = $GLOBALS['machinetable'];
	

	
	//Test the database and the availability of the table and stuff	
	if (!fnDatabaseExists($database)){
		$query = "CREATE DATABASE $database";
		if(mysql_query($query))
			print("Database created \n");
		else
			die ("Could not Create Database, exiting!" . mysql_error()."\n");
	}

	if (!fnTableExists($machinetable))
	{
		$fields = extractFields($machinefields);
		$query="CREATE TABLE $machinetable (".extractFields($GLOBALS['MACHINE_TABLE_FIELDS']).")";
		print($query);
		if(mysql_query($query))
			print("Table created \n");	
		else
			print ("Could not Create the Machine Table, exiting!  " . mysql_error()."\n");
	}


	if (!fnTableExists($jobtable))
	{
		$foreignkey = $machinetable."_id";
		$query="CREATE TABLE $jobtable (".extractFields($GLOBALS['JOB_TABLE_FIELDS']).")";

		if(mysql_query($query))
			print("Table created \n");	
		else
			die ("Could not Create the Jobs Table, exiting!" . mysql_error()."\n");
	}
	if (!fnTableExists($commandtable))
	{
		$query="CREATE TABLE $commandtable (".extractFields($GLOBALS['COMMAND_TABLE_FIELDS']).")";

		if(mysql_query($query))
			print("Table created \n");	
		else
			die ("Could not Create the Jobs Table, exiting!" . mysql_error()."\n");
	}

}


?>
