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