#!/usr/bin/env python3
import os, sys, shutil, subprocess
from pathlib import Path
VERSION="0.2.6c"
BASE=Path("/usr/share/sentinelos/status-tray-live-icons/assets")
THEMES=("SentinelOS-Light","SentinelOS-Dark","SentinelOS-Green","SentinelOS-Purple","SentinelOS-Red")
WIFI={"network-wireless-signal-none":"wifi0.svg","network-wireless-signal-weak":"wifi1.svg","network-wireless-signal-ok":"wifi2.svg","network-wireless-signal-good":"wifi3.svg","network-wireless-signal-excellent":"wifi4.svg","network-wireless-acquiring":"wificonnect.svg","network-wireless-connected":"wifi4.svg","network-wireless-disconnected":"wifioff.svg","network-wireless-disabled":"wifioff.svg","network-wireless-offline":"wifioff.svg","network-wireless-encrypted":"wifilock.svg","network-wireless-hotspot":"wifi4.svg","nm-signal-00":"wifi0.svg","nm-signal-25":"wifi1.svg","nm-signal-50":"wifi2.svg","nm-signal-75":"wifi3.svg","nm-signal-100":"wifi4.svg","nm-device-wireless":"wifi3.svg","nm-no-connection":"wifioff.svg","nm-adhoc":"wifi4.svg","nm-secure-lock":"wifilock.svg"}
BT={"bluetooth":"bt.svg","bluetooth-active":"btconnected.svg","bluetooth-connected":"btconnected.svg","bluetooth-disabled":"btoff.svg","bluetooth-off":"btoff.svg","blueman":"bt.svg","blueman-active":"btconnected.svg","blueman-disabled":"btoff.svg","blueman-tray":"bt.svg","blueman-tray-active":"btconnected.svg","blueman-tray-disabled":"btoff.svg","blueman-tray-connected":"btconnected.svg"}
BAT={"battery":"bat100.svg","battery-full":"bat100.svg","battery-good":"bat75.svg","battery-medium":"bat50.svg","battery-low":"bat25.svg","battery-caution":"bat10.svg","battery-empty":"bat0.svg","battery-full-charging":"bat100c.svg","battery-good-charging":"bat75c.svg","battery-medium-charging":"bat50c.svg","battery-low-charging":"bat25c.svg","battery-caution-charging":"bat10c.svg","ac-adapter":"bat100c.svg"}
ALL={}
for d in (WIFI,BT,BAT):
    for k,v in d.items():
        ALL[k]=v; ALL[k+"-symbolic"]=v
def dirs(theme):
    r=Path("/usr/share/icons")/theme
    return [r/"scalable/status",r/"scalable/panel",r/"24x24/status",r/"22x22/status",r/"16x16/status"]
def install_theme(theme):
    r=Path("/usr/share/icons")/theme; s=BASE/theme
    if not r.exists(): return False,f"{theme}: missing icon theme"
    if not s.exists(): return False,f"{theme}: missing assets"
    count=0
    for d in dirs(theme):
        d.mkdir(parents=True,exist_ok=True)
        for name,src in ALL.items():
            sp=s/src
            if sp.exists():
                shutil.copy2(sp,d/(name+".svg")); count+=1
    if Path("/usr/bin/gtk-update-icon-cache").exists():
        subprocess.run(["/usr/bin/gtk-update-icon-cache","-f","-t",str(r)],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
    return True,f"{theme}: installed {count} live-state aliases"
def summary():
    print("SentinelOS Status Tray Live-State Icons"); print("version:",VERSION)
    print("scope: wifi bluetooth battery live status aliases")
    for t in THEMES: print(f"{t}: {'present' if (Path('/usr/share/icons')/t).exists() else 'missing'}")
def verify():
    ok=True
    for t in ("SentinelOS-Light","SentinelOS-Dark"):
        for n in ("network-wireless-signal-excellent","network-wireless-signal-weak","network-wireless-acquiring","network-wireless-disconnected","bluetooth","bluetooth-connected","bluetooth-disabled","battery-full","battery-low","battery-full-charging"):
            p=Path("/usr/share/icons")/t/"scalable/status"/(n+".svg")
            if not p.exists(): print("missing:",p); ok=False
    print("verify: ok" if ok else "verify: failed"); return 0 if ok else 1
def apply(confirm):
    if os.geteuid()!=0: print("ERROR: --apply requires root",file=sys.stderr); return 2
    if confirm!="APPLY_SENTINELOS_STATUS_TRAY_LIVE_ICONS": print("ERROR: missing --confirm APPLY_SENTINELOS_STATUS_TRAY_LIVE_ICONS",file=sys.stderr); return 2
    for t in THEMES:
        ok,msg=install_theme(t); print(msg)
    print("icon_cache: refreshed where available"); print("panel_note: log out/in or restart panel if cached icons remain"); return 0
if __name__=="__main__":
    if len(sys.argv)<2 or sys.argv[1] in ("--help","-h"):
        print("sentinelos-status-tray-live-icons --summary|--verify|--apply --confirm APPLY_SENTINELOS_STATUS_TRAY_LIVE_ICONS"); sys.exit(0)
    if sys.argv[1]=="--summary": summary(); sys.exit(0)
    if sys.argv[1]=="--verify": sys.exit(verify())
    if sys.argv[1]=="--apply":
        c=sys.argv[3] if len(sys.argv)>=4 and sys.argv[2]=="--confirm" else ""
        sys.exit(apply(c))
    print("ERROR: unknown command",file=sys.stderr); sys.exit(2)
