Less cluttered titlestring.
[profile.git] / .profile.d / PATH.bashrc
1 # $Id$
2 #
3 # Path information is stored on separate lines in XXXdirs.
4 # We extract each directory exists and add it to the appropriate PATH.
5 #
6
7 # Location of the XXXdirs files.
8 DIR=$HOME/.profile.d
9
10 # Set one path to be the same as another.
11 function copypath() {
12   newpath="$1"; shift
13   oldpath="$1"; shift
14
15   # Sanitise and export.
16   path="$(eval echo \$$oldpath)"
17   [ -z "$path" ] || eval export $newpath="$path"
18
19   unset path newpath oldpath
20 }
21
22 # Set a path from directories.
23 function makepath() {
24   newpath="$1"; shift
25   dirs="$1"; shift
26
27   # Check the file exists.
28   [ -e "$DIR/$dirs" ] || return
29
30   # Set IFS to newline only so that we can read $(embedded shell commands).
31   JGD=$IFS
32   IFS='
33 '
34   # Read them.
35   path=
36   while read dir; do
37     dir=$(eval echo "$dir")
38     [ -d "$dir" ] || continue
39
40     path="$path:$dir"
41   done < "$DIR/$dirs"
42   unset dir
43
44   # Restore IFS.
45   IFS=$JGD
46   unset JGD
47
48   # Sanitise path.
49   path=${path#:}
50   [ -z "$path" ] && return
51
52   # Export.
53   eval export $newpath="$path"
54
55   unset path newpath dirs
56 }
57
58 makepath PATH bindirs
59 makepath C_INCLUDE_PATH incdirs
60 copypath CPLUS_INCLUDE_PATH C_INCLUDE_PATH
61 makepath LD_LIBRARY_PATH libdirs
62 copypath LD_RUN_PATH PATH
63 makepath MANPATH mandirs
64 makepath PKG_CONFIG_PATH pkgdirs
65
66 unset dirs copypath makepath newpath