#!/usr/bin/env python3
"""Create synthetic shells for shell-path-spaces.e2e.ts. Usage: script.py SCRATCH.
Run scenarios:prove with SHELL=<SCRATCH>/Jane Doe/.local/bin/fish (unquoted
value containing a space) or SHELL='"<SCRATCH>/my -shell/fish" -l'.
Set ENV=<SCRATCH>/shell-env and T3_PROOF_MARKER=<SCRATCH>/started.
Inherit SHELL,ENV,T3_PROOF_MARKER through T3_SCENARIO_INHERIT_ENV.
"""
from pathlib import Path
import shutil
import sys
root = Path(sys.argv[1]).resolve()
root.mkdir(parents=True, exist_ok=True)
for name in ["Jane Doe/.local/bin/fish", "my -shell/fish"]:
    target = root / name
    target.parent.mkdir(parents=True, exist_ok=True)
    shutil.copy("/bin/dash", target)
for name in ["Jane", "my"]:
    target = root / name
    target.write_text('#!/bin/sh\nprintf "\\nBUG: T3 started the truncated executable instead of the configured shell.\\n"\nexec /bin/dash -i\n')
    target.chmod(0o755)
(root / "shell-env").write_text('case "$0" in\n  *"/Jane Doe/.local/bin/fish"|*"/my -shell/fish") printf "\\nPASS: T3 started the configured shell at its complete path.\\n"; if [ -t 0 ] && [ -t 1 ]; then printf started > "$T3_PROOF_MARKER"; fi ;;\nesac\nPS1="proof$ "\n')
