from pathlib import Path
from datetime import datetime
path = Path('/srv/sub2api-80/shared/env/sub2api-80.env')
trash = Path('/srv/sub2api-80/.trash')
trash.mkdir(parents=True, exist_ok=True)
ts = datetime.now().strftime('%Y%m%d_%H%M%S')
backup = trash / f'sub2api-80.env_{ts}'
text = path.read_text()
backup.write_text(text)
updates = {
    "SUBLB_IMAGE_STUDIO_GENERATION_API_BASE_URL": "https://www.yzcld.com",
    "SUBLB_IMAGE_STUDIO_GENERATION_API_KEY": "sk-2a3427aef139cc81a6aa9791c79edbe5052df829f75a5acc9eadc372fe383908",
    "SUBLB_IMAGE_STUDIO_GENERATION_API_TIMEOUT_SECONDS": "180",
}
lines = text.splitlines()
seen = set()
out = []
for line in lines:
    if "=" in line and not line.lstrip().startswith("#"):
        key = line.split("=", 1)[0].strip()
        if key in updates:
            out.append(f"{key}={updates[key]}")
            seen.add(key)
            continue
    out.append(line)
for key, value in updates.items():
    if key not in seen:
        out.append(f"{key}={value}")
path.write_text("\n".join(out) + "\n")
print(path)
print(backup)
