#!/bin/bash the_guts() { # Add generic prefixes add_prefix "$HOME" add_prefix "$HOME/.local.$(uname -m)" add_prefix "$HOME/.local" add_prefix "$HOME/.prefix.$(uname -m)" add_prefix "$HOME/.prefix" # Add rubygem prefixes local dir for dir in "$HOME"/.gem/*; do # Only add it if we have that type of ruby if type "${dir##*/}" &>/dev/null; then add_prefix "$dir"/* fi done } in_array() { local needle=$1; shift local haystack=("$@") local straw for straw in "${haystack[@]}"; do [[ "$needle" == "$straw" ]] && return 0 done return 1 } add_prefix() { local prefix=$1 local dir # PATH dir="$prefix/bin" if [[ -d "$dir" ]] && ! in_array "$dir" "${paths[@]}"; then paths=("$dir" "${paths[@]}") fi # RUBYLIB dir="$prefix/lib" if [[ -d "$dir" ]] && ! in_array "$dir" "${rubylibs[@]}"; then rubylibs=("$dir" "${rubylibs[@]}") fi } main() { # Import existing values declare -ga paths rubylibs IFS=: paths=($PATH) IFS=: rubylibs=($RUBYLIB) the_guts # Put our values into the env variables IFS=: PATH="${paths[*]}" IFS=: RUBYLIB="${rubylibs[*]}" # Finally, print the values # The sed bit here is the only time we call an external program { declare -p PATH declare -p RUBYLIB } | sed 's/^declare \(-\S* \)*//' } main