<?php
require __DIR__.'/common.php';require __DIR__.'/observe.php';
$worker=json_decode(file_get_contents(__DIR__.'/private/worker-pid.json'),true);$cmd=(string)@file_get_contents('/proc/'.$worker['pid'].'/cmdline');if($cmd!==''&&strpos($cmd,__DIR__.'/index.php')!==false)throw new RuntimeException('Own worker remains');$s=@fsockopen('127.0.0.1',$worker['port'],$n,$e,0.5);if($s){fclose($s);throw new RuntimeException('Port occupied');}
$parent=$lane==='B'?10001:112175;if(strpos((string)file_get_contents('/proc/'.$parent.'/cmdline'),$root.'/public')===false)throw new RuntimeException('Original parent identity');
foreach(['auth.json','current-case.json','current-lease.json']as $f)if(is_file(__DIR__.'/private/'.$f))throw new RuntimeException('Own credential remains');$cache=[];if(is_dir(__DIR__.'/cache')){$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__.'/cache',FilesystemIterator::SKIP_DOTS));foreach($it as $f)if($f->isFile())$cache[]=$f->getPathname();}if($cache)throw new RuntimeException('Own cache remains');
$lock=json_decode(file_get_contents(__DIR__.'/source-lock.json'),true);foreach($lock as $f=>$h)if(hash_file('sha256',__DIR__.'/source/application/'.$f)!==$h)throw new RuntimeException('Source drift');$final=json_decode(file_get_contents(__DIR__.'/private/baseline-final.json'),true);$after=$source();if($after!==$final['source'])throw new RuntimeException('Final lease drift');
$save('resource-check.json',['lane'=>$lane,'own_worker_absent'=>true,'own_port_closed'=>true,'own_cache_files'=>0,'own_auth_absent'=>true,'original_parent_alive'=>true,'source959_equal'=>count($lock)===959,'source_lock_sha256'=>hash_file('sha256',__DIR__.'/source-lock.json'),'source_after'=>$after,'included_files'=>$included(),'HTTP'=>0,'DML'=>0,'DDL'=>0]);echo json_encode(['lane'=>$lane,'worker_absent'=>true,'port_closed'=>true,'cache_auth_absent'=>true,'parent_alive'=>true,'source959_equal'=>true,'final_lease_equal'=>true]),PHP_EOL;
