#!/bin/bash

# Find all the audio sinks but exit if there are none
sinks=($(wpctl status | sed -n '/Sinks:/,/Sources:/p' | grep -E '^\s*│\s+\*?\s*[0-9]+\.' | sed -E 's/^[^0-9]*([0-9]+)\..*/\1/'))
[ ${#sinks[@]} -eq 0 ] && exit 1

# Find current audio sink
current=$(wpctl status | sed -n '/Sinks:/,/Sources:/p' | grep '^\s*│\s*\*' | sed -E 's/^[^0-9]*([0-9]+)\..*/\1/')

# Find the next sink (looping around in the list)
for i in "${!sinks[@]}"; do
  [ "${sinks[$i]}" = "$current" ] && next=${sinks[$(((i + 1) % ${#sinks[@]}))]}
done
next=${next:-${sinks[0]}}

# Set the next sink and ensure it's not muted
wpctl set-default "$next"
wpctl set-mute "$next" 0

