# usage: applydbstr.py <in dbstr_us.txt> <lines file> <out>   (replace same id^type, insert new ones in id/type order)
import sys
src,lf,dst=sys.argv[1:4]
data=open(src,'rb').read()
nl=b'\r\n' if b'\r\n' in data[:5000] else b'\n'
trail=data.endswith(nl)
L=data.split(nl)
if trail: L=L[:-1]
new={}
for l in open(lf,'rb').read().split(b'\n'):
    if l.strip():
        p=l.split(b'^'); new[(int(p[0]),int(p[1]))]=l
idx={}
for i,l in enumerate(L):
    p=l.split(b'^')
    if len(p)>=3 and p[0].isdigit() and p[1].isdigit(): idx.setdefault((int(p[0]),int(p[1])),i)
rep=ins=0
for k,l in new.items():
    if k in idx:
        if L[idx[k]]!=l: L[idx[k]]=l; rep+=1
for k,l in sorted(new.items()):
    if k in idx: continue
    # insert before the first line with a larger key (file is id/type ordered)
    pos=len(L)
    for i,x in enumerate(L):
        p=x.split(b'^')
        if len(p)>=3 and p[0].isdigit() and p[1].isdigit() and (int(p[0]),int(p[1]))>k: pos=i; break
    L.insert(pos,l); ins+=1
out=nl.join(L)+(nl if trail else b'')
open(dst,'wb').write(out)
print(f"replaced {rep}, inserted {ins}, newline {nl!r}, trailing {trail}")
