#!/bin/sh
# Pri prijavi upise adresu i token DeChaosa u widget na panelu, ako ih carobnjak
# ostavio u ~/.config/tossos/dechaos.token (prvi redak URL, drugi token).
# Bez te datoteke ne radi nista, pa widget ostaje na oglednim podacima.
D="$HOME/.config/tossos/dechaos.token"
[ -r "$D" ] || exit 0
URL=$(sed -n 1p "$D"); TOKEN=$(sed -n 2p "$D")
case "$URL" in https://*) ;; *) exit 0 ;; esac
printf '%s' "$TOKEN" | grep -Eq '^[A-Za-z0-9|_-]+$' || exit 0
Q=$(command -v qdbus6 || command -v qdbus || echo /usr/lib/qt6/bin/qdbus)
for i in $(seq 1 30); do
  "$Q" org.kde.plasmashell /PlasmaShell >/dev/null 2>&1 && break
  sleep 2
done
"$Q" org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript "
for (const p of panels().concat(desktops())) {
  for (const w of p.widgets('eu.tossos.dechaos')) {
    w.currentConfigGroup = ['General'];
    w.writeConfig('apiUrl', '$URL/api/v1/today');
    w.writeConfig('apiToken', '$TOKEN');
  }
}" >/dev/null 2>&1
