#!/bin/bash

SINKS=$(pactl list short sinks | awk '{print $2}')

declare -A sink_map

options=""
first=true
while read -r sink; do
    device=$(echo "$sink")
    device_name=$(pactl list sinks | grep -A 20 "$device" | grep "Description:" | awk -F: '{print $2}' | xargs)

    if [ "$first" = true ]; then
        options+="$device_name"
        first=false
    else
        options+="\n$device_name"
    fi
    sink_map["$device_name"]="$device"
done <<< "$SINKS"

CHOSEN=$(echo -e "$options" | dmenu -l 10 -i -p "Select Audio Output")

if [ -n "$CHOSEN" ]; then
    CHOSEN_SINK=${sink_map["$CHOSEN"]}
    pactl set-default-sink "$CHOSEN_SINK"

    for stream in $(pactl list short sink-inputs | awk '{print $1}'); do
        pactl move-sink-input "$stream" "$CHOSEN_SINK"
    done
fi

