#! /bin/bash

HEADPHONE="88:D0:39:7D:6A:70"
SPEAKERS="EC:81:93:94:92:73"

if [ -z "$1" ]; then
  echo "Missing profil"
  echo "example: btswitch.sh <headphone|speakers>"
  exit 1
fi

case ${1} in
  headphone)
    bluetoothctl disconnect ${SPEAKERS}
    bluetoothctl connect ${HEADPHONE}
    # INFO: Dirty
    sleep 5
    ;;
  speakers)
    bluetoothctl disconnect ${HEADPHONE}
    bluetoothctl connect ${SPEAKERS}
    ;;
  *)
    echo "Unknown mode"
    exit 1
esac


OUTPUTID=`pactl list short sinks|grep bluez|cut -f 1`

echo "Set Bluetooth as default output"
pacmd set-default-sink ${OUTPUTID}

echo "Move streams to default output"
pactl list short sink-inputs|while read stream; do
  streamId=$(echo $stream|cut '-d ' -f1)
  echo "moving stream $streamId"
  pactl move-sink-input "$streamId" "${OUTPUTID}"
done
