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