#!/usr/bin/env bash
# wbar — run the custom waybar rig (layouts/custom.jsonc + custom.css) as a
# supervised systemd user service, and control it.
#
# Why a service: waybar 0.15.0 segfaults when the LG 4K drops its DisplayPort
# link on power-off (output removed) and again when it comes back (output
# re-added). Launched as a bare scope nothing relaunched it, so the bar was gone
# every morning. As a transient *service* with Restart=always systemd brings it
# back after RestartSec, at both ends of the cycle. StartLimitIntervalSec=0 keeps
# systemd from giving up while the monitor is gone and Hyprland sits on its
# headless fallback output (the default limit of 5 starts per 10 s would
# reproduce the original "stays dead" behavior). RestartSec=3 keeps that
# potential loop cheap; `systemctl --user show dots-waybar.service -p NRestarts`
# after a night says how many restarts a sleep cycle really costs.
#
# The unit is dots-owned on purpose. HyDE's own bar unit
# (hyde-$XDG_SESSION_DESKTOP-bar.service) is masked on rig hosts (README.md) so
# waybar.py can never spawn a second, plain bar — reusing that name would need
# the mask lifted. Under a different name HyDE keeps seeing "no bar", exactly as
# before.
#
# Because a killed waybar now comes straight back, every killall-based control
# goes through here:
#   wbar            restart (what the old `wbar` abbr did)   also: hc.start.bar
#   wbar toggle     Ctrl+Alt+W
#   wbar reload     re-read config + CSS in place (SIGUSR2), no restart
#   wbar start|stop|status
set -euo pipefail

unit="dots-waybar.service"
cfg="$HOME/.config/waybar/layouts/custom.jsonc"
css="$HOME/.config/waybar/layouts/custom.css"

active() { systemctl --user is-active --quiet "$unit"; }

start() {
  active && return 0
  systemctl --user reset-failed "$unit" 2>/dev/null || true
  # A service gets the user manager's environment, not Hyprland's. Forward the
  # session vars app2unit forwards for HyDE's own units, plus GTK scaling
  # (GDK_SCALE=1 is what keeps the bar sized right on the 1.25-scaled output).
  local -a env=()
  local v
  for v in XDG_VTNR XDG_SEAT XDG_SEAT_PATH XDG_SESSION_ID XDG_SESSION_PATH GDK_SCALE GDK_DPI_SCALE; do
    [ -n "${!v:-}" ] && env+=("--setenv=$v=${!v}")
  done
  systemd-run --user --quiet --collect \
    --unit="$unit" --slice=app-graphical.slice --description="waybar (dots custom rig)" \
    --property=Type=exec --property=ExitType=cgroup \
    --property=Restart=always --property=RestartSec=3 --property=StartLimitIntervalSec=0 \
    --property=After=graphical-session.target --property=PartOf=graphical-session.target \
    "${env[@]}" -- waybar -c "$cfg" -s "$css"
}

case "${1:-restart}" in
  start) start ;;
  stop) systemctl --user stop "$unit" ;;
  restart)
    if active; then systemctl --user restart "$unit"; else start; fi
    ;;
  toggle)
    if active; then systemctl --user stop "$unit"; else start; fi
    ;;
  reload) systemctl --user kill -s SIGUSR2 "$unit" ;;
  status) systemctl --user status --no-pager "$unit" ;;
  *)
    echo "usage: wbar [start|stop|restart|toggle|reload|status]  (default: restart)" >&2
    exit 2
    ;;
esac
