"""Switch an isolated SSH filesystem between real prerequisite failures."""
import os, pathlib, re, shutil, subprocess, sys
root = pathlib.Path(os.environ['PROOF_SSH_ROOT'])
mode = sys.argv[1]
node = root / 'usr/local/bin/node'
if node.exists(): node.unlink()
if mode != 'node-absent':
    source = os.environ['PROOF_NODE_OLD' if mode == 'node-old' else 'PROOF_NODE_SUPPORTED']
    files = [source] + re.findall(r'(/[^\s()]+)', subprocess.check_output(['ldd', source], text=True))
    for source_file in files:
        destination = node if source_file == source else root / source_file.lstrip('/')
        destination.parent.mkdir(parents=True, exist_ok=True)
        shutil.copyfile(source_file, destination)
        destination.chmod(0o755)
