<?php
/* Turn on buffering */
ob_start();

include_once("common.php");

/* Ensure everything is setup okay */
validate_session();

/* Clean up as needed */
sanitise_any_request();

/* Process any request */
switch ($_REQUEST['action']) {

case 'Reset Template':
	reset_session_parameters();
	break;

case 'Add To Template':
	foreach($_REQUEST['param_id'] as $key => $value) {

		if (!array_key_exists($value, $_SESSION['params']))
			$_SESSION['params'][$value] = $value;
	}

	natsort($_SESSION['params']);

	verify_selected_params();

	break;

case 'Remove From Template':
	foreach($_REQUEST['param_id'] as $key => $value) {

		if (array_key_exists($value, $_SESSION['params']))
			unset($_SESSION['params'][$value]);
	}

	verify_selected_params();

	break;

case 'Template Details':
	echo "Details " . $_REQUEST['template'];
	break;

case 'Delete Template':
	echo "Delete " . $_REQUEST['template'] . " is not implemented.  Ask Mark about it.";
	break;

case 'View Template':

  // A simple test for now...
  
  echo '<body>';
  echo '<img src="telem.php?action=Make+Graph&param1=C1_FRM_EOF"><br /><img src="telem.php?action=Make+Graph&param1=C2_FRM_EOF"><br /><img src="telem.php?action=Make+Graph&param1=C3_FRM_EOF">';
  echo '</body>';
  ob_end_flush();
  return;
  break;
  
case 'Create Template':

  echo '<body><img src="' . "telem.php?action=Make+Graph&param_id=C1_FRM_EOF" . '">';
  echo '</body>' . "\n";

	ob_end_flush();
	return;
	break;

case 'Get Data':

  $cmd = '';
  $ts = strtotime("14 days ago");
  $te = strtotime("now");
  $type = 'last';  

  // Add the list of parameters to the request
  foreach($_REQUEST as $key => $value) {
    
    if (strncmp('param', $key, 5) == 0) {
      $cmd = $cmd . " -param ".$value;
    }
    
    if ($key == 'ts') {
      
      $ts = strtotime($value);
    }
    
    if ($key == 'te') {
      
      $te = strtotime($value);
    }

    if ($key == 'type') {

      switch($value) {

      case 'last':
	 $type = 'last';
         break;

      case 'band':
	 $type = 'band';
	 break;

      case 'max':
	 $type = 'max';
	 break;

      case 'min':
	 $type = 'min';
	 break;
      }

    }
    
  }
  
  $cmd = $cmd . " -ts \"$ts\" -te \"$te\"";
  $cmd = $cmd . " -type " . $type;
  
  // We use output buffering so we can conditionally return an image or a text error
  $ret = 0;
  passthru ( "/home/mpc/Projects/SMEI/Code/web/TelemCGI/TelemData " . $cmd . " 2>&1", $ret );

  header("Content-type: text/plain");
  
  ob_end_flush();

  return;

case 'Make Graph':
  
  $cmd = '';
  $ts = strtotime("14 days ago");
  $te = strtotime("now");
  $width = 640;
  $height = 192;
  $weight = 2;
  $raw = 'off';
  $ymin = 0;
  $ymax = 0;
  $ymax_set = 0;
  $ymin_set = 0;
  $type = 'last';
  $project = 'smei';
  
  // Add the list of parameters to the request
  foreach($_REQUEST as $key => $value) {
    
    if (strncmp('param', $key, 5) == 0) {
      $cmd = $cmd . " -param ".$value;
    }
    
    if ($key == 'ts') {
      
      $ts = strtotime($value);
    }
    
    if ($key == 'te') {
      
      $te = strtotime($value);
    }

    if ($key == 'ymax') {
      
      $ymax = $value;
      $ymax_set = 1;

    }
    
    if ($key == 'ymin') {
      
      $ymin = $value;
      $ymin_set = 1;
    }
    
    if ($key == 'w') {
      
      $width = $value + 0;
    }
    
    if ($key == 'h') {
      
      $height = $value + 0;

    }
    
    if ($key == 'wt') {
      
      $weight = $value + 0;

    }

    if ($key == 'raw') {

      if ( $value == 'on' ) {

          $raw = 'on';

      }

    }
    
    if ($key == 'type') {

      switch($value) {

      case 'last':
	 $type = 'last';
         break;

      case 'band':
	 $type = 'band';
	 break;

      case 'max':
	 $type = 'max';
	 break;

      case 'min':
	 $type = 'min';
	 break;
      }

    }

    if ($key == 'project')
	$project = $value;
    
  }
  
  $cmd = $cmd . " -ts \"$ts\" -te \"$te\"";
  $cmd = $cmd . " -width " . min(max($width, 200), 2000);
  $cmd = $cmd . " -height " . min(max($height, 70), 700);
  $cmd = $cmd . " -weight " . min(max($weight, 1), 3);
  $cmd = $cmd . " -raw " . $raw;
  $cmd = $cmd . " -type " . $type;
  $cmd = $cmd . " -project " . $project;

  if ($ymax_set == 1)
	$cmd = $cmd . " -ymax " . $ymax;

  if ($ymin_set == 1)
	$cmd = $cmd . " -ymin " . $ymin;

  // We use output buffering so we can conditionally return an image or a text error
  $ret = 0;
  passthru ( "/home/mpc/Projects/SMEI/Code/web/TelemCGI/TelemGraph " . $cmd . " 2>&1", $ret );

  if ($ret != 0) {
    header("Content-type: text/plain");
    echo "<pre>Command: TelemGraph " . $cmd . "</pre>";

  } else {
    header("Content-type: image/png");
  }
  
  ob_end_flush();

  return;
}

/* Do the main page processing */

switch ($_SESSION['state']) {

case 'template':
	/* Format the page of graphs for the selected template */
	break;

case 'select':
	do_page_header();

	/* Show the template list */
	show_existing_templates();

	/* Show the new template form */
	show_new_template();

	print_status();

	do_page_footer();
	break;

default:
	do_page_header();
	
	report_error('Unknown state encountered.  Are you being evil ?');

	print_status();
	
	do_page_footer();
	break;
}

ob_end_flush();
?>
