X-Git-Url: http://git.iain.cx/?p=profile.git;a=blobdiff_plain;f=.bash_profile;h=5170519b0f37526c3fbdc585ca17afdcde92e096;hp=e061e9c95a2110a0095b66597ca5ae113d3b70ff;hb=2b1bf28a9ce5697d91e668bcb6569ef3202f1b90;hpb=8c8c1365e53b2d7db0d0d5f8539bf260d040d46f diff --git a/.bash_profile b/.bash_profile index e061e9c..5170519 100644 --- a/.bash_profile +++ b/.bash_profile @@ -1,15 +1,102 @@ -# $Id$ -if $(tty -s) || [ "${0:0:1}" = "-" ]; then +if [ -t 0 -o "${0:0:1}" = "-" -o "$1" = "force" ]; then + # Set up PROFILE_HOME if called outside HOME. + if [ -z "$PROFILE_HOME" ]; then + # BASH_SOURCE isn't available prior to bash 3. + profile_home=${BASH_SOURCE%/*} + [ -n "$profile_home" -a -d "$profile_home" -a ! "${profile_home:0:1}" = "/" ] && profile_home=$(readlink -f "$profile_home" 2>/dev/null) + [ "$profile_home" = "$BASH_SOURCE" ] && profile_home=$PWD + [ "$profile_home" = "$HOME" ] || PROFILE_HOME=$profile_home + PROFILE_RC="$profile_home/.bash_profile" + unset profile_home + fi + # Remember if nocaseglob was on. shopt -q nocaseglob nocg=$? # Turn it off so we source stuff in the right order. shopt -u nocaseglob - # And set case-sensitive matching - LC_CTYPE=en_GB; LANG= - for i in ~/.profile.d/*.bashrc; do . $i; done; unset i - # Maybe turn it back on. + # Remember if nullglob was on. + shopt -q nullglob + nullglob=$? + # Turn it on so we can look in subdirectories. + shopt -s nullglob + + # Remember LC_ALL. + lc_all=$LC_ALL + # Turn it off to set case-sensitive matching + LC_ALL=C + + # Force these to come before everything else. + required="OS.bashrc +PATH.bashrc +BECOME.bashrc" + + # Remember PATH so we can mangle it to find tsort and sed. + path="$PATH" + PATH=/usr/bin:/bin:/usr/ccs/bin + + # Sort all scripts by dependencies. + PROFILE_D=${PROFILE_HOME:-~}/.profile.d + deps= + unsorted= + for i in $PROFILE_D/{*/,}*.bashrc; do + dep=$(sed -n 's/^##*[ ]*profile-required:[ ]*//p' "$i") + i="${i##$PROFILE_D/}" + if [ -n "$dep" ]; then + for d in $dep; do + deps="$deps +$d $i" + done + else + unsorted="$unsorted +$i" + fi + done + + deps="$deps$unsorted" + + # Avoid odd number of tokens for tsort. + n=$(echo "$deps" | wc -w) + if [ $((n%2)) = 1 ]; then + deps="$deps +${deps##* +}" + fi + sorted=$(echo "$deps" | tsort) + if [ -n "$sorted" ]; then + deps="$sorted" + unset sorted + fi + deps="$required +$deps" + + # Restore PATH. + PATH="$path" + + # Source them all in the right order. + sourced= + for i in $deps; do + for j in $PROFILE_D/{,*/}$i; do + [ -e "$j" ] || continue + k="${j##$PROFILE_D/}" + [ "${sourced/ $k /}" = "$sourced" ] || continue + . "$j" + sourced="$sourced $k " + break + done + done + + # Maybe turn nocaseglob back on. [ $nocg = 0 ] && shopt -s nocaseglob - unset nocg - __ps1 + # Maybe turn nullglob back off. + [ $nullglob = 0 ] || shopt -u nullglob + + # Reset LC_ALL. + LC_ALL=$lc_all + + unset i j k d n dep deps path required unsorted sourced + unset lc_all nullglob nocg + + # Don't inherit failure from the last script. + true fi