<?php
// map_playground.php
// RJM Programming - October, 2026
ini_set('max_execution_time', 6000);

$pargzero="";
$prephp="";
$postphp="";
$beforephp="";
$swith='"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Templates/File Templates/macOS/Playground"';
$swith='"/System/Volumes/Data/"';
$swith='"./"';
if (isset($argv)) {
$pargzero=$argv[0];
$postphp=realpath($argv[0]);
$beforephp="http://localhost:8888/";
$startwith='"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Templates/File Templates/macOS/Playground"';
$startwith='"/System/Volumes/Data/"';
$startwith='"./"';
if ($argc == 2) {
  $startwith=$argv[-1 + $argc];
  if (strpos($startwith, ' ') !== false) {
    if (substr($startwith,0,1) != '"') {
      $startwith='"' . $argv[-1 + $argc] . '"';
    }
  }
}
if ($argc > 2) {
  // Process
  echo "here";
  exit;
}
} else {
$postphp=$_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'];
$beforephp='http://' . str_replace(':80/','/',$_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . '');
$pargzero=$_SERVER['SCRIPT_NAME'];
}

if (isset($_GET['dummyrun'])) {
exit;
} else if (isset($_POST['path'])) {
$worthwhile=false;
$inmap=str_replace('"','',str_replace('+',' ',urldecode($_POST['path'])));
$sugnewsuffix="_GETME";
if (file_exists($inmap)) {
$prevcontis=file_get_contents($inmap);
$thislat=str_replace('+',' ',urldecode($_POST['latitude']));
$waslat=str_replace('+',' ',urldecode($_POST['waslatitude']));
$thislong=str_replace('+',' ',urldecode($_POST['longitude']));
$waslong=str_replace('+',' ',urldecode($_POST['waslongitude']));
$thistitle=str_replace('+',' ',urldecode($_POST['title']));
$wastitle=str_replace('+',' ',urldecode($_POST['wastitle']));
$thissubtitle=str_replace('+',' ',urldecode($_POST['subtitle']));
$wassubtitle=str_replace('+',' ',urldecode($_POST['wassubtitle']));
if ($thislat != $waslat && strpos($prevcontis, $waslat . ",") !== false) {
  $worthwhile=true;
}
if ($thislong != $waslong && strpos($prevcontis, $waslong . ")") !== false) {
  $worthwhile=true;
}
if ($thistitle != $wastitle && strpos($prevcontis, $wastitle . "") !== false) {
  $worthwhile=true;
}
if ($thissubtitle != $wassubtitle && strpos($prevcontis, $wassubtitle . "") !== false) {
  $worthwhile=true;
}
if ($worthwhile) {
  while (file_exists($inmap . $sugnewsuffix)) {
    $sugnewsuffix=str_replace("_GETME","-GETME",str_replace("-GETME","--GETME",$sugnewsuffix));
  }
  file_put_contents($inmap . $sugnewsuffix, $prevcontis);
}
if ($thislat != $waslat && strpos($prevcontis, $waslat . ",") !== false) {
  $prevcontis=str_replace($waslat . ",", $thislat . ",", $prevcontis);
}
if ($thislong != $waslong && strpos($prevcontis, $waslong . ")") !== false) {
  $prevcontis=str_replace($waslong . ")", $thislong . ")", $prevcontis);
}
if ($thistitle != $wastitle && strpos($prevcontis, $wastitle . "") !== false) {
  $prevcontis=str_replace($wastitle, $thistitle, $prevcontis);
}
if ($thissubtitle != $wassubtitle && strpos($prevcontis, $wassubtitle . "") !== false) {
  $prevcontis=str_replace($wassubtitle, $thissubtitle, $prevcontis);
}
if ($worthwhile) {
  file_put_contents($inmap, $prevcontis);
  //echo "more to do";
  //echo ('open -a Xcode ' . str_replace(DIRECTORY_SEPARATOR . "Contents.swift","",str_replace('+',' ',urldecode($_POST['path']))));
  exec('open -a Xcode ' . str_replace(DIRECTORY_SEPARATOR . "Contents.swift","",str_replace('+',' ',urldecode($_POST['path']))));
  }
} else {
echo str_replace('+',' ',urldecode($_POST['path'])) . ' # not Found';
}
exit;
} else if (isset($_GET['waslatitude'])) {
$worthwhile=false;
$inmap=str_replace('"','',str_replace('+',' ',urldecode($_GET['path'])));
if (file_exists($inmap)) {
$prevcontis=file_get_contents($inmap);
$thislat=str_replace('+',' ',urldecode($_GET['latitude']));
$waslat=str_replace('+',' ',urldecode($_GET['waslatitude']));
$thislong=str_replace('+',' ',urldecode($_GET['longitude']));
$waslong=str_replace('+',' ',urldecode($_GET['waslongitude']));
$thistitle=str_replace('+',' ',urldecode($_GET['title']));
$wastitle=str_replace('+',' ',urldecode($_GET['wastitle']));
$thissubtitle=str_replace('+',' ',urldecode($_GET['subtitle']));
$wassubtitle=str_replace('+',' ',urldecode($_GET['wassubtitle']));
if ($thislat != $waslat && strpos($prevcontis, $waslat . ",") !== false) {
  $worthwhile=true;
}
if ($thislong != $waslong && strpos($prevcontis, $waslong . ")") !== false) {
  $worthwhile=true;
}
if ($thistitle != $wastitle && strpos($prevcontis, $wastitle . "") !== false) {
  $worthwhile=true;
}
if ($thissubtitle != $wassubtitle && strpos($prevcontis, $wassubtitle . "") !== false) {
  $worthwhile=true;
}
if ($worthwhile) {
  while (file_exists($inmap . $sugnewsuffix)) {
    $sugnewsuffix=str_replace("_GETME","-GETME",str_replace("-GETME","--GETME",$sugnewsuffix));
  }
  file_put_contents($inmap . $sugnewsuffix, $prevcontis);
}
if ($thislat != $waslat && strpos($prevcontis, $waslat . ",") !== false) {
  $prevcontis=str_replace($waslat . ",", $thislat . ",", $prevcontis);
}
if ($thislong != $waslong && strpos($prevcontis, $waslong . ")") !== false) {
  $prevcontis=str_replace($waslong . ")", $thislong . ")", $prevcontis);
}
if ($thistitle != $wastitle && strpos($prevcontis, $wastitle . "") !== false) {
  $prevcontis=str_replace($wastitle, $thistitle, $prevcontis);
}
if ($thissubtitle != $wassubtitle && strpos($prevcontis, $wassubtitle . "") !== false) {
  $prevcontis=str_replace($wassubtitle, $thissubtitle, $prevcontis);
}
if ($worthwhile) {
  file_put_contents($inmap, $prevcontis);
  //echo "more to do";
  //echo ('open -a Xcode ' . str_replace(DIRECTORY_SEPARATOR . "Contents.swift","",str_replace('+',' ',urldecode($_POST['path']))));
  exec('open -a Xcode ' . str_replace(DIRECTORY_SEPARATOR . "Contents.swift","",str_replace('+',' ',urldecode($_GET['path']))));
  }
} else {
echo str_replace('+',' ',urldecode($_GET['path'])) . ' # not found';
}
exit;
} else if (isset($_GET['path'])) {
$beforephp='http://' . str_replace(':80/','/',$_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . '/');
$firstthingo="data-onkeydown='retval=true;' readonly ";
if (isset($_GET['latitude'])) {
  if ('' == $_GET['latitude']) {
    $firstthingo=" onclick=ask('" . $_GET['path'] . "'); ";
  }
}
echo "<html>
<head>
<style>
 input[type=text] { width: 96%;  }
</style>
<scr" . "ipt type=text/javascript>
  var retval=false;
  
  function ask(invl) {
    if (invl == '') { invl='" . $swith . "';  }
    var altlookplace=prompt('Please enter better place to start looking for Xcode Swift Map Playground', decodeURIComponent(invl));
    if (altlookplace == null) { altlookplace='';  }
    if (altlookplace != '' && altlookplace != decodeURIComponent(invl)) {
      location.href=document.URL.split('?')[0].split('#')[0] + '?startwith=' + encodeURIComponent(altlookplace);
    }
  }
  
  function andthen() {
    if (retval) {
      var butlist=['path','latitude','longitude'];
      for (var jj=0; jj<butlist.length; jj++) {
        if (document.getElementById(butlist[jj]).value == '') {   retval=false; }
      }
    }
  }
  
  
  setInterval(andthen, 1000);
</scr" . "ipt>
</head>
<body>
<h1>Xcode Swift Map Playground</h1>
<h3>Based on existant one you ever created ...</h3><br>
<form onsubmit=\"if (retval && 4 == 5) { document.getElementById('postwork').style.display='block';  }  return retval;\" data-target=postwork action='" . $beforephp . (substr(explode('?',$_SERVER['REQUEST_URI'])[0],0,1) == '/' ? substr(explode('?',$_SERVER['REQUEST_URI'])[0],1) : explode('?',$_SERVER['REQUEST_URI'])[0]) . "' method=POST>
<input " . $firstthingo . " type=text name=path id=path placeholder=Path title=Path value='" . str_replace('+',' ',urldecode($_GET['path'])) . "'></input><br>
<input style=display:none; data-onkeydown='retval=true;' type=text name=scriptpath id=scriptpath placeholder='PHP Script Path' title='PHP Script Path' value=\"" . str_replace('+',' ',urldecode($_GET['scriptpath'])) . "\" readonly></input><br>
<input style=display:none; data-onkeydown='retval=true;' type=text name=prephp id=prephp placeholder='PHP Path' title='PHP Path' value=\"" . str_replace('+',' ',urldecode($_GET['prephp'])) . "\" readonly></input><br>
<input onkeydown='retval=true;' type=text name=latitude id=latitude placeholder=Latitude title=Latitude value=\"" . str_replace('+',' ',urldecode($_GET['latitude'])) . "\"></input><br>
<input onkeydown='retval=true;' type=text name=longitude id=longitude placeholder=Longitude title=Longitude value=\"" . str_replace('+',' ',urldecode($_GET['longitude'])) . "\"></input><br>
<input onkeydown='retval=true;' type=text name=title id=title placeholder=Title title=Title value=\"" . str_replace('+',' ',urldecode($_GET['title'])) . "\"></input><br>
<input onkeydown='retval=true;' type=text name=subtitle id=subtitle placeholder=Subtitle title=Subtitle value=\"" . str_replace('+',' ',urldecode($_GET['subtitle'])) . "\"></input><br>
<input style=display:none; type=text name=waslatitude id=latitude placeholder=Latitude title=Latitude value=\"" . str_replace('+',' ',urldecode($_GET['latitude'])) . "\"></input>
<input style=display:none; type=text name=waslongitude id=longitude placeholder=Longitude title=Longitude value=\"" . str_replace('+',' ',urldecode($_GET['longitude'])) . "\"></input>
<input style=display:none; type=text name=wastitle id=title placeholder=Title title=Title value=\"" . str_replace('+',' ',urldecode($_GET['title'])) . "\"></input>
<input style=display:none; type=text name=wassubtitle id=subtitle placeholder=Subtitle title=Subtitle value=\"" . str_replace('+',' ',urldecode($_GET['subtitle'])) . "\"></input>
<br><input style=background-color:yellow; type=submit value=\"Xcode Swift Map Playground Tailoring via Your Changes to Current Settings Above\"></input></form>
<br><br><iframe frameborder=0 style=width:100%;height:900px;display:none; name=postwork id=postwork src='" . $pargzero . "?dummyrun=y'></iframe>
</body></html>";
exit;
} else {
if (PHP_OS != "Darwin") {
  header('Location: http://localhost:8888//map_playground.php');
  exit;
}
if (isset($_GET['startwith'])) {
$startwith=str_replace('+',' ',urldecode($_GET['startwith']));
$beforephp='http://' . str_replace(':80/','/',$_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . '/');
} else {
$startwith=$swith;
}
$results="";
//$findone=shell_exec('find "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Templates/File Templates/macOS/Playground" -name "Contents.swift" -type f -exec grep -l "CLLocationCoordinate2DMake(" {} + 2> /dev/null');
$findone=shell_exec('find ' . $startwith . ' -name "Contents.swift" -type f -exec grep -l "CLLocationCoordinate2DMake(" {} + 2> /dev/null');
$projdir="";
$tleis="";
$subtleis="";
$latlongis="";
$lastti=-1;

$finds=explode("\n", $findone);
for ($i=0; $i<sizeof($finds); $i++) {
  $thispath=explode("Contents.swift", $finds[$i])[0];
  if (is_dir($thispath) && strpos($thispath,'Xcode.app') === false && strpos($thispath,'Desktop') === false) {
    if (file_exists($thispath . 'contents.xcplayground')) {
      $contis=file_get_contents($thispath . "Contents.swift");
      $lesscontis=str_replace(' ','',$contis);
      if (strpos($lesscontis, '.subtitle="') !== false && strpos($lesscontis, '.title="') !== false) {
       if ($results == "" || filemtime($thispath . "Contents.swift") > $lastti) {
        $lastti=filemtime($thispath . "Contents.swift");
        if (strpos($thispath, ' ') !== false) {
        $projdir='"' . $thispath . 'Contents.swift"';
        } else {
        $projdir=$thispath . 'Contents.swift';
        }
        $results=$thispath . "Contents.swift";
        if (strpos($contis, '.title = "') !== false) {
        $tleis=explode('"', explode('.title = "', $contis)[1])[0]; 
        } else {
        $tleis=explode('"', explode('.title="', $lesscontis)[1])[0]; 
        }
        if (strpos($contis, '.title = "') !== false) {
        $subtleis=explode('"', explode('.subtitle = "', $contis)[1])[0]; 
        } else {
        $subtleis=explode('"', explode('.subtitle="', $lesscontis)[1])[0]; 
        }
        $latlongis=explode(')', explode('CLLocationCoordinate2DMake(', $contis)[1])[0]; 
       } else if (1 == 5) {
        $results.="\n" . $thispath . "Contents.swift";
       }
      }
    }
  }
}
//echo $results . "\n\nTitle = " . $tleis . "\nSubtitle = " . $subtleis . "\nLatitude,Longitude = " . $latlongis;
//if ($prephp == "") {
//$prephp=exec('which -a php');
//}
//if ($prephp == "") {
//$prephp=exec('alias php');
//}
//if ($prephp == "") {
//$prephp=exec('type -a php');
//}
//if ($prephp == "") {
//$prephp=exec("php -r \"echo realpath(exec('which php')) . PHP_EOL;\"");
//}
if ($prephp == "") {
$prephp=PHP_BINARY;
}
if ($prephp == "") {
$prephp='php';
}
if ($postphp == "") {
$postphp=$argv[0];
}
if ($beforephp != "") {
//exec("open \"" . $beforephp . $argv[0] . "?prephp=" . urlencode($prephp) . "&scriptpath=" . urlencode(str_replace(DIRECTORY_SEPARATOR . '.' . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,dirname(__FILE__) . DIRECTORY_SEPARATOR . $argv[0])) . "&path=" . urlencode($projdir) . "&latitude=" . urlencode(trim(explode(",", $latlongis)[0])) . "&longitude=" . urlencode(trim(explode(",", $latlongis)[1])) . "&title=" . urlencode($tleis) . "&subtitle=" . urlencode($subtleis) . "\"");
exec("open \"" . $beforephp . $pargzero . "?prephp=" . urlencode($prephp) . "&scriptpath=" . urlencode(str_replace(DIRECTORY_SEPARATOR . '.' . DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR,dirname(__FILE__) . DIRECTORY_SEPARATOR . $argv[0])) . "&path=" . urlencode($projdir) . "&latitude=" . urlencode(trim(explode(",", $latlongis)[0])) . "&longitude=" . urlencode(trim(explode(",", $latlongis)[1])) . "&title=" . urlencode($tleis) . "&subtitle=" . urlencode($subtleis) . "\"");
} else {
echo "\n" . $prephp . ' ' . $postphp . ' ' . urlencode(trim(explode(",", $latlongis)[0])) . ' ' . urlencode(trim(explode(",", $latlongis)[1])) . ' ' . urlencode($tleis) . ' '  . urlencode($subtleis);
echo "\n" . exec($prephp . ' ' . $postphp . ' ' . urlencode(trim(explode(",", $latlongis)[0])) . ' ' . urlencode(trim(explode(",", $latlongis)[1])) . ' ' . urlencode($tleis) . ' '  . urlencode($subtleis));
}
}

exit;
?>
