#!/bin/bash
set -e

# Reload waybar in place when it's already running. SIGUSR2 makes waybar
# re-read its config.jsonc and CSS (including @import-ed files like
# ~/.config/nomarchy/current/theme/waybar.css that theme-switch rewrites)
# without destroying its layer-shell surface. A full systemctl restart
# tears the surface down and creates a new one back-to-back, leaving a
# frame or two where Hyprland hasn't cleared the dead surface yet — that's
# the "old on top of new with artifacts" you see after a theme switch on
# the live ISO.
if pgrep -x waybar >/dev/null; then
  exec pkill -SIGUSR2 -x waybar
fi

# Not running yet — start it. Prefer the HM systemd user unit
# (programs.waybar.systemd.enable = true). `systemctl cat` returns
# non-zero when the unit doesn't exist; `list-unit-files` does not.
if systemctl --user cat waybar.service >/dev/null 2>&1; then
  exec systemctl --user start waybar.service
fi

# Fallback for systems where waybar isn't managed by systemd.
nomarchy-restart-app waybar
