<?php

/* Libraries //*/
include('tablefunc.php');

/* MySQL Connect //*/
mysql_connect();
mysql_select_db('plum_data');

/* Redirect, sorta: //*/

if($run)
 {
 print '<html><head><title>Run completed</title></head><body>';
 print '<center><h1>Run Completed</h1></center>';
 save();
 $start=time();
 /////
 $runlocs=mysql_query("select * from topology where run=$old_idx");
 for($n=0;$n!=mysql_num_rows($runlocs);$n++)
  {
  $loc=mysql_fetch_object($runlocs);
  $r=$loc->rack;$l=$loc->location;
  mysql_query("update racks set location=$l where idx=$r");
  }
 /////
 passthru("brain.exe ".$old_idx." -a"); // Makes random combos
 passthru("calc.exe ".$old_idx); // Runs random combos
 passthru("output\\increment\\dump.exe ".$old_idx); // Dumps increment graph
 passthru("output\\simultaneous\\simul_dump.exe ".$old_idx); // Dumps simul graph
 passthru("output\\select_success\\sel_success.exe ".$old_idx); // Dumps selection-success stuff
 passthru("output\\select_success\\rackdata_dump.exe ".$old_idx); // Dumps rack data.
 passthru("output\\select_success\\boxbuild.exe ".$old_idx); // Builds box.txt
 passthru("move bars.txt output");
 passthru("move combos.txt output");
 passthru("move footer.txt output");
 if(!$oldcons)passthru("move never.txt output");
 $end=time();
 $runtime=$end-$start;
 print "<br>Time: ".$runtime.'<br>Mins: '.($runtime/60);
 print '<br>Right-click and \'save target\' on these.<br>';
 print '<a href=incrementgrapher.xls>Increment Grapher</a><br>';
 print '<a href=simultaneousgrapher.xls>Simultaneous Grapher</a><br>';
 print '<a href=select_success.xls>Selection Success</a><br>';
 print 'Your run is called ';
 $query=mysql_query('select * from runs where idx='.$old_idx);
 $query=mysql_fetch_object($query);
 print $query->tblname;
 print '<br>The run limits are ';
 print $query->min.' and '.$query->max.', unless messages from the scripts above say otherwise.';
 print '</body></html>';
 exit();
 }

handlesave();

if($runname)
 {
 $query=mysql_query('select * from runs where name="'.$runname.'"');
 $currentrun=mysql_fetch_object($query);
 }
else
 {
 $query=mysql_query('select * from runs order by name');
 $currentrun=mysql_fetch_object($query);
 }
// $currentrun is set.

$cons=crunch_cons($currentrun->constraints);

// Open doc
print '<html><head><title>Edit/View Run Data</title></head><body>';

print '<center><h1>'.$currentrun->name.'</h1></center>';

print_recordselect();

printform();

print $error;

// Close doc
print '</body></html>';


##########
# End main
##########






function print_recordselect(){
// Globals
	global $currentrun;
	$runnames=yank_array('select name from runs order by name','name');
	$curridx=array_search($currentrun->name,$runnames);
	$prev=$runnames[$curridx-1];
	$next=$runnames[$curridx+1];
// Start printing
	print '<center>';
	if($prev)
	 {
	 $prev=str_replace(' ','%20',$prev);
	 print '<a href=runsetup.php?runname='.$prev.'>&lt Prev</a> ';
	 }
	if($next)
	 {
	 $next=str_replace(' ','%20',$next);
	 print ' <a href=runsetup.php?runname='.$next.'>Next &gt</a>';
	 }
	print '<form method=post action=runsetup.php>';
	print listbox('runname',$runnames,$currentrun->name);
	print input('submit','select','Select');
	print '</form>';
	print '</center>';
}






function printform(){
// Globals
	global $currentrun;
	global $cons;

// Tooltips
	print tooltip_head();

// Form open
	print '<form method=post action=runsetup.php>';

// Hidden
	print input('hidden','old_idx',$currentrun->idx);

// Table open
	print '<table cellspacing=0 cellpadding=2>';

// Row 1
	print '<tr>';

	// Name
		print '<td colspan=2 bgcolor=aaffaa>';

		// Entries
			print table(
			       row('Name:',input('text','name',$currentrun->name)),
			       row('Comment:','<textarea rows=5 cols=30 name=comment>'.$currentrun->comment.'</textarea>'),
			       row('Minimum number of racks:',input('text','min',$currentrun->min)),
			       row('Maximum number of racks:',input('text','max',$currentrun->max)),
			       row('Number of combinations:',input('text','numcombos',$currentrun->numcombos))
			       );

	// End name
		print '</td>';

	// Basic
		print '<td rowspan=3 bgcolor=ccffcc valign=top>';

		// Entries
			print '<center><b>Station-wide Resources</b></center>';
			print table(
			       row('Middecks (MLEs):<hr>',input('text','MLE',$currentrun->MLE)),
			       row('Stowage (no. of racks):<hr>',input('text','stowage',$currentrun->stowage)),
			       row('Resupply upmass (kg):<hr>',input('text','upmass',$currentrun->upmass)),
			       row('Bulk resource- Att. P/Ls (kg):<hr>',input('text','bulk',$currentrun->bulk)),
			       row('Rack overhead factor:<hr>',input('text','rack_overhead',$currentrun->rack_overhead)),
			       row('Crew training (hrs/increment):<hr>',input('text','crewtrng',$currentrun->crewtrng)),
			       row('Crew time (hrs/week):<hr>',input('text','crewtime',$currentrun->crewtime)),
			       row('Total ISS power (kW):<hr>',input('text','power',$currentrun->power)),
			       row('Cam housekeeping power (kW):<hr>',input('text','hskpg',$currentrun->hskpg)),
			       row('Total ISS keepalive (kW):<hr>',input('text','keepalive',$currentrun->keepalive)),
			       row('Increment length (days):',input('text','inclength',$currentrun->inclength))
			       );

	// End basic
		print '</td>';

// End row 1
	print '</tr>';

// Row 2
	print '<tr>';

	// Network avg
		print '<td bgcolor=aaaaff>';

		// Entries
			print '<center><b>Average Data Rate</b></center>';
			print table(
			       row('Uplink:',input('text',avg_uplink,$currentrun->avg_uplink)),
			       row('Downlink:',input('text',avg_downlink,$currentrun->avg_downlink)),
			       row('Ethernet:',input('text',avg_ethernet,$currentrun->avg_ethernet)),
			       row('1553:',input('text',avg_1553,$currentrun->avg_1553)),
			       row('HRL:',input('text',avg_HRL,$currentrun->avg_HRL)),
			       row('Video Uplink:',input('text',video_uplink,$currentrun->video_uplink))
			       );

	// End avg
		print '</td>';

	// Network peak
		print '<td bgcolor=ccccff>';

		// Entries
			print '<center><b>Peak Data Rate</b></center>';
			print table(
			       row('Uplink:',input('text',peak_uplink,$currentrun->peak_uplink)),
			       row('Downlink:',input('text',peak_downlink,$currentrun->peak_downlink)),
			       row('Ethernet:',input('text',peak_ethernet,$currentrun->peak_ethernet)),
			       row('1553:',input('text',peak_1553,$currentrun->peak_1553)),
			       row('HRL:',input('text',peak_HRL,$currentrun->peak_HRL)),
			       row('Video Downlink:',input('text',video_downlink,$currentrun->video_downlink))
			       );

	// End peak
		print '</td>';

	// Network peak duration
		print '<td bgcolor=ccccff>';

		// Entries
			print '<center><b>Peak Data Rate Duration</b></center>';
			print table(
			       row('Uplink:',input('text',peak_uplink_duration,$currentrun->peak_uplink_duration)),
			       row('Downlink:',input('text',peak_downlink_duration,$currentrun->peak_downlink_duration)),
			       row('Ethernet:',input('text',peak_ethernet_duration,$currentrun->peak_ethernet_duration)),
			       row('1553:',input('text',peak_1553_duration,$currentrun->peak_1553_duration)),
			       row('HRL:',input('text',peak_HRL_duration,$currentrun->peak_HRL_duration)),
			       row('Video Rate:',input('text',video_rate,$currentrun->video_rate))
			       );

	// End peak
		print '</td>';

	print "</tr><tr>";


	// Constraints
		print '<td rowspan=1 bgcolor=ffcccc valign=top>';

		// Entries
			print '<table cellspacing=0 valign=top>';

			print '<tr><td align=center colspan=2><br><b>Resource Constraints</b></td></tr>';
			print row(checkbox('cons3',$cons[3]),'- HRF 1 and HRF 2 share crew time.<hr>');
			print row(checkbox('cons4',$cons[4]),'- Both HRFs and EPM share crew time.');
			print '</table>';

	// End constraints
		print '</td>';

	// New SAMS
		print '<td rowspan=1 bgcolor=eaccff valign=top>';

		// Entries
			print '<table cellspacing=0 valign=top>';
			print '<tr><td align=center colspan=1><br><b>New SAMS Parameters</b></td></tr>';
			print row('Uses new SAMS<hr>',checkbox('newsams',$currentrun->newsams));
			print row('KU-Band receiver power:<hr>',input('text','ku_power',$currentrun->ku_power));
			print row('SAMS rack support power:<hr>',input('text','pl_power',$currentrun->pl_power));
			print row('SAMS power:',input('text','sams_power',$currentrun->sams_power));
			print '</table>';

	// End new SAMS
		print '</td>';




// End row 2
	print '</tr>';

// Row 3
	print '<tr>';

	// Buttons
		print '<td colspan=2 bgcolor=ffffcc valign=top>';
		print input('submit','save','Save');
		print input('submit','saveasnew','Save as new');
		print input('submit','delete','Delete');
		print input('submit','run','Run');
		print '</td>';
		print '<td bgcolor=ffffcc align=right>';
		print '</form>'; // Close form
		$btns= '<form method=post action=selection.php>';
		$btns.=input('submit','selection','Selection status');
		$btns.=input('hidden','runname',$currentrun->name);
		$btns.='</form>';

		$btn2= '<form method=post action=runcons.php>';
		$btn2.=input('submit','constraints','Constraints');
		$btn2.=input('hidden','runidx',$currentrun->idx);
		$btn2.='</form>';

		$btn3= '<form method=post action=assignloc.php>';
		$btn3.=input('submit','topology','Topology');
		$btn3.=input('hidden','runidx',$currentrun->idx);
		$btn3.='</form>';

		print table(row($btns,$btn2,$btn3));

		print '</td>';

// End row
	print '</tr>';

// Table close
	print '</table>';
}







function crunch_cons($cons){
	for($n=0;$n!=8;$n++)
	 {
	 $array[$n+1]=$cons & pow(2,$n);
	 }
	return $array;
}








function handlesave(){
// Globals
	global $save;
	global $saveasnew;
	global $delete;
	if($saveasnew)
	 {
	 saveasnew();
	 }
	else if($save)
	 {
	 save();
	 }
	else if($delete)
	 {
	 delete();
	 }
}







function saveasnew(){
// Globals
	global $name;
	global $error;
	global $runname;
	global $old_idx;
// Run query
	mysql_query('insert into runs (name) values ("'.$name.'")');
	$error.=print_error('MySQL Error!',mysql_error());
	$new=mysql_fetch_object(mysql_query('select idx from runs where name="'.$name.'"'));
	$foo=$old_idx;
	$old_idx=$new->idx;
// Copy selection status
	$status=mysql_query('select * from selection where run='.$foo);
	for($n=0;$n!=mysql_num_rows($status);$n++)
	 {
	 $datum=mysql_fetch_object($status);
	 mysql_query('insert into selection (run,rack,status) values ('.$old_idx.','.$datum->rack.','.$datum->status.')');
	 }
// Copy new-style constraints
	$cons=mysql_query("select * from runcons where run=$foo");
	for($n=0;$n!=mysql_num_rows($cons);$n++)
	 {
	 $con=mysql_fetch_object($cons);
	 $consname=$con->cons;
	 mysql_query("insert into runcons (run,cons) values ($old_idx,\"$consname\")");
	 }
// Copy new-style topology
	$topo=mysql_query("select * from topology where run=$foo");
	for($n=0;$n!=mysql_num_rows($topo);$n++)
	 {
	 $t=mysql_fetch_object($topo);
	 $r=$t->rack;$l=$t->location;
	 mysql_query("insert into topology (run,rack,location) values ($old_idx,$r,$l)");
	 }
// Update runname
	$runname=$name;
	save();
}







function save(){
// Globals
	global $error;
	global $old_idx;
	global $name;
	global $runname;
// Start query
	$query='update runs set ';
	$query.='name="'.$name.'"';
	$query.=',comment="'.grabglobal('comment').'"';
	$query.=',min='.grabglobal('min');
	$query.=',max='.grabglobal('max');
	$query.=',numcombos='.grabglobal('numcombos');
	// Station wide
	$query.=',MLE='.grabglobal('MLE');
	$query.=',stowage='.grabglobal('stowage');
	$query.=',upmass='.grabglobal('upmass');
	$query.=',bulk='.grabglobal('bulk');
	$query.=',rack_overhead='.grabglobal('rack_overhead');
	$query.=',crewtrng='.grabglobal('crewtrng');
	$query.=',crewtime='.grabglobal('crewtime');
	$query.=',power='.grabglobal('power');
	$query.=',hskpg='.grabglobal('hskpg');
	$query.=',keepalive='.grabglobal('keepalive');
	$query.=',inclength='.grabglobal('inclength');
	// Network avg
	$query.=',avg_uplink='.grabglobal('avg_uplink');
	$query.=',avg_downlink='.grabglobal('avg_downlink');
	$query.=',avg_1553='.grabglobal('avg_1553');
	$query.=',avg_ethernet='.grabglobal('avg_ethernet');
	$query.=',avg_HRL='.grabglobal('avg_HRL');
	// Network peak
	$query.=',peak_uplink='.grabglobal('peak_uplink');
	$query.=',peak_downlink='.grabglobal('peak_downlink');
	$query.=',peak_1553='.grabglobal('peak_1553');
	$query.=',peak_ethernet='.grabglobal('peak_ethernet');
	$query.=',peak_HRL='.grabglobal('peak_HRL');
	// Network peak duration
	$query.=',peak_uplink_duration='.grabglobal('peak_uplink_duration');
	$query.=',peak_downlink_duration='.grabglobal('peak_downlink_duration');
	$query.=',peak_1553_duration='.grabglobal('peak_1553_duration');
	$query.=',peak_ethernet_duration='.grabglobal('peak_ethernet_duration');
	$query.=',peak_HRL_duration='.grabglobal('peak_HRL_duration');
	// Video
	$query.=',video_uplink='.grabglobal('video_uplink');
	$query.=',video_downlink='.grabglobal('video_downlink');
	$query.=',video_rate='.grabglobal('video_rate');
	// New SAMS
	$query.=',ku_power='.grabglobal('ku_power');
	$query.=',pl_power='.grabglobal('pl_power');
	$query.=',sams_power='.grabglobal('sams_power');
	$query.=',newsams='.grabglobal('newsams');
	// Constraints
	global $cons1;global $cons6;global $cons7;
	global $cons2;global $cons5;global $cons8;
	global $cons3;global $cons4;
	$cons=array($cons1,$cons2,$cons3,$cons4,$cons5,$cons6,$cons7,$cons8);
	$constraints=0;
	for($n=0;$n!=8;$n++)
	 {
	 if($cons[$n])
	  {
	  $constraints+=pow(2,$n);
	  }
	 }
	$query.=',constraints='.$constraints;
	$query.=',oldcons=0';
// Query
	mysql_query($query.' where idx='.$old_idx);
	$error.=print_error('MySQL Error!',mysql_error());
	$runname=$name;
}






function delete(){
// Globals
	global $old_idx;
// Query
	mysql_query('delete from runs where idx='.$old_idx);
	mysql_query("delete from topology where run=$old_idx");
	mysql_query("delete from runcons where run=$old_idx");
// Update runname
	$runname='';
}
?>