mirror of
https://gitlab.com/dabruh/dotfiles.git
synced 2024-12-26 03:48:12 +01:00
Make script POSIX compliant
This commit is contained in:
parent
1611525077
commit
8732f97c2b
1 changed files with 76 additions and 63 deletions
|
@ -1,4 +1,4 @@
|
|||
#!/bin/bash
|
||||
#!/bin/sh
|
||||
|
||||
# Default programs:
|
||||
export BROWSER=firefox
|
||||
|
@ -30,90 +30,103 @@ export NVM_DIR="$XDG_CONFIG_HOME/nvm"
|
|||
export NUGET_PACKAGES="$XDG_DATA_HOME/nuget"
|
||||
|
||||
# Color man pages:
|
||||
export LESS=-r
|
||||
export LESS_TERMCAP_mb=$'\E[01;32m'
|
||||
export LESS_TERMCAP_md=$'\E[01;32m'
|
||||
export LESS_TERMCAP_me=$'\E[0m'
|
||||
export LESS_TERMCAP_se=$'\E[0m'
|
||||
export LESS_TERMCAP_so=$'\E[01;47;34m'
|
||||
export LESS_TERMCAP_ue=$'\E[0m'
|
||||
export LESS_TERMCAP_us=$'\E[01;36m'
|
||||
export LESS='-r'
|
||||
export LESS_TERMCAP_mb
|
||||
export LESS_TERMCAP_md
|
||||
export LESS_TERMCAP_me
|
||||
export LESS_TERMCAP_se
|
||||
export LESS_TERMCAP_so
|
||||
export LESS_TERMCAP_ue
|
||||
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:
|
||||
export DISPLAY_DPI=96
|
||||
|
||||
# Remove an entry from PATH
|
||||
rm_path_entry() {
|
||||
local shell="${SHELL##*/}" rm_entry="${1:?Entry not set}" new_path path_arr
|
||||
_tr="$(which tr 2>/dev/null || echo /usr/bin/tr)"
|
||||
_sed="$(which sed 2>/dev/null || echo /usr/bin/sed)"
|
||||
|
||||
if [[ "$shell" == "zsh" ]]; then
|
||||
IFS=" " read -A path_arr <<<"${PATH//:/ }"
|
||||
else
|
||||
IFS=" " read -a path_arr <<<"${PATH//:/ }"
|
||||
fi
|
||||
|
||||
for path_entry in "${path_arr[@]}"; do
|
||||
grep -Eq "^$rm_entry$" <<<"$path_entry" && continue
|
||||
new_path="$new_path:$path_entry"
|
||||
done
|
||||
|
||||
PATH="${new_path#:}"
|
||||
# Trims the specified leading and trailing characters from the string
|
||||
trim() {
|
||||
string="${1:?Missing string}"
|
||||
char="${2:?Missing trim character}"
|
||||
echo "$string" | $_sed "s/^$char*//; s/$char*$//"
|
||||
}
|
||||
|
||||
# Remove one or more entries from PATH
|
||||
rm_path_entries() {
|
||||
local rm_entry
|
||||
rm_env_path_entry() {
|
||||
src_entries="${1:?Missing source entries}"
|
||||
del_entry="${2:?Missing entry to delete}"
|
||||
new_entries=""
|
||||
|
||||
for rm_entry in "$@"; do
|
||||
rm_path_entry "$rm_entry"
|
||||
done
|
||||
}
|
||||
|
||||
# Prepend one or more entries to PATH.
|
||||
# The order of the supplied args will be kept, e.g. PATH="$1:$2:$PATH"
|
||||
prepend_path_with_entries() {
|
||||
local addl_paths entry
|
||||
|
||||
for entry in "$@"; do
|
||||
! [ -d "$entry" ] && continue
|
||||
addl_paths="$addl_paths:$entry"
|
||||
echo "$src_entries" | $_tr ':' '\n' | while read -r entry; do
|
||||
if [ "$entry" != "$del_entry" ]; then
|
||||
new_entries="$new_entries:$entry"
|
||||
fi
|
||||
done
|
||||
|
||||
PATH="${addl_paths#:}:$PATH"
|
||||
trim "$new_entries" ":"
|
||||
}
|
||||
|
||||
# Append one or more entries to PATH.
|
||||
# The order of the supplied args will be kept, e.g. PATH="$PATH:$1:$2"
|
||||
append_path_with_entries() {
|
||||
local addl_paths entry
|
||||
modify_env_path() {
|
||||
mode="${1:?Missing mode}"
|
||||
entries="${2:?Missing entries}"
|
||||
change="${3:?Missing change}"
|
||||
|
||||
for entry in "$@"; do
|
||||
! [ -d "$entry" ] && continue
|
||||
addl_paths="$addl_paths:$entry"
|
||||
done
|
||||
entries="$(trim "$entries" ":")"
|
||||
|
||||
PATH="$PATH:${addl_paths#:}"
|
||||
case "$mode" in
|
||||
prepend)
|
||||
temp_entries="" # Will hold the new entries in the correct order
|
||||
echo "$change" | $_tr ':' '\n' | while read -r entry; do
|
||||
[ -z "$entry" ] && continue # Skip empty entries
|
||||
[ -d "$entry" ] || continue # Skip non-existent directories
|
||||
temp_entries="$temp_entries:$entry"
|
||||
done
|
||||
|
||||
# Merge the added entries to the existing entries
|
||||
# Trim the leading colon and append the new entries
|
||||
entries="${temp_entries#:}:$entries"
|
||||
;;
|
||||
append)
|
||||
echo "$change" | $_tr ':' '\n' | while read -r entry; do
|
||||
[ -z "$entry" ] && continue # Skip empty entries
|
||||
[ -d "$entry" ] || continue # Skip non-existent directories
|
||||
entries="$entries:$entry"
|
||||
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
|
||||
|
||||
trim "$entries" ":"
|
||||
}
|
||||
|
||||
prepend_paths=(
|
||||
"$HOME/.local/bin"
|
||||
"$HOME/.cargo/bin"
|
||||
"$FLUTTER_HOME/bin"
|
||||
"$GOPATH/bin"
|
||||
"$KREW_ROOT/bin"
|
||||
)
|
||||
append_paths=()
|
||||
prepend_paths="$HOME/.local/bin:$HOME/.cargo/bin:$FLUTTER_HOME/bin:$GOPATH/bin:$KREW_ROOT/bin"
|
||||
|
||||
HOMEBREW_PREFIX="$XDG_DATA_HOME/homebrew"
|
||||
if [ -x "$HOMEBREW_PREFIX/bin/brew" ]; then
|
||||
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
|
||||
|
||||
rm_path_entries "${prepend_paths[@]}" "${append_paths[@]}" # Ensure we clean up first
|
||||
prepend_path_with_entries "${prepend_paths[@]}"
|
||||
append_path_with_entries "${append_paths[@]}"
|
||||
PATH="$(modify_env_path delete "$PATH" "$prepend_paths")"
|
||||
PATH="$(modify_env_path prepend "$PATH" "$prepend_paths")"
|
||||
|
||||
|
||||
# Add the profile-extras file yourself if you wish to override anything:
|
||||
PROFILE_EXTRAS="$XDG_CONFIG_HOME/shell/profile-extras"
|
||||
[ -f "$PROFILE_EXTRAS" ] && . "$PROFILE_EXTRAS"
|
||||
[ -f "$PROFILE_EXTRAS" ] && . "$PROFILE_EXTRAS" || true
|
||||
|
|
Loading…
Reference in a new issue