#!/usr/bin/env bash
# Launch the eww bar on every monitor. Safe to call from Hyprland autostart.
# If eww isn't installed yet, fall back to waybar so you're never bar-less.
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

if ! command -v eww >/dev/null 2>&1; then
  echo "eww not found — falling back to waybar" >&2
  exec waybar
fi

# niri helpers — provides $WM (=niri), wm_outputs, wm_blur_layer, …
. "$DIR/scripts/wm.sh"

# Defensive: if GDK_SCALE is exported anywhere, unset it so eww uses native
# Wayland fractional scaling (GTK >= 3.24.49) and renders crisp at scale 1.25.
# (The niri config deliberately does NOT set GDK_SCALE.)
unset GDK_SCALE GDK_DPI_SCALE

# Make sure helper scripts are executable.
chmod +x "$DIR"/scripts/*.sh 2>/dev/null

# Stop waybar (we're replacing it).
pkill -x waybar 2>/dev/null

# HARD-restart the daemon. A plain `eww close-all` is NOT enough: across daemon
# hiccups/restarts the compositor keeps old layer surfaces mapped while the daemon
# forgets them — orphans that pile up (e.g. "5 header bars") and that eww can no
# longer close (it doesn't know their ids). Killing the daemon makes the
# compositor drop ALL its surfaces, so we always start from a truly clean slate.
eww kill 2>/dev/null
pkill -9 -f '[e]ww daemon' 2>/dev/null
sleep 0.5
# Give it time to initialise (cold start ~2s) before opening windows, or the open
# calls no-op against a not-yet-ready server.
eww daemon >/dev/null 2>&1 &
sleep 2

eww close-all 2>/dev/null

# Use the compositor's active-output count as 0-based eww --screen indices.
n=$(wm_outputs)
[ -z "$n" ] || [ "$n" -lt 1 ] 2>/dev/null && n=1
m=0
while [ "$m" -lt "$n" ]; do
  eww open bar --id "bar-$m" --screen "$m" 2>/dev/null
  m=$((m+1))
done

# Auto-hide dock + frosted-glass blur for the gtk-layer-shell namespace.
# (Blur/rounding/shadow live in ~/.config/niri/config.kdl as a layer-rule;
# wm_blur_layer is a no-op on niri.)


wm_blur_layer "gtk-layer-shell"
eww close dock dock-backdrop dock-menu 2>/dev/null
# Auto-hide: a thin left-edge hover trigger requests "show"; the dock keeps itself
# alive on hover and requests "leave" on hoverlost. The actual open/close + timing
# is done by the `dock.sh watch` DAEMON below — NOT in the eww hover handlers,
# because eww kills a handler the moment it sleeps (which would strand the dock /
# a backdrop and lock out the pointer). Handlers only write intent to a state file.
eww update dock-open=false 2>/dev/null
eww close dock dock-catcher 2>/dev/null          # start hidden (mapped on hover)
printf 'hidden' > "$HOME/.cache/eww/.dock-hide-token" 2>/dev/null
eww open dock-trigger 2>/dev/null
pkill -f '[d]ock\.sh watch' 2>/dev/null
"$DIR/scripts/dock.sh" watch >/dev/null 2>&1 &

# Accent: mirror TANINUX's gui.json into eww's $accent and keep it in sync.
pkill -f "accent.sh watch" 2>/dev/null
"$DIR/scripts/accent.sh" watch >/dev/null 2>&1 &

# UI font: follow the GNOME/TANINUX appearance font (gsettings font-name).
pkill -f "font.sh watch" 2>/dev/null
"$DIR/scripts/font.sh" watch >/dev/null 2>&1 &

# Panel/Dock settings: mirror TANINUX's panel.json (icon size, modules, …).
pkill -f "panelcfg.sh watch" 2>/dev/null
"$DIR/scripts/panelcfg.sh" watch >/dev/null 2>&1 &

# Safety net: close orphaned backdrops that would otherwise swallow all clicks.
# Rewritten for niri — deterministic owner-pairing via `eww active-windows`
# (no niri geometry, no daemon kill), so it no longer over-fires. See guard.sh.
pkill -f "scripts/guard.sh" 2>/dev/null
"$DIR/scripts/guard.sh" >/dev/null 2>&1 &

# Spotlight: clipboard history watcher (cliphist) + warm the app index.
if command -v cliphist >/dev/null 2>&1; then
  pkill -f "wl-paste --watch cliphist store" 2>/dev/null
  wl-paste --type text  --watch cliphist store >/dev/null 2>&1 &
  wl-paste --type image --watch cliphist store >/dev/null 2>&1 &
fi
"$DIR/scripts/spotlight.sh" reindex >/dev/null 2>&1 &

# --- Notifications: tiramisu (headless) + our eww UI, replacing mako --------
# mako is D-Bus-activated, so we kill it and immediately let tiramisu grab the
# org.freedesktop.Notifications name — while tiramisu holds it, D-Bus won't
# re-activate mako. If tiramisu isn't installed yet, leave mako alone.
if command -v tiramisu >/dev/null 2>&1; then
  pkill -x mako 2>/dev/null
  [ -f "$HOME/.cache/eww/dnd" ] || echo false > "$HOME/.cache/eww/dnd"
  pkill -f "notifications.sh listen" 2>/dev/null
  "$DIR/scripts/notifications.sh" listen >/dev/null 2>&1 &
  sleep 0.3
  "$DIR/scripts/notifications.sh" refresh 2>/dev/null
fi
