From b8a81e934ba6a8c16f158e4a9ed017323d31bcb6 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 22 Jun 2022 12:02:43 +0200 Subject: Update wrapper for even better usage --- .local/etc/wrapper.d/mpv.rc | 4 --- .local/share/wrapper.sh | 86 ++++++++++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 29 deletions(-) diff --git a/.local/etc/wrapper.d/mpv.rc b/.local/etc/wrapper.d/mpv.rc index e1241f0..982eb6b 100644 --- a/.local/etc/wrapper.d/mpv.rc +++ b/.local/etc/wrapper.d/mpv.rc @@ -1,9 +1,5 @@ #!/bin/sh -WRAPPER_BINS=" - mpv -" - wrap_before() { if mpc status | awk 'NR==2 { print }' | grep -iq playing then diff --git a/.local/share/wrapper.sh b/.local/share/wrapper.sh index 22a9d4d..40e3517 100755 --- a/.local/share/wrapper.sh +++ b/.local/share/wrapper.sh @@ -12,60 +12,96 @@ main() { - wrapped="${0##*/}" - etcfile="$HOME/.local/etc/wrapper.d/$wrapped.rc" + WRAPPER_BINS_NORMALIZED="$(mktemp)" + WRAPPER_ETCFILE="$HOME/.local/etc/wrapper.d/${0##*/}.rc" - if [ ! -f "$etcfile" ] + if [ ! -f "$WRAPPER_ETCFILE" ] then printf "No configuration for %s found at %s\n" \ - "$wrapped" \ - "$etcfile" \ + "${0##*/}" \ + "$WRAPPER_ETCFILE" \ >&2 exit 3 fi - . "$etcfile" + # shellcheck disable=SC1090 + . "$WRAPPER_ETCFILE" - if [ -z "$WRAPPER_BINS" ] - then - printf "No WRAPPER_BINS specified in %s\n" "$etcfile" >&2 - exit 4 - fi + # Turn all WRAPPER_BINS entries into full paths + for bin in ${WRAPPER_BINS:-${0##*/}} + do + case "$bin" in + ./*|/*) + printf "%s\n" "$bin" + ;; + *) + printf "%s" "$PATH" | sed 's/:/\n/g' | while read -r path + do + full_path="$(printf "%s/%s\n" "$path" "$bin")" - for bin in $WRAPPER_BINS + # Filter out any paths that refer to + # the wrapper script itself. This + # should ensure it won't call itself + # into perpetuity. + if [ "$full_path" = "$0" ] + then + continue + fi + + printf "%s\n" "$full_path" + done + esac + done > "$WRAPPER_BINS_NORMALIZED" + + # Clear some stray variables + unset bin + unset full_path + unset path + + # Loop over all possible wrapped options, and run the first one that is + # executable + while read -r WRAPPER_BIN do - # Check if this entry is sufficient - wrapper_check_executable "$bin" || continue + [ -x "$WRAPPER_BIN" ] || continue # Add a firejail wrapper if desired if [ -n "$FIREJAIL_PROFILE" ] then - bin="firejail --profile=""$FIREJAIL_PROFILE"" -- $bin" + WRAPPER_BIN="firejail --profile=""$FIREJAIL_PROFILE"" -- $WRAPPER_BIN" + fi + + # Optionally add WRAPPER_OPTS, so theres no double space in + # case it is empty. + if [ -n "$WRAPPER_OPTS" ] + then + WRAPPER_BIN="$WRAPPER_BIN $WRAPPER_OPTS" fi # Run all the things we want to run wrap_before - printf "> %s\n" "$bin $WRAPPER_OPTS $@" >&2 - $bin $WRAPPER_OPTS "$@" - exit=$? + wrap_run "$WRAPPER_BIN" "$@" + WRAPPER_EXITCODE=$? wrap_after - return $exit - done + return $WRAPPER_EXITCODE + done < "$WRAPPER_BINS_NORMALIZED" - printf "No underlying executable found for %s:\n" "$wrapped" >&2 + printf "No underlying executable found for %s:\n" "${0##*/}" >&2 for bin in $WRAPPER_BINS do printf "\t%s\n" "$bin" >&2 done } -wrapper_check_executable() { - command -v "$1" > /dev/null && return 0 - [ -x "$1" ] && return 0 +wrap_log() { + printf "[%s] %s\n" "$(date -u +%FT%TZ)" "$*" >&2 +} - return 1 +wrap_run() { + wrap_log "> $*" + # shellcheck disable=SC2068 + $@ } wrap_before() { :; } -- cgit v1.1