<?php
//include_once("your_auth_script_here");

//variable checking functions
function mv($val){
	if (isset($_GET[$val]) && $_GET[$val] != ""){
		return $_GET[$val];
	}else{
		return "";
	}
}
//Setup the required directories
$filename = "";
if (mv("inputfile") != ""){
	$filename = mv("inputfile");
	if (file_exists("input/" . $filename)){
		//Ok to proceed.
	}else{
		die("ERROR. The given filename does not exists in the input/ folder.");
	}
}else{
	die("ERROR. Undefined inputfile filename. (Usage: index.php?inputfile={filenamehere}");
}

$profileDir = "templates/";
$slicingProfile = "default.txt";
if (mv("profile") != ""){
	$slicingProfile = mv("profile");
}
if (!file_exists($profileDir . $slicingProfile)){
	die("ERROR. Slicing config template (template.txt) not exists.");
}else{
	$template = file_get_contents($profileDir . $slicingProfile);
	$outfilename = time() . ".gcode";
	$outFile = "output/" . $outfilename;
	$cmd = str_replace("{inputfilename}","input/" . $filename,$template);
	$cmd = str_replace("{outputfilename}",$outFile,$cmd);
	//cmd ready to be sliced
	$returnedValue = shell_exec("sudo CuraEngine " . $cmd);
	//echo the newly created filename for the gcode conversion
	echo $outfilename;
	//Removing the converted stl file to save spaces
	unlink("input/" . $filename);
}


?>