import io, sys, re
p = sys.argv[1]
d = io.open(p, 'rb').read()
nl = b'\r\n' if b'\r\n' in d else b'\n'
lines = d.split(nl)
out, state, blocks = [], 0, 0
for ln in lines:
    if ln.startswith(b'<<<<<<< '):
        state, blocks = 1, blocks + 1; continue
    if state == 1 and ln.startswith(b'======='):
        state = 2; continue
    if state == 2 and ln.startswith(b'>>>>>>> '):
        state = 0; continue
    if state == 2:
        continue  # theirs dropped
    out.append(ln)
res = nl.join(out)
assert b'<<<<<<<' not in res and b'>>>>>>>' not in res and b'\n=======' not in res, 'markers remain'
io.open(p, 'wb').write(res)
print(f'resolved {blocks} blocks (ours kept), terminator={"CRLF" if nl==b"\r\n" else "LF"}, bytes {len(d)} -> {len(res)}')
