-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
+ canonicalisepath "$var"
+ else
+ copypath "$var" "${source#@}"
+ fi
+done