"""Scratch SSH fixture used by the proof; run in an empty scratch directory.
Usage: python setup.py SCRATCH
Then run sshd -D -e -f SCRATCH/sshd/sshd_config in a tracked terminal.
SetEnv HOME ends in remote-base; change it to remote-head and restart sshd for head.
Build apps/web with vp run build in each checkout first.
Copy driver.ts to packages/ssh/src/proof-driver.ts in each checkout.
Run: python record.py base BASE_CHECKOUT
Then: python record.py head HEAD_CHECKOUT
No existing SSH configuration or T3 state is used.
"""
from pathlib import Path
import subprocess,os,sys,getpass,shutil
p=Path(sys.argv[1]).resolve()
for d in ['sshd','remote-base','remote-head','bin']: (p/d).mkdir(exist_ok=True)
for key in ['host','client']:
 subprocess.run(['ssh-keygen','-q','-t','ed25519','-N','','-f',str(p/'sshd'/key)],check=True)
(p/'sshd/authorized_keys').write_text((p/'sshd/client.pub').read_text())
(p/'sshd/sshd_config').write_text(f"""Port 2299
ListenAddress 127.0.0.1
HostKey {p}/sshd/host
PidFile {p}/sshd/pid
AuthorizedKeysFile {p}/sshd/authorized_keys
StrictModes no
PasswordAuthentication no
KbdInteractiveAuthentication no
UsePAM no
AllowUsers {getpass.getuser()}
SetEnv HOME={p}/remote-base
""")
(p/'sshd/client_config').write_text(f"""Host devbox
 HostName 127.0.0.1
 Port 2299
 User {getpass.getuser()}
 IdentityFile {p}/sshd/client
 IdentitiesOnly yes
 StrictHostKeyChecking no
 UserKnownHostsFile /dev/null
 LogLevel ERROR
""")
(p/'bin/ssh').write_text(f'#!/bin/sh\nexec /usr/bin/ssh -F {p}/sshd/client_config "$@"\n')
os.chmod(p/'bin/ssh',0o755)
for side in ['base','head']:
 bindir=p/('remote-'+side)/'bin';bindir.mkdir(exist_ok=True)
 (bindir/'node').symlink_to(shutil.which('node'))
