-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap
executable file
·669 lines (559 loc) · 13.7 KB
/
bootstrap
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#!/usr/bin/env bash
#
#
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:: \\\
#:: ======> DOTFILES PROVISIONER ○
#:: ///
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
#
#
# Thanks:
# - https://github.com/d12frosted/environment
# - https://github.com/alrra/dotfiles
# - https://github.com/jasonheecs/ubuntu-server-setup
#
set -eo pipefail
: "${DOTFILES_HOSTNAME:=}"
: "${DOTFILES_USERNAME:=}"
: "${DOTFILES_ID_EMAIL:=}"
: "${DOTFILES_USER_PASSWORD:=}"
: "${DOTFILES_SSH_PUBLIC_KEY:=}"
: "${DOTFILES_PGP_KEY:=0x135EEDD0F71934F3}"
: "${DOTFILES_TIMEZONE:=America/New_York}"
: "${USE_HTTPS:=true}"
DOTFILES_BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF:-${DOTFILES_BRANCH:-main}}}"
DOTFILES_BRANCH="${DOTFILES_BRANCH#refs/heads/}"
readonly DOTFILES_BRANCH
readonly QUERENT="${DOTFILES_USERNAME:-montchr}"
readonly REPO_ID="montchr/dotfiles"
readonly DOTFILES_RAW_BASE_URL="https://raw.githubusercontent.com/${REPO_ID}/${DOTFILES_BRANCH}"
readonly DOTFILES_UTILS_URL="${DOTFILES_RAW_BASE_URL}/lib/utils.sh"
readonly DOTFILES_LOCK_FILE="${XDG_CACHE_HOME:-${HOME}/.cache}/dotfiles/dotfiles.lock"
: "${DOTFILES_DIR:=${HOME}/dotfiles}"
: "${XDG_BIN_HOME:=${HOME}/.local/bin}"
XDG_CONFIG_HOME="${HOME}/.config"
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:
#: ==> HELPER VARIABLES
#:
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
USER="${USER:-}"
if [[ -z "${USER}" ]]; then
USER="$(whoami)"
fi
KERNEL_NAME="${KERNEL_NAME:-}"
if [[ -z "${KERNEL_NAME}" ]]; then
KERNEL_NAME="$( uname -s | tr '[:upper:]' '[:lower:]')"
fi
OS_NAME="${OS_NAME:-}"
OS_VERSION="${OS_VERSION:-}"
if [[ -z "${OS_NAME}" || -z "${OS_VERSION}" ]]; then
case "${KERNEL_NAME}" in
darwin)
OS_NAME="macos"
OS_VERSION="$(sw_vers -productVersion)"
;;
linux)
# shellcheck disable=1091
OS_NAME="$(
. /etc/os-release
printf "%s" "${ID}"
)"
# shellcheck disable=1091
OS_VERSION="$(
. /etc/os-release
printf "%s" "${VERSION_ID}"
)"
;;
*) OS_NAME="unknown" ;;
esac
fi
PATH="$HOME/.local/bin:$PATH"
PATH=/nix/var/nix/profiles/default/bin:$PATH
PATH=/run/current-system/sw/bin:$PATH
PATH=$HOME/.nix-profile/bin:$PATH
XDG_DATA_HOME="$HOME/.local/share"
XDG_CACHE_HOME="$HOME/.cache"
XDG_BIN_HOME="${HOME}/.local/bin"
XDG_STATE_HOME="$HOME/.local/state"
DEVELOPER="${HOME}/Developer"
if [[ "$USER" != "$QUERENT" ]]; then
DEVELOPER="${HOME}/Developer/99-personal"
fi
export \
DEVELOPER \
DOTFILES_BOOTSTRAP \
DOTFILES_BRANCH \
DOTFILES_DIR \
DOTFILES_HOSTNAME \
DOTFILES_ID_EMAIL \
DOTFILES_PGP_KEY \
DOTFILES_SSH_PUBLIC_KEY \
DOTFILES_TIMEZONE \
DOTFILES_USERNAME \
DOTFILES_USER_PASSWORD \
KERNEL_NAME \
KERNEL_RELEASE \
OS_NAME \
OS_VERSION \
PATH \
QUERENT \
REPO_ID \
XDG_BIN_HOME \
XDG_CACHE_HOME \
XDG_CONFIG_HOME \
XDG_DATA_HOME \
XDG_STATE_HOME
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:
#: ==> HELPER FUNCTIONS
#:
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
#======================================
# Check for the existence of a command in the current shell environment.
#
# Copied from utils.sh for bootstrapping
#
# Usage:
# shell::has <command>...
#
# Arguments:
# Commands...
# Returns:
# 0 - When all commands are available
# 1 - When any command is not available
#========================================
function shell::has {
for cmd in "$@"; do
command -v "${cmd}" >/dev/null 2>&1 \
|| return 1
done
}
#========================================
# Download a remote file and make it executable.
#
# Copied from fetch::file for bootstrapping.
#
# Usage:
# fetch::file <path> <URL>
# Parameters:
# Path to target file
# Source URL
#========================================
function fetch::file {
local target="$1"
local url="$2"
if shell::has curl; then
curl --silent -o "${target}" "${url}"
return $?
elif shell::has wget; then
wget -qO "${target}" "${url}" &>/dev/null
return $?
else
return 1
fi
chmod a+x "${target}"
hash -r
}
#========================================
# Fetch a remote file and output its path.
#
# Usage:
# fetch_ephemeral <url>
# Arguments:
# Relative path to file
# Outputs:
# Path to file
#========================================
function fetch_ephemeral {
local url="$1"
local tmp_file=""
tmp_file="$(mktemp "/tmp/${url##*/}.XXXX")"
fetch::file "${tmp_file}" "${url}" && {
echo "${tmp_file}"
return 0
}
return 1
}
#========================================
# Verify whether the current OS version is supported by this script.
#
# Globals:
# OS_NAME
# OS_VERSION
# Outputs:
# Feedback on error.
# Returns:
# 0 - Supported.
# 1 - Unsupported.
#========================================
function verify_os {
declare -A Names Versions
local supported_version
Names=(
[macos]="macOS"
[ubuntu]="Ubuntu"
)
Versions=(
[macos]="10.10"
[ubuntu]="20.04"
)
supported_version="${Versions[$OS_NAME]}"
if [[ -z "$supported_version" ]]; then
echo "Sorry, your OS is not supported!"
return 1
fi
str::is_supported_version \
"${OS_VERSION}" \
"${supported_version}" \
&& return 0
printf "Sorry, this script is intended only for %s %s+" \
"${Names[$OS_NAME]}" \
"${supported_version}"
return 1
}
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:
#: ==> GUARDIANS
#:
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
#========================================
# Guard against a lesser optional domain.
#
# Usage:
# guard::domain <domain> <message>
# Globals:
# ALL
# GUARD_<DOMAIN>
# GUARD_IGNORE_<DOMAIN>
# Uses:
# msg::domain__lesser
# msg::domain__inactive
# Parameters:
# Domain name. Used for message prefix and uppercased nameref.
# Message.
# Outputs:
# Domain-prefixed message indicating domain is enabled.
# Domain-prefixed message indicating domain is disabled.
# Returns:
# 0 - Domain is enabled.
# 1 - Domain is disabled.
#========================================
function guard::domain {
local domain=$1
local key
key=$(str::upper "${domain}")
shift
local message="$*"
local guard_ref="GUARD_$key"
local ignore_guard_ref="GUARD_IGNORE_$key"
guard="${!guard_ref}"
ignore_guard="${!ignore_guard_ref}"
if [[ \
("${ALL}" == "true" || "${guard}" == "true") \
&& -z "${ignore_guard}" \
]]; then
msg::domain__lesser "${domain}" "${message}"
return 0
else
msg::domain__inactive "${domain}" "${message}"
return 1
fi
}
#========================================
# Guard for the `install` action.
#
# Usage:
# guard::install && { ... }
# Globals:
# ACTION
#========================================
function guard::install {
[[ "$ACTION" == "install" ]]
return
}
#========================================
# Guard for the `upgrade` action.
#
# Usage:
# guard::upgrade && { ... }
# Globals:
# ACTION
#========================================
function guard::upgrade {
[[ "$ACTION" == "upgrade" ]]
return
}
#========================================
# Guard for the `test` action.
#
# Usage:
# guard::test && { ... }
# Globals:
# ACTION
#========================================
function guard::test {
[[ "$ACTION" == "test" ]]
return
}
#========================================
# Guard for Linux systems.
#
# Usage:
# guard::linux && { ... }
# Globals:
# KERNEL_NAME
#========================================
function guard::linux {
[[ "$KERNEL_NAME" == "linux" ]]
return
}
#========================================
# Guard for Debian.
#
# Usage:
# guard::debian && { ... }
# Globals:
# OS_NAME
#========================================
function guard::debian {
[[ "$OS_NAME" == "debian" ]]
return
}
#========================================
# Guard for Ubuntu.
#
# Usage:
# guard::ubuntu && { ... }
# Globals:
# OS_NAME
#========================================
function guard::ubuntu {
[[ "$OS_NAME" == "ubuntu" ]]
return
}
#========================================
# Guard for macOS.
#
# Usage:
# guard::macos && { ... }
# Globals:
# OS_NAME
#========================================
function guard::macos {
[[ "$OS_NAME" == "macos" ]]
return
}
#========================================
# Guard for the root user.
#
# Usage:
# guard::root && { ... }
# Globals:
# USER
#========================================
function guard::root {
[[ "${USER}" == "root" ]]
return
}
#========================================
# Guard for whether Nix package manager is available.
#
# Usage:
# guard::nixified && { ... }
# Uses:
# shell::has
#========================================
function guard::nixified() {
shell::has nix nix-build nix-channel nix-env nix-shell
return
}
#========================================
# Delete the bootstrap lockfile.
#
# Globals:
# DOTFILES_LOCK_FILE
#========================================
function lock__disengage {
[[ -f "${DOTFILES_LOCK_FILE}" ]] \
&& rm -rf "${DOTFILES_LOCK_FILE}"
}
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:
#: ==> ERROR HANDLING + TRACEBACKS
#:
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
#
# https://docwhat.org/tracebacks-in-bash
#
trap _exit_trap EXIT
trap _err_trap ERR
__DID_TRACEBACK=f
function _exit_trap() {
local _ec="$?"
if [[ $_ec != 0 && ${__DID_TRACEBACK} != t ]]; then
traceback 1
fi
lock__disengage
}
function _err_trap() {
local _ec="$?"
local _cmd="${BASH_COMMAND:-unknown}"
traceback 1
__DID_TRACEBACK=t
echo "The command ${_cmd} exited with exit code ${_ec}." 1>&2
lock__disengage
}
function traceback() {
# Hide the traceback() call.
local -i start=$((${1:-0} + 1))
local -i end=${#BASH_SOURCE[@]}
local -i i=0
local -i j=0
echo "Traceback (last called is first):" 1>&2
for ((i = start; i < end; i++)); do
j=$((i - 1))
local function="${FUNCNAME[$i]}"
local file="${BASH_SOURCE[$i]}"
local line="${BASH_LINENO[$j]}"
echo " ${function}() in ${file}:${line}" 1>&2
done
}
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:
#: ==> INITIALIZATION
#:
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
if [[ -f "${DOTFILES_DIR}/lib/utils.sh" ]]
then
# shellcheck source=./lib/utils.sh
. "${DOTFILES_DIR}/lib/utils.sh"
msg::stream::story <<END
We arrive at a well-travelled outcropping,
overlooking a familiar vista.
Let's get a closer look…
END
else
printf "Summoning remote utilities from %s" "${DOTFILES_UTILS_URL}"
utils_path="$(fetch_ephemeral "${DOTFILES_UTILS_URL}")"
# shellcheck source=./lib/utils.sh
. "${utils_path}"
msg::stream::story <<END
This world is beautiful, but dark.
Let's get our bearings and see what we can see
in the land below…
END
fi
msg::domain "System" "Verifying system state"
if shell::has pfetch; then
pfetch
else
msg::domain "System" "Summoning pfetch"
pfetch="$(fetch_ephemeral "$(repo::qualify_raw_url "gh" "montchr/pfetch" "master" "pfetch")")"
chmod +x "${pfetch}" && "${pfetch}"
fi
msg::domain "System" "Verifying prerequisite dependencies"
shell::has git || {
msg::error "git not found! Aborting."
exit 1
}
shell::has gpg || {
msg::error "gpg not found! Aborting."
exit 1
}
msg::domain "System" "Syncing dotfiles"
msg::stream::story <<END
One cannot simply lift oneself up by one's own bootstraps...
END
repo::sync "${DOTFILES_DIR}" "gh" "${REPO_ID}" "${DOTFILES_BRANCH}" || exit 1
msg::domain "Travelling" "-> ${DOTFILES_DIR}"
cd "${DOTFILES_DIR}" || {
msg::stream::errors <<END
We seem to be lost in '$(pwd)'.
Something went wrong!
The world fades to black as we dematerialize…
END
exit 1
}
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:
#: ==> PREPARATION
#:
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
msg::domain "Supporting" "Defining variables"
ALL="true"
ACTION=""
case $1 in
install|upgrade|test)
ACTION=$1
shift
;;
*)
if [[ -z "$1" ]]; then
ACTION=install
else
msg::error "action '$1' is not supported"
msg::info "supported actions are: install, upgrade, test"
exit 1
fi
;;
esac
POSITIONAL=()
while [[ $# -gt 0 ]]
do
case "$1" in
"") : ;;
-*)
key=$(str::upper "${1#-}")
declare -rx "GUARD_IGNORE_${key}=true"
;;
*)
key=$(str::upper "$1")
declare -rx "GUARD_${key}=true"
ALL="false"
;;
esac
shift
done
set -- "${POSITIONAL[@]}" # restore positional parameters
export ALL
export ACTION
msg::domain "Supporting" "Checking for lockfile"
if [[ -f "${DOTFILES_LOCK_FILE}" ]]
then
msg::stream::errors <<END
Yet another world is being shaped by Eru
One must either wait patiently
or embrace the horrors of the unknown
and manually delete the lockfile:
:: ${DOTFILES_LOCK_FILE}
END
exit 1
fi
msg::domain "Supporting" "Engaging lock"
mkdir -p "$(dirname "${DOTFILES_LOCK_FILE}")"
touch "${DOTFILES_LOCK_FILE}"
trap lock__disengage INT TERM EXIT
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:
#: ==> EXPECTATION
#:
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
msg::domain "Guardian" "Ensure directories exist" && {
fs::ensure_dirs \
"${XDG_BIN_HOME}" \
"$DEVELOPER" \
"$HOME/org" \
"$HOME/org/gtd" \
"$HOME/org/mind" \
"$HOME/org/roam"
}
#====\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===\\===\\\===>
#:
#: ==> REALIZATION
#:
#====///===//===///===//===///===//===///===//===///===//===///===//===///===>
msg::success "Bootstrapping complete!"
lock__disengage
exit