#!/bin/sh -e
# SPDX-License-Identifier: WTFPL
# shellcheck enable=
# Enable a bluetooth headset and out or in/out mode.

find_bluez () {
	pactl list short "$1" | grep bluez | grep -v monitor | awk '{print $2}'
}


card=$(find_bluez cards)

if [ "$1" = inout ]
then
	pactl set-card-profile "$card" headset_head_unit

	sink=$(find_bluez sinks)
	pactl set-default-sink "$sink"
	pactl set-sink-mute "$sink" 0

	src=$(find_bluez sources)
	pactl set-default-source "$src"
	pactl set-source-mute "$src" 0
elif [ "$1" = out ]
then
	pactl set-card-profile "$card" a2dp_sink

	sink=$(find_bluez sinks)
	pactl set-default-sink "$sink"
	pactl set-sink-mute "$sink" 0
else
	cat <<- EOF
		usage: $0 {out | inout}

		Enable a bluetooth headset and set either:
		- in/out mode: mono audio output + microphone (HSP/HFP)
		- out mode: high-quality output, no microphone (A2DP)
	EOF
	exit 64
fi
