Make script POSIX compliant

This commit is contained in:
dabruh 2024-04-12 18:31:25 +02:00
parent 1611525077
commit 8732f97c2b
1 changed files with 76 additions and 63 deletions

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
# Default programs: # Default programs:
export BROWSER=firefox export BROWSER=firefox
@ -30,90 +30,103 @@ export NVM_DIR="$XDG_CONFIG_HOME/nvm"
export NUGET_PACKAGES="$XDG_DATA_HOME/nuget" export NUGET_PACKAGES="$XDG_DATA_HOME/nuget"
# Color man pages: # Color man pages:
export LESS=-r export LESS='-r'
export LESS_TERMCAP_mb=$'\E[01;32m' export LESS_TERMCAP_mb
export LESS_TERMCAP_md=$'\E[01;32m' export LESS_TERMCAP_md
export LESS_TERMCAP_me=$'\E[0m' export LESS_TERMCAP_me
export LESS_TERMCAP_se=$'\E[0m' export LESS_TERMCAP_se
export LESS_TERMCAP_so=$'\E[01;47;34m' export LESS_TERMCAP_so
export LESS_TERMCAP_ue=$'\E[0m' export LESS_TERMCAP_ue
export LESS_TERMCAP_us=$'\E[01;36m' export LESS_TERMCAP_us
LESS_TERMCAP_mb="$(printf '\033[01;32m')"
LESS_TERMCAP_md="$(printf '\033[01;32m')"
LESS_TERMCAP_me="$(printf '\033[0m')"
LESS_TERMCAP_se="$(printf '\033[0m')"
LESS_TERMCAP_so="$(printf '\033[01;47;34m')"
LESS_TERMCAP_ue="$(printf '\033[0m')"
LESS_TERMCAP_us="$(printf '\033[01;36m')"
# Other program settings: # Other program settings:
export DISPLAY_DPI=96 export DISPLAY_DPI=96
# Remove an entry from PATH _tr="$(which tr 2>/dev/null || echo /usr/bin/tr)"
rm_path_entry() { _sed="$(which sed 2>/dev/null || echo /usr/bin/sed)"
local shell="${SHELL##*/}" rm_entry="${1:?Entry not set}" new_path path_arr
if [[ "$shell" == "zsh" ]]; then # Trims the specified leading and trailing characters from the string
IFS=" " read -A path_arr <<<"${PATH//:/ }" trim() {
else string="${1:?Missing string}"
IFS=" " read -a path_arr <<<"${PATH//:/ }" char="${2:?Missing trim character}"
echo "$string" | $_sed "s/^$char*//; s/$char*$//"
}
rm_env_path_entry() {
src_entries="${1:?Missing source entries}"
del_entry="${2:?Missing entry to delete}"
new_entries=""
echo "$src_entries" | $_tr ':' '\n' | while read -r entry; do
if [ "$entry" != "$del_entry" ]; then
new_entries="$new_entries:$entry"
fi fi
for path_entry in "${path_arr[@]}"; do
grep -Eq "^$rm_entry$" <<<"$path_entry" && continue
new_path="$new_path:$path_entry"
done done
PATH="${new_path#:}" trim "$new_entries" ":"
} }
# Remove one or more entries from PATH modify_env_path() {
rm_path_entries() { mode="${1:?Missing mode}"
local rm_entry entries="${2:?Missing entries}"
change="${3:?Missing change}"
for rm_entry in "$@"; do entries="$(trim "$entries" ":")"
rm_path_entry "$rm_entry"
done
}
# Prepend one or more entries to PATH. case "$mode" in
# The order of the supplied args will be kept, e.g. PATH="$1:$2:$PATH" prepend)
prepend_path_with_entries() { temp_entries="" # Will hold the new entries in the correct order
local addl_paths entry echo "$change" | $_tr ':' '\n' | while read -r entry; do
[ -z "$entry" ] && continue # Skip empty entries
for entry in "$@"; do [ -d "$entry" ] || continue # Skip non-existent directories
! [ -d "$entry" ] && continue temp_entries="$temp_entries:$entry"
addl_paths="$addl_paths:$entry"
done done
PATH="${addl_paths#:}:$PATH" # Merge the added entries to the existing entries
} # Trim the leading colon and append the new entries
entries="${temp_entries#:}:$entries"
# Append one or more entries to PATH. ;;
# The order of the supplied args will be kept, e.g. PATH="$PATH:$1:$2" append)
append_path_with_entries() { echo "$change" | $_tr ':' '\n' | while read -r entry; do
local addl_paths entry [ -z "$entry" ] && continue # Skip empty entries
[ -d "$entry" ] || continue # Skip non-existent directories
for entry in "$@"; do entries="$entries:$entry"
! [ -d "$entry" ] && continue
addl_paths="$addl_paths:$entry"
done done
;;
delete)
echo "$change" | $_tr ':' '\n' | while read -r entry; do
[ -z "$entry" ] && continue # Skip empty entries
entries=$(rm_env_path_entry "$entries" "$entry")
done
;;
*)
echo "Invalid mode: $mode (choose 'append', 'prepend', 'delete')" >&2
return 1
;;
esac
PATH="$PATH:${addl_paths#:}" trim "$entries" ":"
} }
prepend_paths=( prepend_paths="$HOME/.local/bin:$HOME/.cargo/bin:$FLUTTER_HOME/bin:$GOPATH/bin:$KREW_ROOT/bin"
"$HOME/.local/bin"
"$HOME/.cargo/bin"
"$FLUTTER_HOME/bin"
"$GOPATH/bin"
"$KREW_ROOT/bin"
)
append_paths=()
HOMEBREW_PREFIX="$XDG_DATA_HOME/homebrew" HOMEBREW_PREFIX="$XDG_DATA_HOME/homebrew"
if [ -x "$HOMEBREW_PREFIX/bin/brew" ]; then if [ -x "$HOMEBREW_PREFIX/bin/brew" ]; then
eval "$("$HOMEBREW_PREFIX/bin/brew" shellenv)" eval "$("$HOMEBREW_PREFIX/bin/brew" shellenv)"
prepend_paths+=("$HOMEBREW_PREFIX/bin" "$HOMEBREW_PREFIX/sbin") prepend_paths="$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin:$prepend_paths"
fi fi
rm_path_entries "${prepend_paths[@]}" "${append_paths[@]}" # Ensure we clean up first PATH="$(modify_env_path delete "$PATH" "$prepend_paths")"
prepend_path_with_entries "${prepend_paths[@]}" PATH="$(modify_env_path prepend "$PATH" "$prepend_paths")"
append_path_with_entries "${append_paths[@]}"
# Add the profile-extras file yourself if you wish to override anything: # Add the profile-extras file yourself if you wish to override anything:
PROFILE_EXTRAS="$XDG_CONFIG_HOME/shell/profile-extras" PROFILE_EXTRAS="$XDG_CONFIG_HOME/shell/profile-extras"
[ -f "$PROFILE_EXTRAS" ] && . "$PROFILE_EXTRAS" [ -f "$PROFILE_EXTRAS" ] && . "$PROFILE_EXTRAS" || true