import subprocess,os,time,pathlib,sys,signal
root=pathlib.Path(__file__).parent;side=sys.argv[1];checkout=sys.argv[2];display=':171' if side=='before' else ':172'
x=subprocess.Popen(['Xvfb',display,'-screen','0','1280x800x24','-nolisten','tcp'],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
env={**os.environ,'DISPLAY':display}
try:
 time.sleep(.5)
 record=subprocess.Popen(['ffmpeg','-y','-loglevel','error','-f','x11grab','-video_size','1280x800','-framerate','10','-i',display,'-c:v','libx264','-preset','ultrafast','-crf','22','-pix_fmt','yuv420p',str(root/('built-'+side+'.mp4'))],stdout=subprocess.DEVNULL,stderr=open(root/(side+'-ffmpeg.log'),'w'),env=env)
 # The terminal displays only the driver's redacted live observations, never raw server logs.
 command='python3 '+str(root/'driver.py')+' '+side+' '+checkout+' | tee '+str(root/('built-'+side+'-public.txt'))+'; sleep 4'
 term=subprocess.Popen(['dbus-run-session','--','gnome-terminal','--wait','--hide-menubar','--geometry=120x40+0+0','--title=Database backup availability proof','--','bash','-c',command],env=env,stdout=subprocess.DEVNULL,stderr=open(root/(side+'-terminal.log'),'w'))
 term.wait(timeout=200)
 record.send_signal(signal.SIGINT);record.wait(timeout=20)
finally:
 x.terminate();x.wait(timeout=10)
