import io

p = 'rig-d2/go-and-silent-controls.sh'
s = io.open(p, 'r', encoding='utf-8', newline='').read()

# the positive-control receipts are written CRLF, because the real ones are: the elevated leg is a
# Windows shell. If the whole-line matching did not strip CR, m2/m5 would go red here.
s = s.replace(
    """printf 'PREPARED\\nNOT_STARTED\\nexecutable_sha256=%s\\ncapture_sha256=%s\\n' "$EXE_SHA" "$CAPTURE_SHA" > "$PREPARED_ACK\"""",
    """printf 'PREPARED\\r\\nNOT_STARTED\\r\\nexecutable_sha256=%s\\r\\ncapture_sha256=%s\\r\\n' "$EXE_SHA" "$CAPTURE_SHA" > "$PREPARED_ACK"   # CRLF: the real receipt is written by a Windows leg""", 1)
s = s.replace(
    """printf 'GO_AUTHORIZED\\nnonce: %s\\n' "$NONCE" > "$RECEIPT\"""",
    """printf 'GO_AUTHORIZED\\r\\nnonce: %s\\r\\n' "$NONCE" > "$RECEIPT"   # CRLF, as a real receipt arrives""", 1)

P = """
echo
echo "== P  MISLEADING TOKENS: a refusal must not read as an agreement =="
# doyle K4QDUUVV. The checks used substring searches, so a receipt saying NOT_PREPARED satisfied
# the search for PREPARED, and NOT_GO_AUTHORIZED satisfied the search for GO_AUTHORIZED. The worst
# direction for this particular mistake: an executor REFUSING would have been read as agreeing.
reset
printf 'NOT_PREPARED\\r\\nNOT_STARTED\\r\\nexecutable_sha256=%s\\r\\ncapture_sha256=%s\\r\\n' "$EXE_SHA" "$CAPTURE_SHA" > "$PREPARED_ACK"
eval "$PREP_CHECK_BLOCK" >/dev/null 2>&1
case "$DIED" in (*PREPARED*) ok 'p1 NOT_PREPARED does not satisfy PREPARED' ;;
                (*) bad "p1 NOT_PREPARED was accepted as PREPARED (died=[$DIED])" ;; esac
reset
printf 'NOT_GO_AUTHORIZED\\r\\nnonce: %s\\r\\n' "$NONCE" > "$RECEIPT"
eval "$GO_BLOCK" >/dev/null 2>&1
want 'p2 NOT_GO_AUTHORIZED does not open the window' "$WENT" ''
case "$DIED" in (*GO_AUTHORIZED*) ok 'p2b and the refusal names the token it required' ;;
                (*) bad 'p2b the refusal does not name the token' ;; esac

echo
echo "== PROCESS DISPOSITION =="""

s = s.replace('\necho\necho "== PROCESS DISPOSITION =="', P, 1)
io.open(p, 'w', encoding='utf-8', newline='').write(s)
print('section P added, positive controls now CRLF')
