#!/usr/bin/env bash
# Volume via wireplumber (wpctl).
sink="@DEFAULT_AUDIO_SINK@"

case "${1:-get}" in
  get)
    raw=$(wpctl get-volume "$sink" 2>/dev/null)   # e.g. "Volume: 0.95 [MUTED]"
    vol=$(printf '%s' "$raw" | awk '{print $2}')
    perc=$(awk -v v="${vol:-0}" 'BEGIN{printf "%d", v*100 + 0.5}')
    muted=false
    printf '%s' "$raw" | grep -q MUTED && muted=true
    if [ "$muted" = true ] || [ "$perc" -eq 0 ]; then icon="󰖁"
    elif [ "$perc" -lt 50 ]; then icon="󰕿"
    else icon="󰕾"; fi
    jq -nc --argjson p "$perc" --argjson m "$muted" --arg i "$icon" \
      '{percent:$p,muted:$m,icon:$i}'
    ;;
  set)  wpctl set-volume -l 1 "$sink" "${2:-0}%";;
  mute) wpctl set-mute "$sink" toggle;;
esac
