import os, pathlib, subprocess, sys
root = pathlib.Path(sys.argv[1]).resolve()
remote = root / sys.argv[2]
remote.mkdir(parents=True, exist_ok=True)
for name in ['key', 'hostkey']:
    if not (root / name).exists():
        subprocess.run(['ssh-keygen', '-q', '-t', 'ed25519', '-N', '', '-f', str(root / name)], check=True)
(root / 'force.sh').write_text(f'#!/bin/sh\nexport HOME={remote}\nexport T3CODE_HOME="$HOME/.t3"\nexport T3CODE_ENVIRONMENT_LABEL="SSH proof"\nexport T3CODE_DEV_SEED=0\nexec /bin/sh -c "$SSH_ORIGINAL_COMMAND"\n')
(root / 'force.sh').chmod(0o755)
(root / 'sshd.conf').write_text(f"""Port 2222
ListenAddress 127.0.0.1
HostKey {root}/hostkey
PidFile {root}/sshd.pid
AuthorizedKeysFile {root}/key.pub
StrictModes no
PasswordAuthentication no
UsePAM no
AllowUsers {os.environ['USER']}
ForceCommand {root}/force.sh
""")
