-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_profile.bash
executable file
·174 lines (163 loc) · 6.14 KB
/
bash_profile.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Bash Configuration FILE: bash_profile.bash
#
# Purpose : Bash ~/.bash_profile Configuration File - Sourced in interactive login shell.
# Created : Sunday, April 7 2024.
# Author : Pierre Rouleau <prouleau001@gmail.com>
# Time-stamp: <2024-06-27 12:16:23 EDT, updated by Pierre Rouleau>
# ----------------------------------------------------------------------------
# Description
# -----------
#
# This USRHOME/dot/bash_profile.bash file is meant to be identified by the
# ~/.bash_profile symlink. It provides:
#
# - the USRHOME-specific code that supports tracing of the shell
# configuration files, then it
# - sources the user-provided usrcfg/do-user-bash_profile.bash file
# it it exists, then it
# - sources the usrhome/dot/bashrc.bash file to mimic what most Bash
# implementation do.
#
#
# On macos
# --------
#
# On macOS with Bash as the default shell, the ~/.bash_profile file is executed
# when Terminal starts. The implementation should normally source the ~/.bashrc
# file.
#
# When a Bash sub-shell is started without any option it is started as an
# interactive, non-login shell and it only source the ~/.bashrc.
#
# - Logic stored only in the ~/.bash_profile will be processed only for login
# shells.
# - Logic stored in ~/.bashrc is only be processed in interactive shells.
#
# On Linux
# --------
#
# On the various Linux distributions I tested so far, the ~/.profile file is
# not processed.
#
#
# With USRHOME
# ------------
#
# To simplify the implementation and support various macOS versions and Linux
# distributions, USRHOME design forces putting **all** Bash control logic
# inside ~/.bashrc. More precisely into the usrhome/dot/bashrc.bash file
# pointed to by the ~/.bashrc symlink.
#
# The file sets the USRHOME__IN_LOGIN to 1 to indicate the sourcing of the
# configuration file by a login shell. It then sources the
# $USRHOME_DIR/dot/bashrc.bash file as would most implementation would do.
#
# However, before doing it, it executes the logic that allows activation of
# the USRHOME tracing.
# ----------------------------------------------------------------------------
# Code
# ----
#
# 0 - Check if shell is interactive
# ---------------------------------
case "$-" in
*i*)
SHELL_IS_INTERACTIVE=true
;;
*)
SHELL_IS_INTERACTIVE=false
;;
esac
#
# 1 - Set USRHOME_DIR and USRHOME_DIR_USRCFG
# ------------------------------------------
#
# Identify the path of the usrcfg directory by taking advantage that usrhome
# and usrcfg are located inside the same parent directory, and that this
# script is executed via a symbolic link.
#
# If it has already been done, as indicated by the existence of the
# USRHOME_DIR environment variable, then skip this part.
#
if [[ -z $USRHOME_DIR ]]; then
# - Identify the name of what bash executed (that should be the ~/.bashrc symlink)
script=${BASH_SOURCE[0]}
# - Then identify the real file pointed by that symlink: it should be
# this file,the usrhome/dot/bashrc.bash, with the complete path.
original_script="$(readlink "$script")"
# Then identify the parent directory of the file, that's the parent
# of the usrcfg directory too.
usrhome_parent="$(dirname "$(dirname "$(dirname "$original_script")")")"
export USRHOME_DIR="$usrhome_parent/usrhome"
export USRHOME_DIR_USRCFG="$usrhome_parent/usrcfg"
fi
# 2 - Determine User Shell Configuration
# --------------------------------------
#
usrhome_config="$USRHOME_DIR_USRCFG/setfor-all-config.sh"
if [ -e "$usrhome_config" ]; then
# shellcheck disable=SC1090
. "$usrhome_config"
else
if [ "$SHELL_IS_INTERACTIVE" = "true" ]; then
printf "***USRHOME ERROR!!*********************************************\n"
printf "Cannot find the user's configuration file!\n"
printf " Expected file: %s\n" "$usrhome_config"
printf " Please write it, use the template example as basis.\n"
printf " The template is: %s\n" "$USRHOME_DIR/template/usrcfg/setfor-all-config.sh"
printf "***************************************************************\n"
fi
fi
unset usrhome_config
# 3 - Trace Shell Configuration if required
# -----------------------------------------
#
# This script needs to source user configuration scripts to
# figure out whether tracing is allowed, it sourced other files
# that reported nested tracing. The level must be reset to 1.
if [ -n "$USRHOME_TRACE_LEVEL" ]; then
unset USRHOME_TRACE_LEVEL
fi
# shellcheck disable=SC2088
usrhome_trace_in "~/.bash_profile --> \$USRHOME_DIR/dot/bash_profile.bash"
# ----------------------------------------------------------------------------
# Mimic behaviour found on most Bash implementations:
#
# - Execute the usercfg/do-usr-bash_profile.bash if it exists, which
# corresponds to executing user's logic stored in ~/.bash_profile.
#
# - The ~/.bash_profile sources ~/.bashrc if it exists.
# - In the case of USRHOME implementation ~/.bashrc
# does exists: it is implemented as usrhome/dot/bashrc.bash
#
# Set USRHOME__IN_LOGIN to 1 when this is a Bash login shell.
# - On Linux, when this file is executed, it's always a login bash shell.
# - On macOS, for Bash, it is always executed, even for interactive shells.
# Therefore, to be able to distinguish between a login and a non-login
# shell on macOS, we don't set USRHOME__IN_LOGIN to 1 here.
# Instead configure your terminal application to use one of
# the following bash launchers:
#
# - usrhome/ibin/macos_bin_bash.sh
# - usrhome/ibin/macos_homebrew_gnu_bash.bash
if [ "$(uname)" != "Darwin" ]; then
# shellcheck disable=SC2034
USRHOME__IN_LOGIN=1
fi
if [ -f "$USRHOME_DIR_USRCFG/do-user-bash_profile.bash" ]; then
# shellcheck disable=SC1091
. "$USRHOME_DIR_USRCFG/do-user-bash_profile.bash"
fi
# Inform other files the shell configuration is done during the setup of a login shell.
# shellcheck disable=SC1091
. "$USRHOME_DIR/dot/bashrc.bash"
if [ "$(uname)" != "Darwin" ]; then
unset USRHOME__IN_LOGIN
fi
# ----------------------------------------------------------------------------
# Cleanup
usrhome_trace_out
# ----------------------------------------------------------------------------
# Local Variables:
# sh-shell: bash
# End: