<?php

require '../Common/openDB.php';
require '../Common/preferences.php';

$con = mysql_connect($website,$userid,$pw);
if (!$con) {
	die('Could not connect: ' . mysql_error());
}

$curDB = "../CurrentlyOpenedDBName.tmp";
if(file_exists ($curDB)){
	$f = fopen($curDB, "r");
	$dbase   = trim(fgets($f)); // linked database name
	fclose($f);
	mysql_select_db($dbase, $con);
	//echo "Connected to " . $dbase . "<br/>";
}else{
	//No database currently open
	return;
}

echo ("
<html>
<head>
	<!-- Link to table format CSS -->
	<link rel='stylesheet' type='text/css' href='../CSS/Font.css'>
	<link rel='stylesheet' type='text/css' href='../CSS/HeaderContentFooter.css'>
	<link rel='stylesheet' type='text/css' href='../CSS/Buttons.css'>
	<link rel='stylesheet' type='text/css' href='../CSS/Miscellaneous.css'>
</head>
<body>
");

echo ("<form method='post'>");
echo ("<table>");
$res = mysql_query("SELECT count(output_id)output_id FROM Output",$con);
while ($row = mysql_fetch_array($res))$count = $row[0];
if($count > 0){		         //     0  1            2     3       4
		$res = mysql_query("SELECT name,frequency_id,status,ranupto,output_id FROM Output",$con);
		while ($row = mysql_fetch_array($res)){
			$frequency_id = $row[1];
			$freq_name = "no frequency associated";
			if($frequency_id != NULL || $frequency_id != ""){
				$res1 = mysql_query("SELECT name FROM Frequency WHERE frequency_id = {$frequency_id}",$con);
				while($row1 = mysql_fetch_array($res1))$freq_name = $row1[0];
			}
				$output_id = $row[4];
				$ranupto = $row[3];
				if($row[3] == NULL || $row[3] == '0000-00-00 00:00:00')$ranupto = "Not run at all";
				$status = $row[2];
				if($row[2] == NULL) $status = "not running";

				if($status == "not running" || $status == "not defined")$Launch = "Launch";
				if($status == "running")$Launch = "Stop";
				echo 	("<tr><td><button type='submit' {$NotSelectedRendering}>$row[0]</button></td>");
				if($freq_name != "no frequency associated"){
					//echo "<td><button type='submit' {$StandardRenderingButton} value= '" . $row[4] . "' name = 'test'  formaction='ESView.php?output_id=$output_id&name=$row[0]'>Test</button></td>";
					echo "<td><button type='submit' {$StandardRenderingButton} value= '" . $row[4] . "' name = '{$Launch}'  formaction='ESView.php?output_id=$output_id&name=$row[0]'>{$Launch}</button></td>";
					echo "<td><button type='submit' {$StandardRenderingButton} value= '" . $row[4] . "' name = 'View'  formaction='ESView.php?output_id={$output_id}&view=yes'>View Output</button></td>";
				}else{
					echo ("<td> Cannot launch ({$freq_name}) </td>");
				}

				echo("</tr>");
		}
}
echo ("</table>");
echo ("</form>");

if(isset($_POST[test])){
	echo "Test facility is still under development</body></html>";
	exit;
}

$output_id = $_GET[output_id];
$OIDDB = $output_id . $dbase;
$OIDDBsp = $dbase . " " . $output_id;

if(isset($_POST[Launch])){

	//get scheduler command
	$res2 = mysql_query("SELECT frequency_id FROM Output WHERE output_id = {$_GET[output_id]}",$con);
	while($row2 = mysql_fetch_array($res2))$frequency_id = $row2[0];
	$command = GetSchedulerCmd($frequency_id, $TimeZone, $output_id . "ES" , $dbase, $con, $oper_sys);
	if($oper_sys == "windows"){
		// Make vbs and bat wrappers
		FileCopier("DIDDB.vbs",$OIDDB.".vbs","FDC",$OIDDB);
		FileCopier("DIDDB.bat",$OIDDB.".bat","FDC",$OIDDBsp);
		if(file_exists("C:\\Program Files (x86)\\Ampps\\www\\EnTrak\\EnTrak\\4_ESView")){
			$path_str = "\"cscript \\\"C:\Program Files (x86)\\\Ampps\www\EnTrak\EnTrak\\4_ESView\\{$OIDDB}.vbs\\\"  \""; // works retain thisecho $command;
		}elseif(file_exists("C:\\Program Files\\Ampps\\www\\EnTrak\\EnTrak\\4_ESView")){
			$path_str = "\"cscript \\\"C:\Program Files\\\Ampps\www\EnTrak\EnTrak\\4_ESView\\{$OIDDB}.vbs\\\"  \""; // works retain thisecho $command;
		}else{
			echo "<h1> Error: ESView.php WAMP server not found";
			exit;
		}

		$command .= " /tr " . $path_str;
		echo $command . "<br/>";
		$output = shell_exec($command);
		echo $output . "<br/>";
	}elseif($oper_sys == "linux"){
		$path_str = shell_exec("pwd");
		$path_str = trim($path_str);
		FileCopier("DIDDB.bash","tmpfile","FDC",$OIDDBsp);
		FileCopier("tmpfile",$OIDDB."_ES.bash","PWD",$path_str);
		unlink("tmpfile");
		$command = str_replace("<br/>"," bash {$path_str}/{$OIDDB}_ES.bash \n",$command);
		add_to_cron($command);
	}
	// update status in database
	mysql_query("UPDATE Output SET status='running' WHERE output_id = {$_GET[output_id]}",$con);

	// refresh page
	header("Location: ESView.php?output_id={$_GET[output_id]}");
}
if(isset($_POST[Stop])){

	// remove files and task from scheduler/cron
	$OIDDB = $output_id . $dbase;
	if($oper_sys == "windows"){
		shell_exec("schtasks.exe /delete /f /tn {$output_id}ES{$dbase}");
		if(file_exists($OIDDB.".vbs"))unlink($OIDDB.".vbs");
		if(file_exists($OIDDB.".bat"))unlink($OIDDB.".bat");
	}elseif($oper_sys == "linux"){
		rm_from_cron($OIDDB."_ES.bash");
		if(file_exists($OIDDB."_ES.bash"))unlink($OIDDB."_ES.bash");
	}
	mysql_query("UPDATE Output SET status='not running' WHERE output_id = {$_GET[output_id]}",$con);
	header("Location: ESView.php?output_id={$_GET[output_id]}");
}

// Display xml output on page
if($_GET[view] == "yes"){
	$output_id = $_GET[output_id];
	$res = mysql_query("SELECT name FROM Output WHERE output_id = {$output_id}",$con);
	while($row = mysql_fetch_array($res))$name = $row[0];
	header("Location: ../../Outputs/{$dbase}/{$name}_{$output_id}/latest_file.xml");
}


echo ("</body></html>");
?>
