#!/bin/bash

# first argument should be the sink id (either 0 or 1 in most cases)

WINDOW_ACTIVE_PID=$(xdotool getactivewindow getwindowpid);

CLIENT_ID=-1;
while read -r line; do
    if [[ $line == Client* ]]
    then
        CLIENT_ID=${line#*#}
    fi
    if [[ $line == *application.process.id* ]]
    then
        CLIENT_PID=${line#*'"'};
        CLIENT_PID=${CLIENT_PID%'"'*};
        if [[ $CLIENT_PID == $WINDOW_ACTIVE_PID ]]
        then
            break;
        fi
    fi
done <<< "$(pactl list clients)"

if [[ $CLIENT_ID -lt 0 ]]
then
    exit
fi

SINK_INPUT_ID=$(pactl list short sink-inputs | awk -v id="$CLIENT_ID" '$3 == id {print $1}')

# then we could use pactl list short sinks,
# but analog-stereo should always be 0 and hdmi-stereo should always be 1

pactl move-sink-input $SINK_INPUT_ID $1
