#!/usr/bin/env python3
"""Deploy dist/ to lern-digital.de/7kaa/ via SFTP (IONOS shared)."""
import paramiko, os, sys, time

HOST="home539747457.1and1-data.host"; USER="u78172133"; PW="_uSzSKNBKve_S79"
REMOTE_DIR="clickandbuilds/lerndigital277331/7kaa"
DIST=os.path.join(os.path.dirname(os.path.abspath(__file__)),"dist")
FILES=[".htaccess","index.html","7kaa.html","7kaa.js","7kaa.wasm","LICENSE.txt","SOURCE.txt",
       "manifest.json","sw.js","icon-192.png","icon-512.png","7kaa-source.zip","7kaa.data"]
if len(sys.argv)>1 and sys.argv[1]=="nodata":
    FILES=[f for f in FILES if f!="7kaa.data"]   # 7kaa.data unchanged -> skip the 103MB upload

t=paramiko.Transport((HOST,22)); t.connect(username=USER,password=PW)
sftp=paramiko.SFTPClient.from_transport(t)
try: sftp.mkdir(REMOTE_DIR)
except IOError: pass
for f in FILES:
    lp=os.path.join(DIST,f); rp=REMOTE_DIR+"/"+f
    sz=os.path.getsize(lp); t0=time.time()
    last=[0]
    def cb(done,total,_last=last,_t0=t0,_f=f,_sz=sz):
        pct=int(done*100/total) if total else 0
        if pct>=_last[0]+10:
            _last[0]=pct; print(f"  {_f}: {pct}% ({done//1024//1024}/{total//1024//1024}MB)",flush=True)
    print(f"uploading {f} ({sz//1024}KB)…",flush=True)
    sftp.put(lp,rp,callback=cb)
    print(f"  {f} done in {int(time.time()-t0)}s",flush=True)
sftp.close(); t.close()
print("DEPLOY OK -> https://lern-digital.de/7kaa/7kaa.html")
