import os,subprocess,hashlib,json
out=[]
def add(s): out.append(str(s))
# keys present in .env (names only)
try:
    data=open("/opt/data/.env","rb").read().decode("utf-8","replace")
    keys=[]
    for ln in data.splitlines():
        ln=ln.strip()
        if not ln or ln.startswith("#") or "=" not in ln: continue
        keys.append(ln.split("=",1)[0])
    add("ENV_KEYS="+",".join(sorted(set(keys))))
except Exception as e: add("ENV_ERR="+repr(e))
# fingerprints of key secrets (sha256, not values)
for k in ["HERMES_BRIDGE_TOKEN","API_SERVER_KEY","AGENTEK_MCP_TOKEN","WEBDAV_PASSWORD"]:
    v=os.environ.get(k)
    if v: add(f"FP {k} sha256={hashlib.sha256(v.encode()).hexdigest()[:16]} len={len(v)}")
    else: add(f"FP {k} ABSENT-in-shell")
# shared config
for p in ["/etc/agentek/shared-config.yaml","/etc/agentek","/run/secrets","/var/run/secrets"]:
    try:
        if os.path.isdir(p): add("DIR "+p+" :: "+",".join(sorted(os.listdir(p))[:40]))
        elif os.path.isfile(p):
            add("FILE "+p+" size=%d"%os.path.getsize(p))
    except Exception as e: add("CHK "+p+" ERR="+repr(e))
# shared config content
try:
    add("SHAREDCFG_BEGIN")
    add(open("/etc/agentek/shared-config.yaml").read()[:4000])
    add("SHAREDCFG_END")
except Exception as e: add("SHAREDCFG_ERR="+repr(e))
# mounts
try:
    add("MOUNTS_BEGIN")
    for ln in open("/proc/self/mountinfo"):
        if "/opt/data" in ln or "agentek" in ln or "secret" in ln.lower(): add(ln.strip()[:300])
    add("MOUNTS_END")
except Exception as e: add("MOUNTS_ERR="+repr(e))
open("/opt/data/workspace/cfg_result.txt","w").write("\n".join(out))
print("DONE")
