From dea6a2ec1b066c1564198c1ed673408ffe50588d Mon Sep 17 00:00:00 2001 From: Tilman Kranz Date: Mon, 9 Oct 2023 03:38:13 +0200 Subject: [PATCH] improved main program structure; checks for dependencies --- pulseaudio-tcp | 126 ++++++++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 55 deletions(-) diff --git a/pulseaudio-tcp b/pulseaudio-tcp index acaa9e7..af7fb15 100644 --- a/pulseaudio-tcp +++ b/pulseaudio-tcp @@ -26,37 +26,6 @@ operation=$1 config_dir="$HOME"/.config/pulseaudio-tcp config="$config_dir"/config.inc.sh -if test "$operation" = setup ; then - echo "Entering setup mode ..." -else - test -e "$config" || { - echo "ERROR: Configfile $config does not exist (use \"$0 setup\" first)." >&2 ; - exit 1 ; - } - - test -r "$config" || { - echo "ERROR: Configfile $config is not readable." >&2 ; - exit 1 ; - } - - test -f "$config" || { - echo "ERROR: Configfile $config is not a regular file." >&2 ; - exit 1 ; - } - - . "$config" - - test -n "$remote_ip" || { - echo "ERROR: \"remote_ip=\" not set in configfile $config." >&2 ; - exit 1 ; - } - - test -n "$remote_user" || { - echo "ERROR: \"remote_user=\" not set in configfile $config." >&2 ; - exit 1 ; - } -fi - ## # Functions @@ -179,7 +148,7 @@ do_status() { # Acquire PulseAudio cookie from remote host sync_pa_cookie() { - if scp -q "$remote_user"@"$remote_ip":.config/pulse/cookie .config/pulse/cookie ; then + if scp -q "$remote_user"@"$remote_ip":.config/pulse/cookie ~/.config/pulse/cookie ; then echo "INFO: Synced PulseAudio cookie from remote_ip=$remote_ip." >&2 return 0 else @@ -302,30 +271,77 @@ do_stop() { ## # Main Program -case "$operation" in - ""|-h|--help) - cat << EOF +rv=0 + +if test "$operation" = setup ; then + echo "Entering setup mode ..." +else + if ! test -e "$config" ; then + echo "ERROR: Configfile $config does not exist (use \"$0 setup\" first)." >&2 + rv=1 + elif ! test -r "$config" ; then + echo "ERROR: Configfile $config is not readable." >&2 + rv=1 + elif ! test -f "$config" ; then + echo "ERROR: Configfile $config is not a regular file." >&2 + rv=1 + else + . "$config" + + if test -z "$remote_ip" ; then + echo "ERROR: \"remote_ip=\" not set in configfile $config." >&2 + rv=1 + elif test -z "$remote_user" ; then + echo "ERROR: \"remote_user=\" not set in configfile $config." >&2 + rv=1 + fi + fi + + if test -z "$(which jq)" ; then + echo "ERROR: Required executable \"jq\" not found." >&2 + rv=1 + elif test -z "$(which pactl)" ; then + echo "ERROR: Required executable \"pactl\" not found." >&2 + rv=1 + elif test -z "$(which ssh)" ; then + echo "ERROR: Required executable \"ssh\" not found." >&2 + rv=1 + fi +fi + +if test "$rv" -ne 0 ; then + echo "ERROR: Preliminary checks failed, skipping operation." >&2 +else + case "$operation" in + ""|-h|--help) + cat << EOF Setup and run encrypted connection to remote PulseAudio/Pipewire server Usage: $0 setup|start|stop|status EOF - exit 0 - ;; - setup) - do_setup || exit 1 - ;; - start) - do_start || exit 1 - ;; - stop) - do_stop || exit 1 - ;; - status) - do_status - exit $? - ;; - *) - echo "ERROR: Usage: $0 setup|start|stop|status" >&2 - exit 1 - ;; -esac + rv=0 + ;; + setup) + do_setup + rv=$? + ;; + start) + do_start + rv=$? + ;; + stop) + do_stop + rv=$? + ;; + status) + do_status + rv=$? + ;; + *) + echo "ERROR: Usage: $0 setup|start|stop|status" >&2 + rv=1 + ;; + esac +fi + +exit "$rv"