from pathlib import Path
import os,sys,json,shutil,subprocess,time,signal
ROOT=Path('/home/jakub/devel/escapistsrereintroduced');sys.path.insert(0,str(ROOT/'tools'))
from external_addons import policy,prepare_profile
from source_manifest import addon_manifest
from dedicated_native import PROBE
base=Path('/tmp/erri-editor-native');base.mkdir(exist_ok=True)
run=base/sys.argv[1];run.mkdir();source=run/'source';source.mkdir()
for rel in addon_manifest(ROOT):
 dest=source/Path(rel).relative_to('addons/core');dest.parent.mkdir(parents=True,exist_ok=True);shutil.copy2(ROOT/rel,dest)
shutil.copy2('/tmp/erri-takeover-pair-source32/resourceDatabase.rdb',source/'resourceDatabase.rdb')
shutil.copy2('/tmp/erri-editor-probe.c',source/'Scripts/Game/ERR/00_Core/ERR_ZProfileProbe.c')
addons=run/'addons';addons.mkdir();steam=Path.home()/'.local/share/Steam'
prepare_profile(addons,steam/'steamapps/compatdata/1874880/pfx/drive_c/users/steamuser/Documents/My Games/ArmaReforger/addons',policy(ROOT),run/'external-cache.json');(addons/'ERR_Core').symlink_to(source,target_is_directory=True)
(addons/'Nogova').symlink_to('/home/jakub/devel/gabreta/addons/NogovaReforger',target_is_directory=True)
profile=run/'profile/profile';profile.mkdir(parents=True)
(run/'network.py').write_text(PROBE)
runtime=Path('/tmp/erri-takeover-dedicated-runtime')
engine=['/tmp/runtime/ArmaReforgerServer','-server','{469880DFE1B48F95}Missions/Nogova_TerrainTest.conf','-addonsDir',str(addons),'-addons','2528466DE220D91C,70C981A53E02B64F','-profile',str(run/'profile'),'-noBackend','-noSound','-bindIP','127.0.0.1','-bindPort','2001','-maxFPS','30','-logconsole','-noSplash','-noThrow','-disableCrashReporter']
cmd=['bwrap','--unshare-net','--unshare-pid','--unshare-ipc','--unshare-uts','--die-with-parent','--cap-drop','ALL','--ro-bind','/','/','--tmpfs','/tmp','--ro-bind',str(runtime),'/tmp/runtime','--bind',str(run),str(run),'--proc','/proc','--dev','/dev','--chdir','/tmp/runtime','python',str(run/'network.py'),os.readlink('/proc/self/ns/net')]+engine
with (run/'stdout.log').open('w') as log:
 p=subprocess.Popen(cmd,stdout=log,stderr=subprocess.STDOUT,start_new_session=True)
 reason='timeout';start=time.monotonic()
 try:
  while time.monotonic()-start<340:
   files=sorted((run/'profile/logs').glob('logs_*/console.log'));text=files[-1].read_text(errors='replace') if files else ''
   if 'ERR EDITOR COMPLETE' in text:reason='complete';break
   if "Can't compile" in text:reason='compile-failed';break
   if p.poll() is not None:reason='exit';break
   time.sleep(.3)
 finally:
  if p.poll() is None:
   os.killpg(p.pid,signal.SIGTERM)
   try:p.wait(timeout=8)
   except subprocess.TimeoutExpired:os.killpg(p.pid,signal.SIGKILL);p.wait()
  (run/'result.json').write_text(json.dumps({'reason':reason,'exit':p.returncode}));print(reason)
  print('\n'.join(line for line in text.splitlines() if 'ERR EDITOR' in line or 'SCRIPT    (E)' in line))
