<?php
// before_all.php
// Prepend PHP for macOS MAMP
// April, 2026

if (isset($argv)) { // command line
  sleep(3); //(5); // dirname(__FILE__) .
if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . "recording_ideas.call")) {
  $cnt=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "recording_ideas.call");
  unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . "recording_ideas.call");
  if (strpos($cnt, "cmdrun=") !== false) {
   file_put_contents('before.all', "It's moi! Called from recording_ideas.php");
   //passthru("open http://localhost:8888/recording_ideas.php");
   passthru("osascript -e 'tell application \"Terminal\" to do script \"open http://localhost:8888/recording_ideas.php\"'");
  }
}
//exit;
} else {
$called_from=''; // . $GLOBALS['_SERVER']['SCRIPT_FILENAME'];
//$trace = debug_backtrace();
// $trace[0] is usually the caller
//if (isset($trace[0]['file'])) {
//    $called_from .= ' ' . $trace[0]['file'];
//    //error_log("Prepend file called by: " . $called_from);
//}
file_put_contents('before.all', "It's moi! Called from " . $called_from);
if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "recording_ideas.call")) {
  $cnt=file_get_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "recording_ideas.call");
  unlink($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "recording_ideas.call");
  if (strpos($cnt, "cmdrun=") !== false) {
   file_put_contents('before.all', "It's moi! Called from recording_ideas.php");
   //passthru("open http://localhost:8888/recording_ideas.php");
   passthru("osascript -e 'tell application \"Terminal\" to do script \"open http://localhost:8888/recording_ideas.php\"'");
  }
} else {
  exec('php ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'before_all.php &');
}
}
?>
