<html>
<head>
<title>getFluxTube</title>
</head>
<body>

<?php

	set_include_path('');

	include('Net/SSH2.php');
	$ssh = new Net_SSH2('141.52.188.80');
	if (!$ssh->login('schwarz', 'C8.(qGVCJSSS')) {
		exit('Login Failed');
	}

	include('Net/SFTP.php');
	$sftp = new Net_SFTP('141.52.188.80');
	if (!$sftp->login('schwarz', 'C8.(qGVCJSSS')) {
		exit('Login Failed');
	}

	foreach (glob("fluxtube/*") as $file)
	{
		$fp = fopen($file, "r+");

		if (fread($fp, filesize($file)) == "")
		{
			$timestamp = time();

			// 1) run KS

			$curr = explode("_", substr($file, strpos($file, "fluxtube/") + 9));
			$comm = "cd SimulationDump; echo ' ./../Kasper3DevNew/install-fieldline-script/bin/KatrinKassiopeia ../Kasper3DevNew/install-fieldline-script/config/Kassiopeia/Katrin/AxialMainSpecDetectorFieldLines_SDS-II_Flexible.xml -r";
			$comm = $comm . " PS1=" . $curr[0];
			$comm = $comm . " PS2=" . $curr[1];
			$comm = $comm . " VER=" . $curr[2];
			$comm = $comm . " HOR=" . $curr[3];
			$comm = $comm . " L1=" . $curr[4];
			$comm = $comm . " L2=" . $curr[5];
			$comm = $comm . " L3=" . $curr[6];
			$comm = $comm . " L4=" . $curr[7];
			$comm = $comm . " L5=" . $curr[8];
			$comm = $comm . " L6=" . $curr[9];
			$comm = $comm . " L7=" . $curr[10];
			$comm = $comm . " L8=" . $curr[11];
			$comm = $comm . " L9=" . $curr[12];
			$comm = $comm . " L10=" . $curr[13];
			$comm = $comm . " L11=" . $curr[14];
			$comm = $comm . " L12=" . $curr[15];
			$comm = $comm . " L13=" . $curr[16];
			$comm = $comm . " L14=" . $curr[17];
			$comm = $comm . " PCH=" . $curr[18];
			$comm = $comm . " DET=" . $curr[19];
			$comm = $comm . " OUT=" . "../../../../SimulationDump/" . $timestamp . ".root";

			// 2) run analysis

			$comm = $comm . " && ./ReadExe " . $timestamp;

			// 3) send results

			$sshcomm = 'lftp -u sds2,sds2sds2 sftp://neutrino.ikp.kit.edu -e "put ' . $timestamp . '.txt -o ' . $file . ' && exit" ';

			$comm = $comm . ' && sleep 1 && ' . $sshcomm . " '";

			// do it with tesla

			$comm = $comm . " | qsub -N sds" . $timestamp . " -cwd -V -q long.q -j y";

			//echo $comm . '<br>';
			echo $ssh->exec($comm) . '<br>';

			sleep(2);

			echo 'OK!!!<br><br><br>';
		}
		fclose($fp);
	}

/*
	foreach (glob("fluxtube/*") as $file)
	{
		$fp = fopen($file, "r+");
		if (fread($fp, filesize($file)) == "")
		{
			$timestamp = time();

			$curr = explode("_", substr($file, strpos($file, "fluxtube/") + 9));
			$comm = 'cd SimulationDump; ./../Kasper3DevNew/install/bin/KatrinKassiopeia ../Kasper3DevNew/install/config/Kassiopeia/Katrin/AxialMainSpecDetectorFieldLines_SDS-II_Flexible.xml -r';
			$comm = $comm . ' PS1=' . $curr[0];
			$comm = $comm . ' PS2=' . $curr[1];
			$comm = $comm . ' VER=' . $curr[2];
			$comm = $comm . ' HOR=' . $curr[3];
			$comm = $comm . ' L1=' . $curr[4];
			$comm = $comm . ' L2=' . $curr[5];
			$comm = $comm . ' L3=' . $curr[6];
			$comm = $comm . ' L4=' . $curr[7];
			$comm = $comm . ' L5=' . $curr[8];
			$comm = $comm . ' L6=' . $curr[9];
			$comm = $comm . ' L7=' . $curr[10];
			$comm = $comm . ' L8=' . $curr[11];
			$comm = $comm . ' L9=' . $curr[12];
			$comm = $comm . ' L10=' . $curr[13];
			$comm = $comm . ' L11=' . $curr[14];
			$comm = $comm . ' L12=' . $curr[15];
			$comm = $comm . ' L13=' . $curr[16];
			$comm = $comm . ' L14=' . $curr[17];
			$comm = $comm . ' PCH=' . $curr[18];
			$comm = $comm . ' DET=' . $curr[19];
			$comm = $comm . ' OUT=' . '"../../../../SimulationDump/' . $timestamp . '.root"';	
			echo $comm . '<br>';
			echo $ssh->exec($comm) . '<br>';

			sleep(1);

			$comm = "cd SimulationDump; ./ReadExe " . $timestamp . ";";
			echo $comm . '<br>';
			echo $ssh->exec($comm) . '<br>';

			sleep(1);

			echo $file . '<br>';
			echo $sftp->get('SimulationDump/' . $timestamp . '.txt', $file) . '<br>';

			sleep(1);

			echo 'OK!!!<br><br><br>';
		}
		fclose($fp);
	}
*/
?>

</body>
</html>

