<?php
declare(strict_types=1);
$lane=$argv[1]??'';$mode=$argv[2]??'';
if(!in_array($lane,['B','C'],true)||!in_array($mode,['preflight','seed','launch','run','stop'],true))exit(78);
$pid=$lane==='B'?10001:112175;
$root=$lane==='B'?'/tmp/zhct-http-store-mysql.dQTTBP':'/tmp/zhct-dm-poc-20260928.fcpiiN/store';
if(strpos((string)file_get_contents('/proc/'.$pid.'/cmdline'),$root.'/public')===false)throw new RuntimeException('Parent drift');
$env=[];foreach(explode("\0",(string)file_get_contents('/proc/'.$pid.'/environ')) as $v){$p=explode('=',$v,2);if(count($p)===2)$env[$p[0]]=$p[1];}
if(($env['POC_EXTERNAL_STUBS']??'')!=='1'||($env['POC_REAL_QUEUE']??'0')!=='0')throw new RuntimeException('Unsafe environment');
$port=$lane==='B'?18381:18382;$env['CITY_LANE']=$lane;$env['CITY_PARENT']=$root;$env['CITY_ASSETS']=__DIR__;
$args=[$lane==='B'?'/usr/local/php7.3/bin/php':'/www/server/php/73/bin/php'];
if($lane==='C')array_push($args,'-c','/www/server/php/73/etc/php-cli.ini','-d','extension=/home/dmdba/dmdbms/drivers/php_pdo/libphp73_dm.so','-d','extension=/home/dmdba/dmdbms/drivers/php_pdo/php73_pdo_dm.so');
array_push($args,'-d','display_errors=0','-d','log_errors=0');
if($mode==='stop'){
    $p=(int)file_get_contents(__DIR__.'/worker.pid');
    if(strpos((string)@file_get_contents('/proc/'.$p.'/cmdline'),__DIR__.'/index.php')===false)throw new RuntimeException('Not our worker');
    posix_kill($p,SIGTERM);usleep(200000);$s=@fsockopen('127.0.0.1',$port,$n,$e,1);$listening=is_resource($s);if($s)fclose($s);
    echo json_encode(['worker_stopped'=>!is_dir('/proc/'.$p),'port_listening'=>$listening]),"\n";exit($listening?2:0);
}
if($mode==='launch'){
    if($s=@fsockopen('127.0.0.1',$port,$n,$e,1)){fclose($s);throw new RuntimeException('Port occupied');}
    if(!is_dir(__DIR__.'/source/application')){
        mkdir(__DIR__.'/source',0700,true);
        $copy='cp -R '.escapeshellarg($root.'/application').' '.escapeshellarg(__DIR__.'/source/application');
        $p=proc_open($copy,[1=>STDOUT,2=>STDERR],$pipes);if(proc_close($p)!==0)throw new RuntimeException('Private copy failed');
        $p=proc_open('tar -xzf '.escapeshellarg(__DIR__.'/overlay.tar.gz').' -C '.escapeshellarg(__DIR__.'/source'),[1=>STDOUT,2=>STDERR],$pipes);if(proc_close($p)!==0)throw new RuntimeException('Overlay failed');
    }
    array_push($args,'-S','127.0.0.1:'.$port,'-t',__DIR__,__DIR__.'/index.php');
    $cmd='nohup '.implode(' ',array_map('escapeshellarg',$args)).' </dev/null >/dev/null 2>&1 & echo $!';
    $p=proc_open($cmd,[1=>['pipe','w'],2=>STDERR],$pipes,null,$env);$out=stream_get_contents($pipes[1]);fclose($pipes[1]);$rc=proc_close($p);
    file_put_contents(__DIR__.'/worker.pid',trim($out));echo $out;exit($rc);
}
if($mode==='seed')$env['CITY_FIXTURE_APPROVAL']='CREATE_CITY_TEST_TREE_V1';
array_push($args,__DIR__.'/'.($mode==='seed'?'seed.php':'run.php'),$mode);
$p=proc_open(implode(' ',array_map('escapeshellarg',$args)),[1=>STDOUT,2=>STDERR],$pipes,null,$env);exit(proc_close($p));
