Fixed regression in previous commit.
[profile.git] / .bash_profile
1 if [ -t 0 -o "${0:0:1}" = "-" -o "$1" = "force" ]; then
2   # Set up PROFILE_HOME if called outside HOME.
3   if [ -z "$PROFILE_HOME" ]; then
4     # BASH_SOURCE isn't available prior to bash 3.
5     profile_home=${BASH_SOURCE%/*}
6     [ -n "$profile_home" -a -d "$profile_home" -a ! "${profile_home:0:1}" = "/" ] && profile_home=$(readlink -f "$profile_home" 2>/dev/null)
7     [ "$profile_home" = "$BASH_SOURCE" ] && profile_home=$PWD
8     [ "$profile_home" = "$HOME" ] || PROFILE_HOME=$profile_home
9     unset profile_home
10   fi
11
12   # Remember if nocaseglob was on.
13   shopt -q nocaseglob
14   nocg=$?
15   # Turn it off so we source stuff in the right order.
16   shopt -u nocaseglob
17   # Remember if nullglob was on.
18   shopt -q nullglob
19   nullglob=$?
20   # Turn it on so we can look in subdirectories.
21   shopt -s nullglob
22
23   # Remember LC_ALL.
24   lc_all=$LC_ALL
25   # Turn it off to set case-sensitive matching
26   LC_ALL=C
27
28   # Force these to come before everything else.
29   required="OS.bashrc
30 PATH.bashrc
31 BECOME.bashrc"
32
33   # Remember PATH so we can mangle it to find tsort and sed.
34   path="$PATH"
35   PATH=/usr/bin:/bin:/usr/ccs/bin
36
37   # Sort all scripts by dependencies.
38   PROFILE_D=${PROFILE_HOME:-~}/.profile.d
39   deps=
40   unsorted=
41   for i in $PROFILE_D/{*/,}*.bashrc; do
42     dep=$(sed -n 's/^##*[       ]*profile-required:[    ]*//p' "$i")
43     i="${i##$PROFILE_D/}"
44     if [ -n "$dep" ]; then
45       for d in $dep; do
46         deps="$deps
47 $d $i"
48       done
49     else
50       unsorted="$unsorted
51 $i"
52     fi
53   done
54
55   deps="$deps$unsorted"
56
57   # Avoid odd number of tokens for tsort.
58   n=$(echo "$deps" | wc -w)
59   if [ $((n%2)) = 1 ]; then
60     deps="$deps
61 ${deps##*
62 }"
63   fi
64   sorted=$(echo "$deps" | tsort)
65   if [ -n "$sorted" ]; then
66     deps="$sorted"
67     unset sorted
68   fi
69   deps="$required
70 $deps"
71
72   # Restore PATH.
73   PATH="$path"
74
75   # Source them all in the right order.
76   sourced=
77   for i in $deps; do
78     for j in $PROFILE_D/{,*/}$i; do
79       [ -e "$j" ] || continue
80       k="${j##$PROFILE_D/}"
81       [ "${sourced/ $k /}" = "$sourced" ] || continue
82       . "$j"
83       sourced="$sourced $k "
84       break
85     done
86   done
87
88   # Maybe turn nocaseglob back on.
89   [ $nocg = 0 ] && shopt -s nocaseglob
90   # Maybe turn nullglob back off.
91   [ $nullglob = 0 ] || shopt -u nullglob
92
93   # Reset LC_ALL.
94   LC_ALL=$lc_all
95
96   unset i j k d n dep deps path required unsorted sourced
97   unset lc_all nullglob nocg
98
99   # Don't inherit failure from the last script.
100   true
101 fi