X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=.profile.d%2FPATH.bashrc;h=a67a5ba508dd3d2e9f2300c190b2ae4a7e28af7a;hb=500ba2b170e6f1bfad014c9bca55e19225a9bbe8;hp=ab57aaabca30c6e20d9d61443abe86797a6fa86d;hpb=0022b1a8c3238ee7350d19b93ac9816e66405cc3;p=profile.git diff --git a/.profile.d/PATH.bashrc b/.profile.d/PATH.bashrc index ab57aaa..a67a5ba 100644 --- a/.profile.d/PATH.bashrc +++ b/.profile.d/PATH.bashrc @@ -6,7 +6,7 @@ # # Location of the XXXdirs files. -DIR="$HOME/.PATH" +DIR=${PROFILE_HOME:-~}/.PATH # Paths to set and the file to get them from @variable to copy from. PATHS=" @@ -49,6 +49,38 @@ function copypath() { [ -z "$path" ] || eval "export $newpath='$path'" } +# Try to expand variables in a path definition, discarding it if expansion fails. +function expandpath() { + local path="$1"; shift + + while [ -n "$path" ]; do + [ "${path/\$/}" = "$path" ] && return 0 + path="${path#*\$}" + + local var="${path%%/*}" + eval "[ -n \"\$$var\" ] || return 1" + done + + return 0 +} + +# Helper. +function addpath() { + local path="$1"; shift + local dir="$1"; shift + + if ! expandpath "$dir"; then + echo "$path" + else + dir=$(eval echo "$dir") + if [ -d "$dir" ]; then + echo "$path:$dir" + else + echo "$path" + fi + fi +} + # Set a path from directories. function makepath() { local newpath="$1"; shift @@ -61,10 +93,11 @@ function makepath() { # Read them. local path= while read dir; do - local dir=$(eval echo "$dir") - [ -d "$dir" ] || continue - - path="$path:$dir" + path=$(addpath "$path" "$dir") + if [ -n "$PROFILE_HOME" -a ! "${dir#\$HOME}" = "$dir" ]; then + dir="$PROFILE_HOME${dir#\$HOME}" + path=$(addpath "$path" "$dir") + fi done < "$dirs" # Restore IFS. @@ -115,4 +148,4 @@ for path in $PATHS; do done -unset DIR PATHS dir dirs path var source sanitisepath copypath makepath newpath setpaths +unset DIR PATHS dir dirs path var source expandpath sanitisepath copypath makepath newpath addpath