/
dhd-shdetect
executable file
·288 lines (250 loc) · 8.46 KB
/
dhd-shdetect
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/sh
set -e
export SHDETECT_RESULTS="$HOME/.shdetect_dhd.sh"
export DHD=${DHD:-"$HOME/.dhd"}
usage() {
cat <<EOF
$0: Detect settings for DHD shell config file
Detect settings like what the \$PATH should be and what commands are available,
and store them in the results file:
$SHDETECT_RESULTS
This can be dot-sourced in .profile/.bashrc.
This allows expensive computations, e.g. what paths exist or what the ruby
command believes its Gem.dir to be, to happen just once, rather than to be
recomputed on every shell launch.
This command also applies basic configuration to e.g. git and npm.
EOF
}
debug=
while test $# -gt 0; do
case "$1" in
-h | --help ) usage; exit 0;;
-d | --debug) debug=1; set -x; shift;;
*) usage; exit 1;;
esac
done
#### Pre-calculate some versioned directories
# Python versions installed from python.org go into /Library/Frameworks/Python.framework/Versions,
# and there is a Current symlink pointing to the latest version.
# (Note that in the user's ~/Library/Python, there is no Current symlink, only specific versions.)
py3fwk=
if test -e /Library/Frameworks/Python.framework/Versions/Current; then
# py3fwk=$(find /Library/Frameworks/Python.framework/Versions -maxdepth 1 -type d -name '3.1?' | sort | tail -n 1)
py3vers=$(ls -l /Library/Frameworks/Python.framework/Versions/Current | awk '{print $NF}')
py3fwk="/Library/Frameworks/Python.framework/Versions/$py3vers"
else
py3vers=$(/usr/bin/python3 -c 'import sys;print(f"{sys.version_info.major}.{sys.version_info.minor}");')
fi
# A list of all the paths which MIGHT exist and contain binaries we
# want in our $PATH
# Order is important in $PATH; earlier entries are searched first
# Some specific notes:
# * npmglobal can be customized: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
# * Homebrew python should be preempted by a Python installation from python.org
# <https://justinmayer.com/posts/homebrew-python-is-not-for-you/>
POSSIBLE_PATHS="
$py3fwk/bin
${HOME}/opt/bin
${HOME}/opt/sbin
${HOME}/.cabal/bin
${HOME}/.cabal/sbin
${HOME}/.local/bin
${HOME}/.local/sbin
${HOME}/.poetry/bin
${DHD}/opt/bin
${DHD}/opt/bash/bin
${HOME}/.config/emacs/bin
${HOME}/opt/homebrew/bin
${HOME}/opt/homebrew/sbin
${HOME}/opt/npmglobal/bin
${HOME}/opt/npmglobal/sbin
${HOME}/test spaces in path
${HOME}/Library/Python/$py3vers/bin
${HOME}/.fly/bin
${HOME}/.krew/bin
${HOME}/env/bin
/opt/homebrew/bin
/opt/homebrew/sbin
/usr/pkg/bin
/usr/pkg/sbin
/usr/local/go/bin
/usr/local/bin
/usr/local/sbin
/usr/bin
/usr/sbin
/bin
/sbin
/usr/local/texlive/2023basic/bin/universal-darwin
/Applications/Emacs.app/Contents/MacOS
/Applications/FontForge.app/Contents/Resources/opt/local/bin
/Applications/QMK Toolbox.app/Contents/Resources
/Applications/Neovide.app/Contents/MacOS
/Applications/Inkscape.app/Contents/MacOS
/c/WINDOWS
/c/WINDOWS/system32/Wbem
/c/WINDOWS/system32
/c/MinGW/bin
/c/MinGW/sbin
/c/MinGW/msys/1.0/bin
/c/MinGW/msys/1.0/sbin
${GOROOT:-""}/bin
${GOPATH:-""}/bin
${PATH}
"
IGNORE_PATHS="
${HOME}/.nvm/*
"
export PATH=$("$DHD/opt/bin/pathsetup" "$POSSIBLE_PATHS" "$IGNORE_PATHS")
if ! test "$MANPATH"; then
if cmdavail manpath; then
export MANPATH=$(manpath)
else
export MANPATH="/usr/share/man"
fi
fi
HOMEBREWDIR=
GOROOT=
if cmdavail brew; then
# This is NOT a standard environment variable, for some reason
HOMEBREWDIR=$(dirname "$(dirname "$(command -v brew)")")
if test -d "$HOMEBREWDIR/opt/go" && test -z "$GOROOT"; then
GOROOT="$HOMEBREWDIR/opt/go/libexec"
fi
# I can't remember why I had this in here. Commenting out in case I need to remember later.
# if test -d "$HOMEBREWDIR/opt/openssl@1.1"; then
# export PATH="$HOMEBREWDIR/opt/openssl@1.1/bin:$PATH"
# export LDFLAGS="-L$HOMEBREWDIR/opt/openssl@1.1/lib"
# export CPPFLAGS="-I$HOMEBREWDIR/opt/openssl@1.1/include"
# export PKG_CONFIG_PATH="$HOMEBREWDIR/opt/openssl@1.1/lib/pkgconfig"
# fi
fi
if test -d "$HOME/.nvm"; then
export NVM_DIR="$HOME/.nvm"
# To load NVM, we must make sure that npm doesn't have any incompatible settings.
grep -v '^\s*prefix\s*=' "$HOME/.npmrc" > "$HOME/.npmrc.tmp"
mv "$HOME/.npmrc.tmp" "$HOME/.npmrc"
# Load NVM, which may add `npm` to the PATH
. "$NVM_DIR/nvm.sh"
fi
NPM_PACKAGES=
if cmdavail npm; then
NPM_PACKAGES="${HOME}/.npm-packages"
npm config set update-notifier false
# this is apparently not compatible with nvm which we use at work
# npm config set prefix "${HOME}/.npm-packages"
npm config set fund false
export PATH="$(pathsetup "${PATH}:${NPM_PACKAGES}/bin")"
export MANPATH="${MANPATH}:${NPM_PACKAGES}/share/man"
fi
# Bun
BUN_INSTALL=
if test -d "$HOME/.bun"; then
export BUN_INSTALL="$HOME/.bun"
export PATH="${PATH}:${BUN_INSTALL}/bin"
fi
RUBY_GEMS_PATHS=
if cmdavail ruby; then
RUBY_GEMS_PATHS="$(ruby -rrubygems -e 'puts "%s/bin:%s/bin" % [Gem.dir, Gem.user_dir]')"
export PATH="$(pathsetup "$PATH:$RUBY_GEMS_PATHS")"
fi
if test -e "$HOME/Library/Android/sdk"; then
export ANDROID_HOME="$HOME/Library/Android/sdk"
export PATH="$PATH:$ANDROID_HOME/emulator"
export PATH="$PATH:$ANDROID_HOME/tools"
export PATH="$PATH:$ANDROID_HOME/tools/bin"
export PATH="$PATH:$ANDROID_HOME/platform-tools"
fi
export EDITOR=vi
if cmdavail vimr; then
export EDITOR="vimr --wait --cur-env"
elif cmdavail nvim; then
export EDITOR=nvim
elif cmdavail vim; then
export EDITOR=vim
fi
DHD_LS_TYPE=
if ls --version 2>/dev/null | grep -q GNU; then
DHD_LS_TYPE=gnu
elif ls -h -G >/dev/null 2>&1; then
DHD_LS_TYPE=bsd
fi
DHD_DF_H=
if df -h / >/dev/null 2>&1; then
DHD_DF_H=1
fi
# Fucking environment variables on macOS
if cmdavail launchctl && test "$LANG"; then
launchctl setenv LC_ALL "$LANG"
fi
# Test for a grep that supports --color=auto, which includes GNU, FreeBSD, and macOS greps
DHD_GREP_SUPPORTS_COLOR_AUTO=
if echo x | grep -q --color=auto x 2>/dev/null; then
DHD_GREP_SUPPORTS_COLOR_AUTO=1
fi
# Normalize hostnames
# Some hostnames are unchangeable and bad, like my work laptop's hostname.
# These we set explicitly.
# Other hostnames are just the lower case short hostname (without fqdn).
DHD_HOSTNAME=
uname_n_lower="$(uname -n | tr '[:upper:]' '[:lower:]')"
case "$uname_n_lower" in
"virtu’s-macbook-pro*") DHD_HOSTNAME=virtumbp;;
*) DHD_HOSTNAME="${uname_n_lower%%.*}";;
esac
# Available commands
# Can test for them with
# test "${DHD_CMDAVAIL#*cmd_name}" != "$DHD_CMDAVAIL"
DHD_CMDAVAIL=
cmdavail rainbow && DHD_CMDAVAIL="$DHD_CMDAVAIL rainbow"
cmdavail lolcatjs && DHD_CMDAVAIL="$DHD_CMDAVAIL lolcatjs"
cmdavail figlet && DHD_CMDAVAIL="$DHD_CMDAVAIL figlet"
cmdavail fortune && DHD_CMDAVAIL="$DHD_CMDAVAIL fortune"
cmdavail doctl && DHD_CMDAVAIL="$DHD_CMDAVAIL doctl"
cmdavail aws && DHD_CMDAVAIL="$DHD_CMDAVAIL aws"
cmdavail aws_completer && DHD_CMDAVAIL="$DHD_CMDAVAIL aws_completer"
cmdavail kubectl && DHD_CMDAVAIL="$DHD_CMDAVAIL kubectl"
cmdavail starship && DHD_CMDAVAIL="$DHD_CMDAVAIL starship"
# motd stuff
figlet_font_dir="$HOME/Documents/Repositories/psyops/docker/setup/figlet-fonts"
DHD_FIGLET_FONTS=
test -e "$figlet_font_dir" && DHD_FIGLET_FONTS="$figlet_font_dir"
fortunate_checkout="$HOME/Documents/Repositories/fortunate"
FORTUNATE_CHECKOUT=
test -e "$fortunate_checkout" && FORTUNATE_CHECKOUT="$fortunate_checkout"
test -e "$SHDETECT_RESULTS" && rm "$SHDETECT_RESULTS"
genresults() {
cat <<ENDRESULTS
# AUTO GENERATED FILE - MODIFICATIONS WILL BE OVERWRITTEN BY
# $0
# GENERATED $(date +%Y%m%d-%H%M%S)
export DHD_SHDETECT_INCLUDED=1
export PATH="$PATH"
export MANPATH="$MANPATH"
export GOROOT="$GOROOT"
export GOPATH="$GOPATH"
export NPM_PACKAGES="$NPM_PACKAGES"
export BUN_INSTALL="$BUN_INSTALL"
export RUBY_GEMS_PATHS="$RUBY_GEMS_PATHS"
export ANDROID_HOME="$ANDROID_HOME"
export EDITOR="$EDITOR"
export VISUAL="$EDITOR"
export FSEDIT="$EDITOR"
export DHD_DF_H="$DHD_DF_H"
export DHD_LS_TYPE="$DHD_LS_TYPE"
export DHD_LS_SUPPORTS_COLOR_AUTO="$DHD_LS_SUPPORTS_COLOR_AUTO"
export DHD_HOSTNAME="$DHD_HOSTNAME"
export DHD_CMDAVAIL="$DHD_CMDAVAIL"
export DHD_FIGLET_FONTS="$DHD_FIGLET_FONTS"
export FORTUNATE_CHECKOUT="$FORTUNATE_CHECKOUT"
export HOMEBREWDIR="$HOMEBREWDIR"
export NVM_DIR="$NVM_DIR"
# Very fast way to test precomputed command availability
dhd_cmdavail() {
test "\${DHD_CMDAVAIL#*\$1}" != "\$DHD_CMDAVAIL"
return \$?
}
ENDRESULTS
}
genresults > "$SHDETECT_RESULTS"
echo "Generated data saved to $SHDETECT_RESULTS"
echo "Now log in again, or run the '.z' alias"