diff options
Diffstat (limited to '.local/share/wrapper.sh')
-rwxr-xr-x | .local/share/wrapper.sh | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/.local/share/wrapper.sh b/.local/share/wrapper.sh new file mode 100755 index 0000000..9335039 --- /dev/null +++ b/.local/share/wrapper.sh @@ -0,0 +1,105 @@ +#!/bin/sh + +# This program is free software: you can redistribute it and/or modify it under +# the terms of the GNU Affero General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. + +main() +{ + WRAPPER_BINS_NORMALIZED="$(mktemp)" + WRAPPER_ETCFILE="$HOME/.local/etc/wrapper.d/${0##*/}.sh" + + if [ ! -f "$WRAPPER_ETCFILE" ] + then + printf "No configuration for %s found at %s\n" \ + "${0##*/}" \ + "$WRAPPER_ETCFILE" \ + >&2 + + exit 3 + fi + + # shellcheck disable=SC1090 + . "$WRAPPER_ETCFILE" + + # 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")" + + # 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_CMD + do + [ -x "$WRAPPER_CMD" ] || continue + + # Add a firejail wrapper if desired + if [ -n "$FIREJAIL_PROFILE" ] + then + WRAPPER_CMD="firejail --profile=""$FIREJAIL_PROFILE"" -- $WRAPPER_CMD" + fi + + # Optionally add WRAPPER_OPTS, so theres no double space in + # case it is empty. + if [ -n "$WRAPPER_OPTS" ] + then + WRAPPER_CMD="$WRAPPER_CMD $(printf "%s" "$WRAPPER_OPTS" | tr "\n" " " | awk '{$1=$1};1')" + fi + + # Run all the things we want to run + wrap_before + wrap_log "> $WRAPPER_CMD $*" + $WRAPPER_CMD "$@" + WRAPPER_EXITCODE=$? + wrap_after + + return $WRAPPER_EXITCODE + done < "$WRAPPER_BINS_NORMALIZED" + + printf "No underlying executable found for %s:\n" "${0##*/}" >&2 + for bin in $WRAPPER_BINS + do + printf "\t%s\n" "$bin" >&2 + done +} + +wrap_log() { + printf "[%s] %s\n" "$(date -u +%FT%TZ)" "$*" >&2 +} + +wrap_before() { :; } +wrap_after() { :; } + +main "$@" |