import os
new_pw = open("/tmp/amd-agent-newpassword.txt").read().strip()
path = "/home/claude/.config/amd-agent/credentials.env"
lines = open(path).read().splitlines()
out = []
found = False
for line in lines:
    if line.startswith("AMD_PASSWORD="):
        out.append("AMD_PASSWORD=" + new_pw)
        found = True
    else:
        out.append(line)
if not found:
    out.append("AMD_PASSWORD=" + new_pw)
tmp = path + ".tmp"
with open(tmp, "w") as f:
    f.write("\n".join(out) + "\n")
os.chmod(tmp, 0o600)
os.replace(tmp, path)
os.chmod(path, 0o600)
# verify mode and that password length matches (no value print)
st = os.stat(path)
print("mode:", oct(st.st_mode & 0o777))
# confirm stored pw length equals new pw length
stored = None
for line in open(path).read().splitlines():
    if line.startswith("AMD_PASSWORD="):
        stored = line.split("=",1)[1]
print("stored_matches_new:", stored == new_pw, "len:", len(stored or ""))
# delete temp password file
os.remove("/tmp/amd-agent-newpassword.txt")
print("temp_file_exists_after_delete:", os.path.exists("/tmp/amd-agent-newpassword.txt"))
print("CREDDONE")
