#!/bin/sh
set -eu

case ${1:-} in
    --toggle)
        pkill -x -USR1 waybar 2>/dev/null || true
        exit 0
        ;;
    --reload)
        pkill -x -USR2 waybar 2>/dev/null || true
        exit 0
        ;;
esac

profile=${1:-}
if [ -z "$profile" ]; then
    desktop=$(printf '%s' "${XDG_CURRENT_DESKTOP:-${XDG_SESSION_DESKTOP:-}}" | tr '[:upper:]' '[:lower:]')
    case "$desktop" in
        *hyprland*) profile=hyprland ;;
        *niri*) profile=niri ;;
        *labwc*) profile=labwc ;;
        *sway*) profile=sway ;;
        *) profile=generic ;;
    esac
fi

case "$profile" in
    sway|labwc|niri|hyprland|generic) ;;
    *)
        echo "unknown CatOS Waybar profile: $profile" >&2
        exit 2
        ;;
esac

config_root=${XDG_CONFIG_HOME:-$HOME/.config}/catos-waybar
config="$config_root/$profile.json"
style="$config_root/style.css"
[ -f "$config" ] || config="/usr/share/catos/waybar/profiles/$profile.json"
[ -f "$style" ] || style=/usr/share/catos/waybar/themes/catos/style.css

exec waybar -c "$config" -s "$style"
