#!/usr/bin/env bash
# hermes-robot: start Hermes with the robot as the default speaker and microphone,
# then put your usual devices back when Hermes exits.
#
# Install:  install -Dm755 hermes-robot ~/.local/bin/hermes-robot
# Use:      hermes-robot            (any arguments are passed through to hermes)
set -uo pipefail

SINK=robot_speaker
SOURCE=robot_mic

has() { pactl list short "$1" | awk '{print $2}' | grep -qxF "$2"; }
if ! has sinks "$SINK" || ! has sources "$SOURCE"; then
    echo "hermes-robot: the robot's audio devices aren't present." >&2
    echo "  Is the robot powered and on Wi-Fi? Check with:  pactl list short sources" >&2
    exit 1
fi

prev_sink="$(pactl get-default-sink)"
prev_source="$(pactl get-default-source)"
restore() {
    pactl set-default-sink "$prev_sink" 2>/dev/null
    pactl set-default-source "$prev_source" 2>/dev/null
}
trap restore EXIT INT TERM

pactl set-default-sink "$SINK"
pactl set-default-source "$SOURCE"

hermes "$@"
