<?php
// sass_watchdog.php
// RJM Programming
// Optionally helps out sass --watch [inputSassSCSS] [outputCSS]

$incssfiles=[" "];
$outcssfiles=["  "];
$logfiles=["   "];
$errlogfiles=["    "];
$results=["      "];
$amrunning=["      "];
$sasexe=["       "];
$sasswitch=[" --watch "];

//      /usr/local/Cellar/sass/1.22.12/bin/sass --watch /Applications/MAMP/htdocs/sass/base.scss /Applications/MAMP/htdocs/sass/build/test.css >> /Applications/MAMP/htdocs/sass/sass_watch.ok 2>> /Applications/MAMP/htdocs/sass/sass_watch.notok & 

function LaunchBackgroundProcess($command) {   // thanks to https://stackoverflow.com/questions/45953/php-execute-a-background-process

    global $incssfiles, $outcssfiles, $logfiles, $errlogfiles,$amrunning,$sasexe;

  // Run command Asynchroniously (in a separate thread)
  if (PHP_OS=='WINNT' || PHP_OS=='WIN32' || PHP_OS=='Windows') {
    // Windows
    $command = 'start "" '. $command;
  } else {
    // Linux/UNIX
    $command = $command .' /dev/null &';
  }
  $handle = popen($command, 'r');
  if ($handle !== false) {
    pclose($handle);
    return true;
  } else {
    return false;
  }
}


//Note for windows users:
//You cannot redirect stdout/stderr to nul in the following manner:
//startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
//However, you can do this:
//startBackgroundProcess('ping yandex.com >nul 2>&1');

function startBackgroundProcess(
    $command,
    $stdin = null,
    $redirectStdout = null,
    $redirectStderr = null,
    $cwd = null,
    $env = null,
    $other_options = null
) {  // thanks to https://stackoverflow.com/questions/45953/php-execute-a-background-process

    global $incssfiles, $outcssfiles, $logfiles, $errlogfiles,$amrunning,$sasexe;
    
    $descriptorspec = array(
        1 => is_string($redirectStdout) ? array('file', $redirectStdout, 'w') : array('pipe', 'w'),
        2 => is_string($redirectStderr) ? array('file', $redirectStderr, 'w') : array('pipe', 'w'),
    );
    if (is_string($stdin)) {
        $descriptorspec[0] = array('pipe', 'r');
    }
    $proc = proc_open($command, $descriptorspec, $pipes, $cwd, $env, $other_options);
    if (!is_resource($proc)) {
        throw new \Exception("Failed to start background process by command: $command");
    }
    if (is_string($stdin)) {
        fwrite($pipes[0], $stdin);
        fclose($pipes[0]);
    }
    if (!is_string($redirectStdout)) {
        fclose($pipes[1]);
    }
    if (!is_string($redirectStderr)) {
        fclose($pipes[2]);
    }
    return $proc;
}

if (!function_exists("readline")) {  // thanks to https://stackoverflow.com/questions/23238378/call-to-undefined-function-readline
    function readline($prompt = null){
        if ($prompt){
            echo $prompt;
        }
        $fp = fopen("php://stdin","r");
        $line = rtrim(fgets($fp, 1024));
        return $line;
    }
}


if (!isset($argc)) { 
if (isset($_GET['check'])) {
} else if (isset($_GET['in1']) && isset($_GET['out1'])) {
} else if (isset($_POST['in1out1'])) {
   $one=1;
   while (isset($_POST['in' . $one . 'out' . $one])) {
    if (sizeof($incssfiles) > $one) {
     if (PHP_OS=='WINNT' || PHP_OS=='WIN32' || PHP_OS=='Windows') {
       if (1 == 2) { startBackgroundProcess('ping yandex.com >nul 2>&1'); }
     } else {
       if (1 == 2) { startBackgroundProcess('ping yandex.com >> /dev/null 2>> /dev/null'); }
     }
    }
    $one++;
   }
} else {
  echo "<!doctype html>
<html>
<head>
<script type='text/javascript'>
 var atthis=1;
 var inputtemp='';
 var vals=[];
 function add(oin) {
   if (inputtemp == '') {
     inputtemp=oin.outerHTML;
   }
   if (oin.value.replace(' ',';').indexOf(';') != -1) {
     vals.push(oin.value);
     atthis++;
     //document.getElementById('mytr').innerHTML+='<td id=mytd' + atthis + '>' + inputtemp.replace(/1/g, '' + atthis) + '</td>';
     document.getElementById('mytbody').innerHTML+='<tr><td id=mytd' + atthis + '>' + inputtemp.replace(/1/g, '' + atthis) + '</td></tr>';
     for (var i=0; i<eval(-1 + atthis); i++) {
       document.getElementById('in' + eval(1 + i) + 'out' + eval(1 + i)).value=vals[i];
     }
   }
 }
</script>
</head>
<body onload=\"add(document.getElementById('in1out1'));\">
<h1>Sass 'sass --watch [inputSassSCSS] [outputCSS]' Supervisor and Watchdog</h1>
<h3>RJM Programming</h3>
<h3>September, 2019</h3>
<form id=myform action=./sass_watchdog.php method=POST>
<table id=myexetable border=5><tbody id=myexetbody>
<tr id=myexetr>
<td id=myexetd1><input style='width:300px;' name=sasexe value='/usr/local/Cellar/sass/1.22.12/bin/sass' placeholder='Sass sass executable path'></input></td>
<td id=myswitchtd1><input style='width:100px;' name=sasswitch value=' -- watch ' placeholder='Sass sass executable switch'></input></td>
</tr>
</tbody>
</table>
<br><br><table id=mytable border=5><tbody id=mytbody>
<tr id=mytr><td id=mytd1>
<input style='width:400px;' id=in1out1 onblur=add(this); name=in1out1 value='' placeholder='Separate with ; (or space) an [inputSassSCSS];[outputCSS]'></input>
</td></tr>
</tbody>
</table>
<br><br>
<table id=mylogtable border=5><tbody id=mylogtbody>
<tr id=mylogtr>
<td id=mylogtd1><input style='width:200px;' name=logfiles value='' placeholder='Append to this logfile'></input></td>
<td id=mylogtd2><input style='width:200px;' name=errlogfiles value='' placeholder='Append to this error logfile'></input></td>
</tr>
</tbody>
</table>
<br><br><input style='background-color:yellow;' type=submit id=mysubmit value='Start Watchdog for Above'></input>
</form>
</body>
</html>";
}
} else {
}
?>
