-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
Β·320 lines (263 loc) Β· 7.41 KB
/
Copy pathinit.sh
File metadata and controls
executable file
Β·320 lines (263 loc) Β· 7.41 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
#
# Initializes my dotfiles
#
# Typically, this script is downloaded and executed on a new computer:
# sh -c "$(curl -fsLS https://raw.githubusercontent.com/ngscheurich/dotfiles/main/init.sh)"
#
# Usage:
# init.sh
#
set -euo pipefail
case "$(uname)" in
Darwin)
GUM_PLATFORM="Darwin_arm64"
;;
Linux)
GUM_PLATFORM="Linux_amd64"
;;
*)
echo "[fatal] Unsupported platform: $(uname)"
exit 1
;;
esac
VERSION="1.0.0"
GUM_VERSION="0.16.2"
GUM_RELEASE="gum_${GUM_VERSION}_${GUM_PLATFORM}"
GUM_ARCHIVE="${GUM_RELEASE}.tar.gz"
GUM_SBOM="${GUM_ARCHIVE}.sbom.json"
HOMEBREW_BIN="/opt/homebrew/bin"
SCRIPT_DEPS="fish"
LOCAL_BIN="${HOME}/.local/bin"
CHEZMOI_STATE="${HOME}/.local/share/chezmoi"
if [ -n "${TMPDIR:-}" ]; then
WORKDIR="${TMPDIR%/}/dotfiles"
else
WORKDIR="/tmp/dotfiles"
fi
if command -v gum >/dev/null 2>&1; then
GUM="$(command -v gum)"
else
GUM="${WORKDIR}/${GUM_RELEASE}/gum"
fi
# Helpers ------------------------------------------------------------------ {{{
log() {
if [ -x "$GUM" ]; then
level="$1"
shift
"$GUM" log --structured --time timeonly -l "$level" "$@"
else
echo "[$1]" "$@"
fi
}
raise() {
log fatal "$1"
exit 1
}
is_darwin() {
test "$(uname)" = "Darwin"
}
if is_darwin; then
CONTINUE_KEY="Return"
else
CONTINUE_KEY="Enter"
fi
wait_user() {
if [ -x "$GUM" ]; then
"$GUM" spin --spinner pulse --title "$1" -- read -r _
else
echo "$1"
read -r _
fi
}
greeting() {
title="$($GUM style --bold "π ngscheurich/dotfiles")"
version="$($GUM style --foreground 212 "v$VERSION")"
"$GUM" style \
--border-foreground 238 --border double --align center \
--width 40 --margin "1 1" --padding "1 1" \
"$title" "$version"
"$GUM" format <<EOF
# Greetings, wanderer.
Youβve stumbled upon my dotfiles setup script.
## What this script does
This is an automation script that helps me install and configure the programs I
rely on. Namely, it:
1. Installs dependencies for this script to run
2. Installs [chezmoi](https://www.chezmoi.io/)
3. Initializes and applies my current chezmoi state
Additionally, if the platform is Darwin, the macOS command line developer tools
are installed.
## Idempotency
The script attempts to check for and skip any steps that have already been
completed, so don't be (too) wary about running it multiple times.
## Cleanup
The script will make a temporary directory for any files it needs to create or
download, and it will try to clean it up.
EOF
echo ""
"$GUM" confirm "Shall we proceed?" || exit 0
}
farewell() {
echo ""
"$GUM" confirm "All done! Show system info?" && fastfetch || exit 0
}
# }}}
# Gum ------------------------------------------------------------- {{{
download_gum() {
for file in "$GUM_ARCHIVE" "$GUM_SBOM" "checksums.txt"; do
url="https://github.com/charmbracelet/gum/releases/download/v${GUM_VERSION}/${file}"
curl -fsLSO "$url"
done
}
verify_gum() {
result=$(sha256sum --check checksums.txt 2>/dev/null)
echo "$result" | grep -q "$GUM_ARCHIVE" && echo "$result" | grep -q "$GUM_SBOM"
}
install_gum() {
icon="π"
# If "$GUM" is not an executable file, install gum
if [ -x "$GUM" ]; then
log info "${icon} Gum detected." path "$GUM"
else
log info "${icon} Installing gum."
download_gum
if verify_gum; then
# This makes the gum binary available in the working directory
tar xzf "$GUM_ARCHIVE"
else
raise "Gum artifacts could not be verified."
fi
fi
}
# }}}
# macOS command line developer tools ------------------------------ {{{
install_macos_dev_tools() {
icon="π οΈ"
# If the tools path isn't found, install the tools
if xcode-select --print-path >/dev/null 2>&1; then
log info "${icon} macOS command line developer tools detected." path "$(xcode-select -p)"
else
log info "${icon} Requesting macOS command line developer tools install..."
# Pause until the tools are installed
xcode-select --install >/dev/null 2>&1
wait_user "Requested macOS command line developer tools install. Press ${CONTINUE_KEY} when complete..."
fi
}
# }}}
# Homebrew ----------------------------------------------------------------- {{{
install_homebrew() {
icon="πΊ"
# If the Homebrew bin directory doesn't exist, install Homebrew
if [ -d "$HOMEBREW_BIN" ]; then
log info "${icon} Homebrew detected." path "$HOMEBREW_BIN"
else
log info "${icon} Installing Homebrew..."
/bin/bash -c \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
zprofile="${ZDOTDIR-$HOME}/.zprofile"
echo >>"$zprofile"
# shellcheck disable=SC2016
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >>"$zprofile"
eval "$(/opt/homebrew/bin/brew shellenv)"
}
# }}}
# Script dependencies ------------------------------------------------------ {{{
dep_installed() {
brew_ls=$(brew list)
echo "$brew_ls" | grep -Fxq "$1"
}
dep_install() {
brew install --force "$1"
}
install_script_deps() {
icon="π¦"
deps_missing=0
# Check for any missing deps
for dep in $SCRIPT_DEPS; do
if ! dep_installed "$dep"; then deps_missing=1; fi
done
# If any deps are missing, install them
if [ "$deps_missing" -eq 0 ]; then
log info "${icon} Script dependencies detected." deps "$SCRIPT_DEPS"
else
log info "${icon} Installing script dependencies..." deps "$SCRIPT_DEPS"
for dep in $SCRIPT_DEPS; do
if ! dep_installed "$dep"; then
dep_install "$dep"
fi
done
fi
}
# }}}
# chezmoi ------------------------------------------------------------------ {{{
install_chezmoi() {
icon="π "
path="${LOCAL_BIN}/chezmoi"
# If chezmoi doesn't exist at the expected path, install it
if [ -f "$path" ]; then
log info "${icon} Chezmoi detected." path "$path"
else
log info "${icon} Installing chezmoi..."
sh -c "$(curl -fsLS get.chezmoi.io)" -- -b "$LOCAL_BIN"
fi
}
initialize_chezmoi() {
icon="π"
# If chezmoi state doesn't exist, initialize and apply it
if test -d "$CHEZMOI_STATE"; then
log info "${icon} Dotfiles initialized." path "$CHEZMOI_STATE"
else
log info "${icon} Initializing dotfiles..."
"${LOCAL_BIN}/chezmoi" init --apply ngscheurich
fi
}
# }}}
# Fish --------------------------------------------------------------------- {{{
change_shell_to_fish() {
icon="π"
fish=$(command -v fish)
if is_darwin; then
shell=$(dscl . -read "/Users/${USER}" UserShell | awk '{print $2}')
else
shell=$(grep "^${USER}:" /etc/passwd | cut -d: -f7)
fi
# If the current shell is not Fish, set it using chsh
if [ "$shell" = "$fish" ]; then
log info "${icon} User shell is Fish." path "$fish"
else
log info "${icon} Changing user shell to Fish..."
if ! grep -Fxq "$fish" /etc/shells; then
echo "$fish" | sudo tee -a /etc/shells
fi
chsh -s "$fish"
fi
}
# }}}
# ==============================================================================
# Main
# ------------------------------------------------------------------------------
main() {
# Create working directory
if [ ! -d "$WORKDIR" ]; then mkdir -p "$WORKDIR"; fi
cd "$WORKDIR" || raise "Could not change directory"
install_gum
# Display greeting
greeting
# macOS setup
if is_darwin; then
install_macos_dev_tools
install_homebrew
fi
install_script_deps
install_chezmoi
change_shell_to_fish
initialize_chezmoi
# Display farewell
farewell
# Clean up working directory
rm -rf "$WORKDIR"
}
main
# vim:ft=sh:fdm=marker:fdl=0