#!/usr/bin/env python3
"""Capture the real terminal, not a rendered mockup. Run with headless-desktop."""
import subprocess as sp, os, sys, pathlib, time
out=pathlib.Path(sys.argv[2]); out.parent.mkdir(parents=True,exist_ok=True)
# Keep recorder logs and video outside the new run directory until the driver creates it.
video=out.parent/(out.name+'.mp4')
log=open(out.parent/(out.name+'-recorder.log'),'w')
rec=sp.Popen(['ffmpeg','-y','-f','x11grab','-video_size','1920x1080','-framerate','10','-i',os.environ['DISPLAY'],'-c:v','libx264','-preset','ultrafast','-crf','22','-pix_fmt','yuv420p',str(video)],stdout=log,stderr=sp.STDOUT)
try:
    term=sp.run(['gnome-terminal','--wait','--geometry=145x46','--hide-menubar','--zoom=1.25','--','python3',str(pathlib.Path(__file__).parent/'prove.py'),*sys.argv[1:]])
    time.sleep(1)
finally:
    rec.terminate(); rec.wait(timeout=15); log.close()
sys.exit(term.returncode)
