#!/bin/sh
# Simple rofi script for changing default audio sink

audio_info="$(wpctl status \
    | awk '/Audio/,/Video/ {print}')"

case $1 in
    sink)
        section="$(echo "$audio_info" \
            | awk '/Sinks:/,/Sink endpoints:/ {print}')"
        ;;
    source)
        section="$(echo "$audio_info" \
            | awk '/Sources:/,/Source endpoints:/ {print}')"
        ;;
    *)
        echo "Aguments must be one of (sink|source)."
        exit 1
        ;;
esac

devices_and_numbers="$(echo "$section" \
    | tail -n +2 | head -n -2 | cut -c 10-)"

devices="$(echo "$devices_and_numbers" \
    | awk -F'.' '{print $2}' \
    | awk -F'[' '{print $1}' \
    | cut -c 2-)"

lines="$(echo "$devices" | wc -l)"
chosen="$(echo "$devices" | fuzzel -l $lines --dmenu -p "Select audio $1:" -i)"
if [ -z "$chosen" ]; then
    # Exited without selecting
    exit
fi

number="$(echo "$devices_and_numbers" \
    | grep "$chosen" \
    | awk '{print $1}' \
    | sed 's/\./ /')"

wpctl set-default $number

