From: Iain Patterson Date: Fri, 18 Sep 2009 12:16:54 +0000 (+0100) Subject: Environment overhaul. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=719ead9b34472b47fc4721e304f7ce0b703f94c4;p=profile.git Environment overhaul. Store XXXdirs files in ~/.PATH and add subdirectories for SYSTEM and ARCHITECTURE under there. Remove duplicate entries from paths set in PATH.bashrc. Also read XXXdirs.pre and XXXdirs.post. .pre entries are guaranteed to come before whatever was already defined or is defined with XXXdirs. .post entries are guaranteed to be present but may not necessarily be last if they were already defined. Add paths under opt to default XXXdirs.pre files. Add /lib to libdirs.post on Solaris as the Solaris linker uses the value of LD_LIBRARY_PATH to override system defaults. --- diff --git a/.PATH/SunOS/libdirs.post b/.PATH/SunOS/libdirs.post new file mode 100644 index 0000000..0bf940b --- /dev/null +++ b/.PATH/SunOS/libdirs.post @@ -0,0 +1 @@ +/usr/lib diff --git a/.PATH/bindirs.post b/.PATH/bindirs.post new file mode 100644 index 0000000..68b9d0a --- /dev/null +++ b/.PATH/bindirs.post @@ -0,0 +1,4 @@ +/sbin +/usr/sbin +/bin +/usr/bin diff --git a/.PATH/bindirs.pre b/.PATH/bindirs.pre new file mode 100644 index 0000000..92a7b1d --- /dev/null +++ b/.PATH/bindirs.pre @@ -0,0 +1,3 @@ +$HOME/opt/$SYSTEM/$ARCHITECTURE/bin +$HOME/opt/$SYSTEM/bin +$HOME/opt/bin diff --git a/.PATH/incdirs.pre b/.PATH/incdirs.pre new file mode 100644 index 0000000..d4d8669 --- /dev/null +++ b/.PATH/incdirs.pre @@ -0,0 +1 @@ +$HOME/opt/include diff --git a/.PATH/libdirs.pre b/.PATH/libdirs.pre new file mode 100644 index 0000000..bebf96a --- /dev/null +++ b/.PATH/libdirs.pre @@ -0,0 +1,2 @@ +$HOME/opt/$SYSTEM/$ARCHITECTURE/lib +$HOME/opt/$SYSTEM/lib diff --git a/.PATH/mandirs.pre b/.PATH/mandirs.pre new file mode 100644 index 0000000..fa1ba1b --- /dev/null +++ b/.PATH/mandirs.pre @@ -0,0 +1 @@ +$HOME/opt/man diff --git a/.PATH/pkgdirs.pre b/.PATH/pkgdirs.pre new file mode 100644 index 0000000..303cdbe --- /dev/null +++ b/.PATH/pkgdirs.pre @@ -0,0 +1,3 @@ +$HOME/opt/$SYSTEM/$ARCHITECTURE/lib/pkgconfig +$HOME/opt/$SYSTEM/lib/pkgconfig +$HOME/opt/lib/pkgconfig diff --git a/.profile.d/PATH.bashrc b/.profile.d/PATH.bashrc index 3113f75..0528314 100644 --- a/.profile.d/PATH.bashrc +++ b/.profile.d/PATH.bashrc @@ -1,64 +1,118 @@ # Path information is stored on separate lines in XXXdirs. -# We extract each directory exists and add it to the appropriate PATH. +# We check each directory exists and add it to the appropriate PATH. +# Prepend paths with XXXdirs.pre. They are guaranteed to precede other paths. +# Append paths with XXXdirs.post. They are not guaranteed to follow others. +# Set per-OS files in $SYSTEM[/$ARCHITECTURE] subdirectories of $DIR. # # Location of the XXXdirs files. -DIR="$HOME/.profile.d" +DIR="$HOME/.PATH" + +# Paths to set and the file to get them from @variable to copy from. +PATHS=" +PATH:bindirs +C_INCLUDE_PATH:incdirs +CPLUS_INCLUDE_PATH:@C_INCLUDE_PATH +LD_LIBRARY_PATH:libdirs +LD_RUN_PATH:@PATH +MANPATH:mandirs +PKG_CONFIG_PATH:pkgdirs +" + +function sanitisepath() { + local paths="${@//:/ }" + local sane + + for path in $paths; do + paths="${paths## }" + paths="${paths%% }" + paths="${paths#$path} " + if [ "${sane/:$path:/}" = "$sane" ]; then + sane="$sane:$path:" + fi + done + + sane="${sane//::/:}" + sane="${sane#:}" + sane="${sane%:}" + + echo "$sane" +} # Set one path to be the same as another. function copypath() { - newpath="$1"; shift - oldpath="$1"; shift + local newpath="$1"; shift + local oldpath="$1"; shift # Sanitise and export. - path="$(eval echo \$$oldpath)" + local path="$(eval echo \$$oldpath)" [ -z "$path" ] || eval "export $newpath='$path'" - - unset path newpath oldpath } # Set a path from directories. function makepath() { - newpath="$1"; shift - dirs="$1"; shift - - # Check the file exists. - [ -e "$DIR/$dirs" ] || return + local newpath="$1"; shift + local dirs="$1"; shift # Set IFS to newline only so that we can read $(embedded shell commands). - JGD=$IFS + local JGD=$IFS IFS=' ' # Read them. - path= + local path= while read dir; do - dir=$(eval echo "$dir") + local dir=$(eval echo "$dir") [ -d "$dir" ] || continue path="$path:$dir" - done < "$DIR/$dirs" - unset dir + done < "$dirs" # Restore IFS. IFS=$JGD - unset JGD + + # Are we setting, prepending or appending? + local existing="$(eval echo \$$newpath)" + dirs="${dirs##*/}" + local where="${dirs##*.}" + case "$where" in + "$dirs") ;; + pre) path="$path:$existing";; + post) path="$existing:$path";; + *) return;; + esac # Sanitise path. - path=${path#:} + path=$(sanitisepath "$path") [ -z "$path" ] && return # Export. eval "export $newpath='$path'" - - unset path newpath dirs } -makepath PATH bindirs -makepath C_INCLUDE_PATH incdirs -copypath CPLUS_INCLUDE_PATH C_INCLUDE_PATH -makepath LD_LIBRARY_PATH libdirs -copypath LD_RUN_PATH PATH -makepath MANPATH mandirs -makepath PKG_CONFIG_PATH pkgdirs +# Construct directory list, omitting nonexistent and undefined ones. +dirs= +for dir in ${SYSTEM:-#}/${ARCHITECTURE:-#} ${SYSTEM:-} ""; do + [ "${dir/#/}" = "$dir" ] || continue + [ -d "$DIR/$dir" ] || continue + dirs="$dirs,$DIR/$dir" + dirs="${dirs%%/}" +done +dirs="${dirs##,}" +[ "${dirs/,/}" = "$dirs" ] || dirs="{$dirs}" + +for path in $PATHS; do + var="${path%:*}" + source="${path#*:}" + + if [ "${source#@}" = "$source" ]; then + for dir in $(eval echo $dirs/$source{,.pre,.post}); do + [ -e "$dir" ] || continue + makepath "$var" "$dir" + done + else + copypath "$var" "${source#@}" + fi +done + -unset dirs copypath makepath newpath +unset DIR PATHS dir dirs path var source sanitisepath copypath makepath newpath setpaths