1 # Path information is stored on separate lines in XXXdirs.
2 # We check each directory exists and add it to the appropriate PATH.
3 # Prepend paths with XXXdirs.pre. They are guaranteed to precede other paths.
4 # Append paths with XXXdirs.post. They are not guaranteed to follow others.
5 # Set per-OS files in $SYSTEM[/$ARCHITECTURE] subdirectories of $DIR.
8 # Location of the XXXdirs files.
9 DIR=${PROFILE_HOME:-~}/.PATH
11 # Paths to set and the file to get them from @variable to copy from.
14 C_INCLUDE_PATH:incdirs
15 CPLUS_INCLUDE_PATH:@C_INCLUDE_PATH
16 LD_LIBRARY_PATH:libdirs
18 PKG_CONFIG_PATH:pkgdirs
21 function sanitisepath() {
22 local paths="${@//:/ }"
25 for path in $paths; do
28 paths="${paths#$path} "
29 if [ "${sane/:$path:/}" = "$sane" ]; then
41 # Set one path to be the same as another.
43 local newpath="$1"; shift
44 local oldpath="$1"; shift
46 # Sanitise and export.
47 local path="$(eval echo \$$oldpath)"
48 [ -z "$path" ] || eval "export $newpath='$path'"
51 # Try to expand variables in a path definition, discarding it if expansion fails.
52 function expandpath() {
53 local path="$1"; shift
55 while [ -n "$path" ]; do
56 [ "${path/\$/}" = "$path" ] && return 0
59 local var="${path%%/*}"
60 eval "[ -n \"\$$var\" ] || return 1"
68 local path="$1"; shift
71 if ! expandpath "$dir"; then
74 dir=$(eval echo "$dir")
75 if [ -d "$dir" ]; then
83 # Set a path from directories.
85 local newpath="$1"; shift
86 local dirs="$1"; shift
88 # Set IFS to newline only so that we can read $(embedded shell commands).
95 path=$(addpath "$path" "$dir")
96 if [ -n "$PROFILE_HOME" -a ! "${dir#\$HOME}" = "$dir" ]; then
97 dir="$PROFILE_HOME${dir#\$HOME}"
98 path=$(addpath "$path" "$dir")
105 # Are we setting, prepending or appending?
106 local existing="$(eval echo \$$newpath)"
108 local where="${dirs##*.}"
111 pre) path="$path:$existing";;
112 post) path="$existing:$path";;
117 path=$(sanitisepath "$path")
118 [ -z "$path" ] && return
121 eval "export $newpath='$path'"
124 # Remove entries which are symlinks to other existing entries.
125 function canonicalisepath() {
126 local path="$1"; shift
133 local dirs=$(eval echo "\$$path")
134 local check=":$dirs:"
135 for dir in ${dirs//:/
137 if [ -L "$dir" ]; then
138 # Is this a symlink to another entry?
139 local canon=$(readlink -f "$dir" 2>/dev/null)
140 if [ -n "$canon" ]; then
141 [ "${check/:$canon:/}" = "$check" ] || continue
145 newpath="$newpath:$dir"
150 eval "export $path='${newpath##:}'"
153 # Construct directory list, omitting nonexistent and undefined ones.
155 for dir in "${SYSTEM:-@}/${ARCHITECTURE:-@}" "${SYSTEM:-@}" ""; do
156 [ "${dir/@/}" = "$dir" ] || continue
157 [ -d "$DIR/$dir" ] || continue
158 dirs="$dirs,$DIR/$dir"
162 [ "${dirs/,/}" = "$dirs" ] || dirs="{$dirs}"
164 for path in $PATHS; do
168 if [ "${source#@}" = "$source" ]; then
169 for dir in $(eval echo $dirs/$source{,.pre,.post}); do
170 [ -e "$dir" ] || continue
171 makepath "$var" "$dir"
173 canonicalisepath "$var"
175 copypath "$var" "${source#@}"
179 unset DIR PATHS dir dirs path var source expandpath sanitisepath copypath makepath newpath addpath canonicalisepath