import re,sys
D=open('/home/user/mirasim-proxy/dist/server.decoded.js','rb').read().decode('latin-1')

def defn(name, limit=1, span=1400):
    """print body of `function NAME(` occurrences"""
    out=[]
    for m in re.finditer(r'function\s+'+re.escape(name)+r'\s*\(', D):
        out.append(D[m.start():m.start()+span])
        if len(out)>=limit: break
    return out

def ctx(pat, span=600, limit=6, regex=False):
    out=[]
    it = re.finditer(pat if regex else re.escape(pat), D)
    for m in it:
        out.append((m.start(), D[max(0,m.start()-span):m.end()+span]))
        if len(out)>=limit: break
    return out

if __name__=='__main__':
    mode=sys.argv[1]
    if mode=='def':
        for b in defn(sys.argv[2], int(sys.argv[3]) if len(sys.argv)>3 else 1,
                      int(sys.argv[4]) if len(sys.argv)>4 else 1400):
            print("-"*90); print(b)
    else:
        for off,b in ctx(sys.argv[2], int(sys.argv[3]) if len(sys.argv)>3 else 600,
                         int(sys.argv[4]) if len(sys.argv)>4 else 6):
            print("-"*90); print(f"@{off}"); print(b)
