"""Generate disposable loopback SSH configuration beside the driver; never upload generated keys.
Run on Linux as the current SSH login user, then run:
  headless-desktop node driver.mjs base /path/to/base-checkout
  headless-desktop node driver.mjs head /path/to/head-checkout
Build each checkout with vp i and vp run build:desktop:bundle first.
Install playwright-core under ./tools (npm install --prefix ./tools playwright-core).
"""
from pathlib import Path
import getpass
import subprocess
r = Path(__file__).resolve().parent
for name in ('host-key', 'client-key'):
    subprocess.run(['ssh-keygen', '-q', '-t', 'ed25519', '-N', '', '-f', str(r / name)], check=True)
(r / 'authorized_keys').write_text((r / 'client-key.pub').read_text())
(r / 'remote-command').write_text('''#!/bin/sh
export HOME="$1"
export SHELL=/bin/dash
unset T3CODE_HOME T3_SERVICE_LAUNCHER_CONTEXT T3_BOOT_SERVICE_UNIT
exec /bin/sh -c "$SSH_ORIGINAL_COMMAND"
''')
(r / 'remote-command').chmod(0o755)
(r / 'sshd_config').write_text(f'''Port 22495
ListenAddress 127.0.0.1
HostKey {r}/host-key
PidFile {r}/sshd.pid
AuthorizedKeysFile {r}/authorized_keys
StrictModes no
PasswordAuthentication no
KbdInteractiveAuthentication no
UsePAM no
PermitRootLogin no
AllowUsers {getpass.getuser()}
ForceCommand {r}/remote-command
''')
