#!/bin/zsh -feu

zmodload zsh/regex

source /usr/share/mmc-shell/mmc-functions.sh
colors

sink_number()
{
    pactl list short sinks|grep -e $1 | cut -f 1


    # pactl list clients
    # -> number #1206
    # pactl list sink-inputs
    # Sink Input #268
    # ....
    # Client: 1196

    # so:
    # pactl move-sink-input 268  alsa_output.pci-0000_00_1b.0.analog-stereo
}

usage()
{
    cat <<EOF
usage: ${0##*/} [-c N] [+-pbsh] [-i name] [-l]
-c cardNR
-b builtin

-B bluetooth
-F hands-free-profile on bluetooth
-p  phones
-s  speakers
-u  USB phones

-i [name] custom sink? ( pactl list short sinks)

-l list sinks

-d debug
*****
Pure pulse-audio switching between sinks.
No alsa access.
EOF
}


move_to()
{
    local sink=$1
    # sink_input=$(pactl list short sink-inputs|cut -f 1)
    (
        if [[ $debug = yes ]]; then
            set -x
        fi
        pactl set-default-sink $sink

        pactl list short sink-inputs|cut -f 1 |
            while read si
            do
                optional_traced_exec pactl move-sink-input $si $sink
            done
    )
}

activate_profile()
{
    local CARD=$1
    local PROFILE=$2

    (
        set -x
        pactl set-card-profile $CARD $PROFILE
    )
}

debug=no

# pactl list cards |grep alsa.long_card_name = "Dell-WD15-Dock"
if lsusb|grep 0bda:4014 >/dev/null; then
    WD15_DOCK=y
#    WD15_DOCK=n
else
    WD15_DOCK=n
fi

WD15_SINK_REGEXP='USB.*Dock_0'
# WD15_SINK_REGEXP='USB.*Dock_1'
WD15_SINK_REGEXP='Generic_USB_Audio_200901010001'

wd15card="alsa_card.usb-Generic_USB_Audio_200901010001-00"

# usb_phones='alsa_output.usb-Plantronics_Plantronics_C320-M_DC07CFE9246B394C99F2ECEDA32A7A3F-00.analog-stereo'
usb_phones='alsa_output.usb-Plantronics_Plantronics_Blackwire_325.1_B7BAE642E3D4304EA84DE1E1FC01BBA3-00.analog-stereo'


find_pch()
{
    foreach f (/proc/asound/card?/id) {
        if [[ $(cat $f) -regex-match 'PCH.*' ]];
        then
            # foreach f (/proc/asound/card?/id) { if [[ $(cat $f) -regex-match 'PCH.*' ]]; then echo
            CARD=${${f%/id}#/proc/asound/card}
            ;fi
    }
}

CARD=1
find_pch

while getopts :bBc:dFhi:lpstux OPT; do
    case $OPT in
        d)
            debug=yes
            ;;
        t)
            # find bluetooth
            # set profile
            # set as default sink
            #   source

            ;;
        F | B)
            card=$(pactl list short cards |grep bluez| cut -f 2)

            profile=a2dp_sink
            # handsfree_head_unit
            pactl set-card-profile $card $profile


            bluetooth_sink=$(pactl list short sinks| cut -f 2|- grep bluez)
            # pactl list cards
            # Name: bluez_card.80_C3_BA_2A_08_92
            # device.description = "MOMENTUM 4"
            # cards=$(pactl list short cards|grep bluez)
            # pactl set-card-profile 22 a2dp_sink
            # pactl set-card-profile bluez_card.80_C3_BA_2A_08_92  handsfree_head_unit
            # bluetoothctl connect '00:1B:66:C0:D0:41'
            # move_to "bluez_sink.E8_07_BF_3E_41_06.headset_head_unit"
            move_to $bluetooth_sink
            # bluez_output.00_16_94_1E_BA_C0.1
            # 450: "bluez_sink.00_1B_66_C0_D0_41.a2dp_sink"
            # bluez_sink.00_1B_66_C0_D0_41.handsfree_head_unit
            ;;
        c)
            CARD=$OPTARG
            ;;
        p|+p)
            if [[ $WD15_DOCK = y ]]; then
                activate_profile $wd15card \
                                 "output:analog-stereo-headphone+input:analog-stereo-mic" || : ignore
                headphones=$(optional_traced_exec sink_number $WD15_SINK_REGEXP)
                # headphones="alsa_output.usb-Generic_USB_Audio_200901010001-00.HiFi__hw_Dock__sink"
                move_to $headphones
            else
                sink=alsa_output.pci-0000_00_1f.3.analog-stereo
                # fixme: this is a list!
                pactl set-sink-port $sink analog-output-headphones
                move_to $sink
                audio-select -c $CARD -p
            fi
            ;;
        b|+b)
            # before precision 3530?
            sink="alsa_output.pci-0000_00_1b.0.analog-stereo"
            # precision 3530:
            sink=alsa_output.pci-0000_00_1f.3.analog-stereo
            builtin=$(optional_traced_exec sink_number $sink)
            pactl set-sink-port $sink analog-output-speaker

            audio-select -c $CARD -b
            # $builtin
            move_to $sink
            ;;
        s|+s)
            if [[ $WD15_DOCK = y ]]
            then
                info "so we have the dock -- turn on the Line (speaker profile), and get the sink"
                activate_profile $wd15card HiFi
                # "output:analog-stereo-speaker"
                speakers=$(optional_traced_exec sink_number $WD15_SINK_REGEXP)
                info "now move $speakers"
                move_to alsa_output.usb-Generic_USB_Audio_200901010001-00.HiFi__Line__sink
                # $speakers
            else
                # audio-select -c $CARD -j
                # e-dock
                # profile pactl set-card-profile
                card_name=pci-0000_00_1f.3
                card=alsa_card.$card_name
                card_output=analog-stereo
                pactl set-card-profile $card "output:${card_output}+input:${card_output}"
                # alsa_card.pci-0000_00_1b.0 output:analog-stereo+input:analog-stereo
                sink="alsa_output.${card_name}.${card_output}"
                # pactl set-sink-port $sink analog-output-lineout
                # analog-output-speaker
                pactl set-sink-port $sink analog-output-headphones
                pactl set-sink-mute $sink off
                #
                move_to $sink
            fi
            ;;
        u) # usb headphones
            move_to $usb_phones
            ;;
        h|+h)
            usage
            exit 0
            ;;
        x)
            set -x
            ;;
        i)
            move_to $OPTARG
            ;;
        l)
            LOG_STEP "sinks-inputs"
            pactl list short sink-inputs
            LOG_STEP "sinks:"
            pactl list short sinks
            ;;
        *)
            usage >&2
            exit 2
    esac
done
shift OPTIND-1
OPTIND=1


erase_configuration_file()
{
    local tdb_file=$1

    drop=(sink-input-by-application-name:MPlayer
          sink-input-by-application-name:mpg123
          sink-input-by-application-name:gthumb
          'sink-input-by-application-name:libao[ogg123]'
         )

    foreach key ($drop) {
        if false; then
            tdbtool $tdb_file delete "$key"
        fi
    }
}


erase_configuration_file  ~/.config/pulse/*-stream-volumes.tdb
