<?php

$postData = file_get_contents("php://input");

$uid = $postData['id'];
$filename = $postData['filename'];
$language = $postData['language'];
$input_data = $postData['input_data'];

$ret_stat = 0;

$template = $uid . strval(rand(1, 1000000));
$in_fname = "temp_in" . $template;

$file = fopen($in_fname, 'w');
file_put_contents($in_fname, $input_data);
fclose($file);

$out_fname = "temp_out" . $template;

if(strcmp($language, "cpp") == 0)
{
    $base = basename($filename, ".cpp");
    system("./" . $uid . "/" . $base . " < " . $in_fname . " > " . $out_fname, $ret_stat);
}

else if(strcmp($language, "java") == 0)
{
    $base = basename($filename, ".java");
    system("java -cp " . $uid . "/ " . $base . " < " . $in_fname . " > " . $out_fname, $ret_stat);
}

else if(strcmp($language, "python") == 0)
{
    system("python3 " . $uid . "/" . $filename . " < " . $in_fname . " > " . $out_fname, $ret_stat);
}

if($ret_stat != 0)
{
    echo "Runtime Error!!";
}

else
{
    $file = fopen($out_fname, 'r');
    echo fread($file, filesize($out_fname));
    fclose($file);
}

unlink($in_fname);
unlink($out_fname);

?>