#!/bin/bash

# Wait a bit for the device to be ready
sleep 2

# Function to find the sink
find_sink() {
    pactl list short sinks | grep "alsa_output.usb-BEHRINGER_X18" | awk '{print $2}'
}

# Wait until the sink is found
SINK_NAME=""
while [ -z "$SINK_NAME" ]; do
    SINK_NAME=$(find_sink)
    if [ -n "$SINK_NAME" ]; then
        echo "Sink found: $SINK_NAME"
    else
        echo "Sink not found. Retrying..."
        sleep 2 # Wait for 2 seconds before trying again
    fi
done

# Force to stereo (2 channels) once the sink is found
if [ -n "$SINK_NAME" ]; then
    if ! pactl list short modules | grep -q "sink_name=xr18_stereo"; then
        pactl load-module module-remap-sink sink_name=xr18_stereo master=$SINK_NAME channels=2 channel_map=front-left,front-right
        pactl set-default-sink xr18_stereo
    fi
fi
