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

$opcont='';
$reopcont='';
$opch='';
$reopch='';
$icont=' ';

if (isset($argv)) { // command line
  sleep(5); // dirname(__FILE__) .
if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.txt')) {
  $reopch=exec("cat " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.txt' . " | grep '/mysqld ' | head -1 | cut -d':' -f 2 > " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.mm');
  exec("cp -f " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.txt ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'open.txt'); 
} else {
  $opch=' ';
  file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'open.txt', '');
}
exec("ps -ef | grep 'MAMP' > " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.txt'); 
$opcont=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'open.txt');
$reopcont=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.txt');
if ($opch == "") {
  $opch=exec("cat " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'open.txt' . " | grep '/mysqld ' | head -1 | cut -d':' -f 2 > " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'open.mm');
}
if ($reopch == "") {
  $reopch=exec("cat " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.txt' . " | grep '/mysqld ' | head -1 | cut -d':' -f 2 > " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.mm');
}
if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.ongoing')) {
  if ("$opch" == "$reopch") {
    $vsc=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.ongoing');
    if ("$vsc" != "$reopch" && $vsc != "") {
      $opch=' ';
      unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.ongoing');
    }
  }
}
if ("$opch" != "$reopch" || (strpos($reopcont, '/httpd') !== false && strpos($opcont, '/httpd') === false)) {
  if (!file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.started')) {
   if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.ongoing')) {
    //file_put_contents('problem.ongoingdel', '' . $opch);
    unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . "mamp.ongoing");
   }
   file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.started', 'pre-emptive');
   if (!file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.ongoing') || "$opch" != "$reopch") {
   if (strpos($reopcnt, '/httpd') === false) {
   if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.txt')) {
     unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'reopen.txt');
   }
   if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.ongoing')) {
     unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.ongoing');
   }
   if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'index.php')) {
     $icont=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'index.php');
        //file_put_contents('huh0.0',$icont . "\n" . $reopch);
     $reopch=exec("ps -ef | grep 'MAMP' | grep '/mysqld ' | head -1 | cut -d':' -f 2");
     if (strpos($icont, "'mamp.started','" . $reopch . "'") !== false) {
       //file_put_contents('huh1',$reopch);
       $icont='';
     } else if (strpos($icont, "'mamp.started','") !== false) {
       //file_put_contents('huh2',$reopch);
       $jcont="'mamp.started','" . explode("'",explode("'mamp.started','", $icont)[1])[0] . "'";
       $kcont="'mamp.started','" . $reopch . "'";
       file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'index.php', str_replace($jcont,$kcont,$icont));
     } else if (strpos($icont, "<" . "?php") !== false) {
       //file_put_contents('huh3',$reopch);
       file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'index.php', str_replace("<" . "?php","<" . "?php  \n// 'mamp.started','" . $reopch . "'\n",$icont));
     } else if (strpos($icont, "?" . ">") !== false) {
       //file_put_contents('huh4',$reopch);
       file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'index.php', str_replace("?" . ">","\n// 'mamp.started','" . $reopch . "'\n?" . ">",$icont));
     }
   }
   if ($icont != '') {
     passthru("osascript -e 'tell application \"Terminal\" to do script \"open http://localhost:8888/index.php ; exit\"'");
   }
   }
   //} else if ("$opch" != "$reopch") {
   //file_put_contents('problem.opre', '' . $opch);
   //} else {
   //file_put_contents('problem.ongoing', '' . $opch);
   }
  }
} else if (strpos($reopcont, '/httpd') !== false && strpos($opcont, '/httpd') !== false && file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.started')) {
  //file_put_contents('problem.ongoingcre', '' . $opch);
  $reopch=exec("cat " . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'open.mm');
  $reopch=exec("ps -ef | grep 'MAMP' | grep '/mysqld ' | head -1 | cut -d':' -f 2");
  file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.ongoing', '' . $reopch);
  unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mamp.started');
}
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 if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . "macos_say_record.call")) {
  $cnt=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "macos_say_record.call");
  unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . "macos_say_record.call");
  if (strpos($cnt, "cmdrun=") !== false) {
   file_put_contents('before.all', "It's moi! Called from macos_say_record.php");
   //passthru("open http://localhost:8888/recording_ideas.php");
   if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'index.php')) {
   passthru("osascript -e 'tell application \"Terminal\" to do script \"open http://localhost:8888/index.php ; open http://localhost:8888/macos_say_record.php ; exit\"'");
   } else {
   passthru("osascript -e 'tell application \"Terminal\" to do script \"open http://localhost:8888/macos_say_record.php ; exit\"'");
   }
  }
} 
//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);
//}
if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.txt')) {
  $reopch=exec("cat " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.txt' . " | grep '/mysqld ' | head -1 | cut -d':' -f 2 > " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.mm');
  exec("cp -f " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.txt ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'open.txt'); 
} else {
  $opch=' ';
  file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'open.txt', '');
}
if (3 == 3) {
exec("ps -ef | grep 'MAMP' > " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.txt'); 
$opcont=file_get_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'open.txt');
$reopcont=file_get_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.txt');
if ($opch == "") {
  $opch=exec("cat " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'open.txt' . " | grep '/mysqld ' | head -1 | cut -d':' -f 2 > " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'open.mm');
}
if ($reopch == "") {
  $reopch=exec("cat " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.txt' . " | grep '/mysqld ' | head -1 | cut -d':' -f 2 > " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.mm');
}
if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.ongoing')) {
  if ("$opch" == "$reopch") {
    $vsc=file_get_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.ongoing');
    if ("$vsc" != "$reopch" && $vsc != "") {
      $opch=' ';
      unlink($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.ongoing');
    }
  }
}
if ("$opch" != "$reopch" || (strpos($reopcont, '/httpd') !== false && strpos($opcont, '/httpd') === false)) {
  if (!file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.started')) {
   if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.ongoing')) {
    //file_put_contents('problem.oNgoingdel', '' . $opch);
    unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . "mamp.ongoing");
   }
   file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.started', 'pre-emptive');
   if (!file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.ongoing') || "$opch" != "$reopch") {
   if (strpos($reopcnt, '/httpd') === false) {
   if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.txt')) {
     unlink($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'reopen.txt');
   }
   if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.ongoing')) {
     unlink($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.ongoing');
   }
   if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'index.php')) {
     $icont=file_get_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'index.php');
       // file_put_contents('huh00.0',$icont . "\n" . $reopch);
     $reopch=exec("ps -ef | grep 'MAMP' | grep '/mysqld ' | head -1 | cut -d':' -f 2");
     if (strpos($icont, "'mamp.started','" . $reopch . "'") !== false) {
       $icont='';
     } else if (strpos($icont, "'mamp.started','") !== false) {
       $jcont="'mamp.started','" . explode("'",explode("'mamp.started','", $icont)[1])[0] . "'";
       $kcont="'mamp.started','" . $reopch . "'";
       file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'index.php', str_replace($jcont,$kcont,$icont));
     } else if (strpos($icont, "<" . "?php") !== false) {
       file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'index.php', str_replace("<" . "?php","<" . "?php  \n// 'mamp.started','" . $reopch . "'\n",$icont));
     } else if (strpos($icont, "?" . ">") !== false) {
       file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'index.php', str_replace("?" . ">","\n// 'mamp.started','" . $reopch . "'\n?" . ">",$icont));
     }
   }
   if ($icont != '') {
     passthru("osascript -e 'tell application \"Terminal\" to do script \"open http://localhost:8888/index.php ; exit\"'");
   }
   }
   //} else if ("$opch" != "$reopch") {
   //file_put_contents('problem.oPre', '' . $opch);
   //} else {
   //file_put_contents('problem.oNgoing', '' . $opch);
   }
  }
} else if (strpos($reopcont, '/httpd') !== false && strpos($opcont, '/httpd') !== false && file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.started')) {
  //file_put_contents('problem.oNgoingcre', '' . $opch);
  $reopch=exec("cat " . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'open.mm');
  $reopch=exec("ps -ef | grep 'MAMP' | grep '/mysqld ' | head -1 | cut -d':' -f 2");
  file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.ongoing', '' . $reopch);
  unlink($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'mamp.started');
}
}
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 ; exit\"'");
  }
} if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "macos_say_record.call")) {
  $cnt=file_get_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "macos_say_record.call");
  unlink($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "macos_say_record.call");
  if (strpos($cnt, "cmdrun=") !== false) {
   file_put_contents('before.all', "It's moi! Called from macos_say_record.php");
   //passthru("open http://localhost:8888/recording_ideas.php");
   if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'index.php')) {
   passthru("osascript -e 'tell application \"Terminal\" to do script \"open http://localhost:8888/index.php ; open http://localhost:8888/macos_say_record.php ; exit\"'");
   } else {
   passthru("osascript -e 'tell application \"Terminal\" to do script \"open http://localhost:8888/macos_say_record.php ; exit\"'");
   }
  }
} else {
  exec('php ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'before_all.php &');
}
}
?>
