Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling #10

Merged
merged 2 commits into from
Nov 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions mic_over_mumble
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
# https://github.com/pzmarzly/mic_over_mumble
set -euo pipefail
# To debug, uncomment the following line:
# set -x
Expand All @@ -7,7 +8,6 @@ set -euo pipefail
# ID = PID / process ID

main () {
prepare_env
set_up
echo "Press Return to shut down..."
read -n1 -s -r
Expand Down Expand Up @@ -38,6 +38,7 @@ run_mumble_client_wizard () {
}

set_up () {
prepare_env
start_mumble_server
start_mumble_client
change_pa_config
Expand All @@ -52,12 +53,16 @@ shut_down () {
}

start_mumble_client () {
echo "Starting Mumble client..."
echo "Starting Mumble client (a window should appear in a moment)..."
mumble "mumble://localhost" >/dev/null 2>&1 &
MUMBLE_CLIENT_ID=$!
# FIXME: I don't remember why I thought this
# was necessary.
disown
# Mumble is slow to launch, and the
# user may need to click on OK button.
# FIXME: probe PulseAudio server to see if
# Mumble has connected.
sleep 15
}

Expand Down Expand Up @@ -136,28 +141,56 @@ set_default_source () {
# Data fetching & parsing.

get_mumble_client_paid () {
pacmd list-sink-inputs |
result=$(pacmd list-sink-inputs |
grep -F -e "index: " -e "media.name = " |
cut_every_second_newline |
grep -F -e "Mumble" |
print_second_column
take_second_column)

if [ -z "$result" ]; then
echo "Error: Mumble client did not connect to PulseAudio (yet?)." 2>&1
print_how_to_restart 2>&1
exit 1
fi

if [ "$(echo "$result" | wc -l)" != "1" ]; then
echo "Error: Multiple Mumble instances found." 2>&1
print_how_to_restart 2>&1
exit 1
fi

echo "$result"
}

get_sink_paid () {
pacmd list-sinks |
result=$(pacmd list-sinks |
grep -F -e "index: " -e "name: " |
cut_every_second_newline |
grep -F -e "Loopback" |
cut_active_device_indicator |
print_second_column
take_second_column)

if [ -z "$result" ]; then
echo "Error: Failed to find the device the script should have added." 2>&1
print_how_to_restart 2>&1
exit 1
fi

if [ "$(echo "$result" | wc -l)" != "1" ]; then
echo "Error: Multiple virtual devices found." 2>&1
print_how_to_restart 2>&1
exit 1
fi

echo "$result"
}

# https://serverfault.com/a/375098/449626
cut_every_second_newline () {
awk 'ORS=NR%2?" ":"\n"'
}

print_second_column () {
take_second_column () {
awk '{print $2}'
}

Expand All @@ -168,4 +201,13 @@ cut_active_device_indicator () {
cut -c 5-
}

# Errors

print_how_to_restart () {
echo "Please find the reason why this happened, try fixing it"
echo "(\`pacmd list-sink-inputs\` and \`pacmd list-sinks\` may be"
echo "useful), then kill mumble and murmurd, restart PulseAudio via"
echo "\`pulseaudio -k\`, and finally, restart the script."
}

main