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