Created
January 31, 2025 20:44
-
-
Save b-/b03d4fe83b8f00f31b3ce58bbf086ba8 to your computer and use it in GitHub Desktop.
wrapper script generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create wrapper script | |
# | |
# This script will create a wrapper script, replacing the passed argument (a link) with a wrapper script that applies some flags. | |
USAGE=" | |
Usage: ${0} <link> [flags...] | |
Examples: | |
- ${0} /usr/bin/firefox --private-window | |
- ${0} /usr/bin/firefox --private-window --new-tab https://example.com | |
- ${0} /usr/bin/google-chrome --enable-features=UseOzonePlatform,TouchpadOverscrollHistoryNavigation --ozone-platform=wayland | |
" | |
LINK="${1:?"${USAGE}"}" | |
set -euo pipefail | |
# load the script template | |
# shellcheck disable=SC2016 | |
SCRIPT='#!/bin/bash | |
# __SCRIPT__ | |
exec __TARGET__ __FLAGS__ "$[@]" | |
' | |
if [ ! -L "${LINK}" ]; then | |
echo "${LINK} is not a link, skipping..." | |
return | |
fi | |
shift | |
if [ -n "${1:-}" ]; then # if additional flags are passed... | |
FLAGS_SPLAT="$(printf "%q " "${@}")" | |
else # no additional flags | |
FLAGS_SPLAT="" | |
fi | |
TARGET=$(readlink -f "${LINK}") | |
# Replace links with a script that launches the browser with the correct flags: | |
SCRIPT="${SCRIPT//__TARGET__/"${TARGET}"}" # Substitute the target in | |
SCRIPT="${SCRIPT//__SCRIPT__/"${LINK}"}" # Replace the comment in the header, so we can identify the script | |
SCRIPT="${SCRIPT//__FLAGS__/"${FLAGS_SPLAT}"}" # Substitute the flags in | |
echo "${SCRIPT}" | |
# rm -f "${LINK}" # Remove the link | |
# tee "${LINK}" <<<"$SCRIPT" # Write the script to the link | |
# chmod +x "${LINK}" # Make the script executable | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment