improved main program structure; checks for dependencies

This commit is contained in:
Tilman Kranz 2023-10-09 03:38:13 +02:00
parent 4e82cfe6d5
commit dea6a2ec1b

View File

@ -26,37 +26,6 @@ operation=$1
config_dir="$HOME"/.config/pulseaudio-tcp config_dir="$HOME"/.config/pulseaudio-tcp
config="$config_dir"/config.inc.sh 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=<IP address>\" not set in configfile $config." >&2 ;
exit 1 ;
}
test -n "$remote_user" || {
echo "ERROR: \"remote_user=<username>\" not set in configfile $config." >&2 ;
exit 1 ;
}
fi
## ##
# Functions # Functions
@ -179,7 +148,7 @@ do_status() {
# Acquire PulseAudio cookie from remote host # Acquire PulseAudio cookie from remote host
sync_pa_cookie() { 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 echo "INFO: Synced PulseAudio cookie from remote_ip=$remote_ip." >&2
return 0 return 0
else else
@ -302,30 +271,77 @@ do_stop() {
## ##
# Main Program # Main Program
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=<IP address>\" not set in configfile $config." >&2
rv=1
elif test -z "$remote_user" ; then
echo "ERROR: \"remote_user=<username>\" 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 case "$operation" in
""|-h|--help) ""|-h|--help)
cat << EOF cat << EOF
Setup and run encrypted connection to remote PulseAudio/Pipewire server Setup and run encrypted connection to remote PulseAudio/Pipewire server
Usage: $0 setup|start|stop|status Usage: $0 setup|start|stop|status
EOF EOF
exit 0 rv=0
;; ;;
setup) setup)
do_setup || exit 1 do_setup
rv=$?
;; ;;
start) start)
do_start || exit 1 do_start
rv=$?
;; ;;
stop) stop)
do_stop || exit 1 do_stop
rv=$?
;; ;;
status) status)
do_status do_status
exit $? rv=$?
;; ;;
*) *)
echo "ERROR: Usage: $0 setup|start|stop|status" >&2 echo "ERROR: Usage: $0 setup|start|stop|status" >&2
exit 1 rv=1
;; ;;
esac esac
fi
exit "$rv"