-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1389 lines (1216 loc) · 51.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1389 lines (1216 loc) · 51.9 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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# shellcheck disable=SC2016
set -euo pipefail
IFS=$'\n\t'
FORCE_MODE=false
MINIMAL_MODE=false
DRY_RUN=false
COPY_MODE=false
CLEAN_BACKUPS=false
SHOW_HELP=false
INSTALL_ALL=false
SKIP_GIT_CONFIG=false
UNINSTALL_MODE=false
SKIP_TOOLS=false
BREW_UPGRADE=false
SYNC_MODE=false
TRUST_BREW_TAPS=false
OPTIONAL_INSTALL_APPROVED=false
TEST_FUNCTION="${DOTFILES_TEST_FUNCTION:-}"
for arg in "$@"; do
case "$arg" in
-dr | --dry-run)
DRY_RUN=true
echo "🧪 Running in dry-run mode. No files will be changed."
;;
-c | --copy)
COPY_MODE=true
echo "📄 Running in copy mode with backup."
;;
-cb | --clean-backups)
CLEAN_BACKUPS=true
;;
-f | --force)
FORCE_MODE=true
;;
-m | --minimal)
MINIMAL_MODE=true
;;
--skip-tools)
SKIP_TOOLS=true
;;
--brew-upgrade)
BREW_UPGRADE=true
;;
--sync)
SYNC_MODE=true
;;
--trust-brew-taps)
TRUST_BREW_TAPS=true
;;
-h | --help)
SHOW_HELP=true
;;
-a | --all)
INSTALL_ALL=true
;;
--uninstall)
UNINSTALL_MODE=true
;;
*)
echo "❌ Unknown argument: $arg" >&2
exit 2
;;
esac
done
if $SHOW_HELP; then
echo "Usage: ./install.sh [options]"
echo ""
echo "Options:"
echo " -dr, --dry-run Run in dry mode (no files modified)"
echo " -c, --copy Copy files instead of symlinking"
echo " -f, --force Skip prompts and force optional installs (does not clean backups)"
echo " -m, --minimal Install only core dotfiles (no extras or tools)"
echo " --skip-tools Skip optional package managers and tool installation"
echo " --brew-upgrade Upgrade Brewfile dependencies during macOS installation"
echo " --sync Reconcile dotfiles, shell setup, and Git defaults without tool installs or prompts"
echo " --trust-brew-taps Explicitly trust every third-party tap declared in the Brewfile"
echo " -cb, --clean-backups Offer to remove backups created by this installer"
echo " -a, --all Automatically install all optional tools"
echo " --uninstall Remove links and revert shell rc additions"
echo " -h, --help Show this help message"
exit 0
fi
if $SYNC_MODE && { $MINIMAL_MODE || $INSTALL_ALL || $FORCE_MODE || $BREW_UPGRADE || $CLEAN_BACKUPS || $TRUST_BREW_TAPS; }; then
echo "❌ --sync cannot be combined with --minimal, --all, --force, --brew-upgrade, --clean-backups, or --trust-brew-taps." >&2
exit 2
fi
if $UNINSTALL_MODE && { $COPY_MODE || $MINIMAL_MODE || $SKIP_TOOLS || $BREW_UPGRADE || $INSTALL_ALL || $SYNC_MODE || $TRUST_BREW_TAPS; }; then
echo "❌ --uninstall cannot be combined with installation-only options." >&2
exit 2
fi
if $FORCE_MODE; then
INSTALL_ALL=true
fi
if $MINIMAL_MODE; then
INSTALL_ALL=false
SKIP_GIT_CONFIG=true
SKIP_FETCH=true
SKIP_TOOLS=true
fi
if $SYNC_MODE; then
SKIP_TOOLS=true
SKIP_FETCH=true
fi
### === Defaults & Constants ===
UV_PYTHON_VERSION='3.13'
NVM_VERSION="${DOTFILES_NVM_VERSION:-v0.40.4}"
if [[ ! "$NVM_VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "❌ DOTFILES_NVM_VERSION must be a semantic version tag such as v0.40.4." >&2
exit 2
fi
COMMON_RC_LINES=(
"[[ -f ~/.shell_aliases ]] && source ~/.shell_aliases"
"[[ -f ~/.shell_functions ]] && source ~/.shell_functions"
"[[ -f ~/.git_aliases ]] && source ~/.git_aliases"
"[[ -f ~/.git_functions ]] && source ~/.git_functions"
"[[ -f ~/.history_settings ]] && source ~/.history_settings"
"[[ -f ~/.omp_init ]] && source ~/.omp_init"
'[[ -d "$HOME/.local/bin" ]] && export PATH="$HOME/.local/bin:$PATH"'
'[[ -f "$HOME/.local/share/swiftly/env.sh" ]] && . "$HOME/.local/share/swiftly/env.sh"'
)
NVM_RC_LINES=(
'export NVM_DIR="$HOME/.nvm"'
'[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"'
'[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"'
)
HOMEBREW_RC_LINES=(
'eval "$(/opt/homebrew/bin/brew shellenv)"'
'eval "$(/usr/local/bin/brew shellenv)"'
)
PATH_DEDUP_MARKER='# PATH de-dup (dotfiles installer)'
BASH_PATH_DEDUP_LINE='[ -x /usr/bin/awk ] && [ -x /usr/bin/paste ] && [ -x /usr/bin/tr ] && PATH="$([ -x /usr/bin/printf ] && /usr/bin/printf %s "$PATH" | /usr/bin/tr ":" "\n" | /usr/bin/awk '\''!seen[$0]++'\'' | /usr/bin/paste -sd:)" && export PATH'
LEGACY_PATH_DEDUP_MARKER='# Remove duplicates from PATH'
LEGACY_PATH_DEDUP_LINE='export PATH=$(echo "$PATH" | tr ":" "\n" | awk "!seen[\$0]++" | paste -sd:)'
DELTA_GIT_DEFAULTS=(
"core.pager=delta"
"interactive.diffFilter=delta --color-only"
"delta.navigate=true"
"delta.dark=true"
"delta.side-by-side=true"
"delta.line-numbers=true"
)
### === Define dotfiles ===
BASEDIR=$(cd "$(dirname "$0")" && pwd)
DOTFILES_HOME="$BASEDIR"
MACOS_BREWFILE="$DOTFILES_HOME/macos/Brewfile"
MACOS_DOCK_SCRIPT="$DOTFILES_HOME/macos/dock.sh"
MACOS_DEFAULTS_SCRIPT="$DOTFILES_HOME/macos/defaults.sh"
GIT_DEFAULTS_FILE="$DOTFILES_HOME/git/defaults.conf"
GIT_CONFIG_STATE_DIR="$HOME/.config/dotfiles/installer-state"
GIT_CONFIG_STATE_FILE="$GIT_CONFIG_STATE_DIR/git-config.before"
BACKUP_MANIFEST="$GIT_CONFIG_STATE_DIR/backups.list"
SYMLINK_KEYS=(
"$HOME/.shell_aliases"
"$HOME/.shell_functions"
"$HOME/.history_settings"
"$HOME/.omp_init"
"$HOME/.nanorc"
"$HOME/.git_aliases"
"$HOME/.git_functions"
"$HOME/.global.gitignore"
)
SYMLINK_VALUES=(
"$DOTFILES_HOME/general/.aliases"
"$DOTFILES_HOME/general/.functions"
"$DOTFILES_HOME/general/.history_settings"
"$DOTFILES_HOME/general/.omp_init"
"$DOTFILES_HOME/nano/.nanorc"
"$DOTFILES_HOME/git/.git_aliases"
"$DOTFILES_HOME/git/.git_functions"
"$DOTFILES_HOME/git/global.gitignore"
)
REQUIRED_TOOLS=(nano docker swift git)
### === Detect Environment ===
OS="$(uname -s)"
SHELL_NAME=$(basename "$SHELL")
if $TRUST_BREW_TAPS && [ "$OS" != "Darwin" ]; then
echo "❌ --trust-brew-taps is only available on macOS." >&2
exit 2
fi
printf "\n🔍 Detected OS: %s\n" "$OS"
printf "🔍 Detected Shell: %s\n" "$SHELL_NAME"
add_to_rc_if_not_present() {
local rc_file="${1/#\~/$HOME}" # expand ~ to $HOME
local line_to_add="$2"
if [ -f "$rc_file" ] && grep -Fq "$line_to_add" "$rc_file"; then
echo "📄 $line_to_add found in $rc_file, no need to add"
else
if $DRY_RUN; then
echo "🧪 Would add to $rc_file: $line_to_add"
else
echo "📝 adding $line_to_add to $rc_file"
echo "$line_to_add" >>"$rc_file"
fi
fi
}
record_backup() {
local backup_path="$1"
mkdir -p "$GIT_CONFIG_STATE_DIR"
touch "$BACKUP_MANIFEST"
if ! grep -Fqx -- "$backup_path" "$BACKUP_MANIFEST"; then
printf '%s\n' "$backup_path" >>"$BACKUP_MANIFEST"
fi
}
backup_existing_path() {
local path="$1"
local backup_path
local counter=0
backup_path="${path}.bak.$(date +%s)"
while [ -e "$backup_path" ] || [ -L "$backup_path" ]; do
counter=$((counter + 1))
backup_path="${path}.bak.$(date +%s).$counter"
done
mv -- "$path" "$backup_path"
record_backup "$backup_path"
printf '%s\n' "$backup_path"
}
calculate_sha256() {
local path="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$path" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$path" | awk '{print $1}'
else
return 1
fi
}
verify_sha256_file() {
local expected_sha256="$1"
local path="$2"
local actual_sha256
if [[ ! "$expected_sha256" =~ ^[[:xdigit:]]{64}$ ]]; then
echo "❌ Invalid SHA-256 digest for $path" >&2
return 2
fi
if ! actual_sha256=$(calculate_sha256 "$path"); then
echo "❌ No SHA-256 utility available; refusing to use $path" >&2
return 1
fi
if [ "$actual_sha256" != "$expected_sha256" ]; then
echo "❌ SHA-256 mismatch for $path" >&2
return 1
fi
}
run_remote_script() {
local url="$1"
shift
local expected_sha256=""
local script status
if [ "${1:-}" = "--sha256" ]; then
if [ "$#" -lt 2 ]; then
echo "❌ --sha256 requires a SHA-256 digest for $url" >&2
return 2
fi
expected_sha256="$2"
shift 2
if [[ ! "$expected_sha256" =~ ^[[:xdigit:]]{64}$ ]]; then
echo "❌ Invalid SHA-256 digest for $url" >&2
return 2
fi
fi
script=$(mktemp "${TMPDIR:-/tmp}/dotfiles-download.XXXXXX")
if ! curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 -o "$script" "$url"; then
rm -f "$script"
return 1
fi
if [ -n "$expected_sha256" ]; then
if ! verify_sha256_file "$expected_sha256" "$script"; then
rm -f "$script"
return 1
fi
echo "🔐 Verified SHA-256 for $url"
fi
if bash "$script" "$@"; then
status=0
else
status=$?
fi
rm -f "$script"
return "$status"
}
remove_from_rc_if_present() {
local rc_file="${1/#\~/$HOME}"
local line_to_remove="$2"
if [ -f "$rc_file" ] && grep -Fq "$line_to_remove" "$rc_file"; then
if $DRY_RUN; then
echo "🧪 Would remove line from $rc_file: $line_to_remove"
else
echo "🧽 removing line from $rc_file: $line_to_remove"
# Create a temp file safely
tmp_file="${rc_file}.tmp.$$"
grep -Fv "$line_to_remove" "$rc_file" >"$tmp_file" || true
mv "$tmp_file" "$rc_file"
fi
fi
}
cleanup_legacy_path_dedup() {
local rc_file="$1"
remove_from_rc_if_present "$rc_file" "$LEGACY_PATH_DEDUP_MARKER"
remove_from_rc_if_present "$rc_file" "$LEGACY_PATH_DEDUP_LINE"
}
apt_package_installed() {
dpkg -s "$1" >/dev/null 2>&1
}
install_required_apt_package() {
local pkg="$1"
if apt_package_installed "$pkg"; then
echo "✅ $pkg already installed"
else
echo "📦 Installing $pkg..."
sudo apt install -y "$pkg"
fi
}
install_optional_apt_package() {
local pkg="$1"
if apt_package_installed "$pkg"; then
echo "✅ $pkg already installed"
else
echo "📦 Installing $pkg..."
if sudo apt install -y "$pkg"; then
echo "✅ Installed $pkg"
else
echo "⚠️ $pkg is unavailable from apt on this system; continuing without it"
fi
fi
}
install_eza_from_apt_repository() {
local key_url="https://raw.githubusercontent.com/eza-community/eza/main/deb.asc"
local keyring="/etc/apt/keyrings/gierens.gpg"
local source_file="/etc/apt/sources.list.d/gierens.list"
local architecture key_tmp
if apt_package_installed eza; then
echo "✅ eza already installed"
return 0
fi
architecture=$(dpkg --print-architecture 2>/dev/null || true)
if [ -z "$architecture" ]; then
echo "⚠️ Cannot determine the Debian architecture; skipping eza."
return 0
fi
echo "📥 Configuring the official eza apt repository..."
key_tmp=$(mktemp "${TMPDIR:-/tmp}/eza-key.XXXXXX")
if ! curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 -o "$key_tmp" "$key_url"; then
rm -f "$key_tmp"
echo "⚠️ Could not download the eza repository key; skipping eza."
return 0
fi
if [ -n "${DOTFILES_EZA_KEY_SHA256:-}" ]; then
if ! verify_sha256_file "$DOTFILES_EZA_KEY_SHA256" "$key_tmp"; then
rm -f "$key_tmp"
echo "⚠️ eza repository key verification failed; skipping eza."
return 0
fi
echo "🔐 Verified SHA-256 for the eza repository key"
fi
if ! sudo mkdir -p "$(dirname "$keyring")" || ! sudo gpg --dearmor --yes --output "$keyring" "$key_tmp"; then
rm -f "$key_tmp"
echo "⚠️ Could not install the eza repository key; skipping eza."
return 0
fi
rm -f "$key_tmp"
sudo chmod 0644 "$keyring"
printf 'deb [arch=%s signed-by=%s] http://deb.gierens.de stable main\n' \
"$architecture" "$keyring" | sudo tee "$source_file" >/dev/null
sudo chmod 0644 "$source_file"
sudo apt update
if ! sudo apt install -y eza; then
echo "⚠️ eza is unavailable from the official repository; skipping it."
fi
}
ensure_local_bin_on_path() {
if [ -d "$HOME/.local/bin" ] && [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
export PATH="$HOME/.local/bin:$PATH"
fi
}
report_macos_sudo_touch_id_status() {
local sudo_pam="/etc/pam.d/sudo"
local sudo_tid_line='auth sufficient pam_tid.so'
if grep -Fqx "$sudo_tid_line" "$sudo_pam" 2>/dev/null; then
echo "✅ Touch ID is already enabled for sudo via $sudo_pam"
return 0
fi
echo "⚠️ Touch ID for sudo does not appear to be enabled."
echo "ℹ️ Manual recovery path:"
echo " 1. Edit /etc/pam.d/sudo with sudo"
echo " 2. Ensure this exact line is present: $sudo_tid_line"
echo " 3. Test with: sudo -k && sudo -v"
}
offer_github_authentication() {
if ! command -v gh >/dev/null 2>&1; then
return 0
fi
if gh auth status --hostname github.com >/dev/null 2>&1; then
echo "✅ GitHub CLI is already authenticated on github.com"
return 0
fi
if [ "${DOTFILES_TEST_INTERACTIVE:-false}" != true ] && { [ ! -t 0 ] || [ ! -t 1 ]; }; then
echo "ℹ️ gh is not authenticated. Run 'gh auth login' from an interactive shell when ready."
return 0
fi
local configure_gh_auth
read -r -p $'\n🐙 Authenticate GitHub CLI with gh auth login? [y/N]: ' configure_gh_auth || return 0
if [[ ! "$configure_gh_auth" =~ ^[Yy]$ ]]; then
echo "ℹ️ Skipping GitHub CLI authentication. Run 'gh auth login' later when ready."
return 0
fi
if gh auth login; then
echo "✅ GitHub CLI authentication completed."
echo "ℹ️ Configure Git to use gh credentials with: gh auth setup-git"
echo "ℹ️ Add an SSH authentication key with: gh ssh-key add ~/.ssh/<key>.pub --type authentication"
echo "ℹ️ Add an SSH signing key with: gh ssh-key add ~/.ssh/<key>.pub --type signing"
echo "ℹ️ Add a GPG signing key with: gh gpg-key add <public-key-file>"
else
echo "⚠️ GitHub CLI authentication was not completed; you can retry with: gh auth login"
fi
}
offer_nvm_global_package_migration() {
local previous_node="$1"
local target_node migrate_packages
target_node="$(nvm version current 2>/dev/null || echo none)"
if [ "$target_node" = "$previous_node" ] || [ "$target_node" = "none" ] || [ "$target_node" = "system" ]; then
return 0
fi
echo "ℹ️ Global npm packages are scoped to each nvm Node version."
echo " Current target: $target_node"
echo " Source version: $previous_node"
if $FORCE_MODE || $INSTALL_ALL || { [ "${DOTFILES_TEST_INTERACTIVE:-false}" != true ] && { [ ! -t 0 ] || [ ! -t 1 ]; }; }; then
echo "ℹ️ Skipping package migration in non-interactive/automatic mode."
echo " Run when ready: nvm use $target_node && nvm reinstall-packages $previous_node"
return 0
fi
read -r -p $'📦 Migrate global npm packages to the new Node version with nvm reinstall-packages? [y/N]: ' migrate_packages || return 0
if [[ "$migrate_packages" =~ ^[Yy]$ ]]; then
if nvm reinstall-packages "$previous_node"; then
echo "✅ Global npm packages migrated from $previous_node to $target_node"
else
echo "⚠️ Global npm package migration failed; retry with: nvm use $target_node && nvm reinstall-packages $previous_node"
fi
else
echo "ℹ️ Skipping package migration. Run when ready: nvm use $target_node && nvm reinstall-packages $previous_node"
fi
}
ensure_macos_xcode_tools() {
local developer_dir xcode_version xcode_line
if ! developer_dir=$(xcode-select -p 2>/dev/null); then
echo "⚠️ Xcode developer tools are missing."
echo "ℹ️ Install Xcode from the App Store or run: xcode-select --install"
return 1
fi
if ! xcode_version=$(xcodebuild -version 2>/dev/null); then
echo "⚠️ xcodebuild is unavailable even though xcode-select points to $developer_dir"
echo "ℹ️ Install Xcode, then run: sudo xcode-select -s /Applications/Xcode.app/Contents/Developer"
return 1
fi
echo "✅ Xcode developer directory: $developer_dir"
while IFS= read -r xcode_line; do
[[ -n "$xcode_line" ]] && echo "✅ $xcode_line"
done <<<"$xcode_version"
}
report_macos_stats_quarantine_hint() {
local stats_app="/Applications/Stats.app"
if [ ! -d "$stats_app" ] || ! command -v xattr >/dev/null 2>&1; then
return 0
fi
if xattr -p com.apple.quarantine "$stats_app" >/dev/null 2>&1; then
echo "ℹ️ Stats.app still has the quarantine bit set."
echo " If it refuses to open, run:"
echo " sudo xattr -r -d com.apple.quarantine /Applications/Stats.app/"
fi
}
report_git_identity_hint() {
local git_name git_email
if ! command -v git >/dev/null 2>&1; then
return 0
fi
git_name=$(git config --global --get user.name 2>/dev/null || true)
git_email=$(git config --global --get user.email 2>/dev/null || true)
if [[ -n "$git_name" && -n "$git_email" ]]; then
return 0
fi
echo "ℹ️ Git identity is not fully configured yet."
echo " Set it with:"
echo " git config --global user.name \"Your Name\""
echo " git config --global user.email \"you@example.com\""
}
phase_banner() {
printf '\n%s\n' "────────────────────────────────────────────────"
printf '▶ %s\n' "$1"
printf '%s\n' "────────────────────────────────────────────────"
}
install_uv_python_version() {
if uv python install --preview --default "$UV_PYTHON_VERSION"; then
ensure_local_bin_on_path
return 0
fi
echo "⚠️ uv default executables require preview mode; falling back to Python $UV_PYTHON_VERSION without default executables."
uv python install "$UV_PYTHON_VERSION" || true
ensure_local_bin_on_path
}
trust_brewfile_taps() {
local tap
while IFS= read -r tap; do
echo "🔐 Trusting Homebrew tap: $tap"
brew trust --tap "$tap"
done < <(sed -nE 's/^[[:space:]]*tap[[:space:]]+"([^"]+)".*/\1/p' "$MACOS_BREWFILE")
}
apply_rc_lines() {
local action="$1"
local rc_file="$2"
shift 2
local line
for line in "$@"; do
if [[ "$action" == "add" ]]; then
add_to_rc_if_not_present "$rc_file" "$line"
else
remove_from_rc_if_present "$rc_file" "$line"
fi
done
}
snapshot_git_config_value() {
local state_file="$1"
local setting_id="$2"
local setting_key="$3"
local value
while IFS= read -r value; do
git config --file "$state_file" --add "snapshot.$setting_id" "$value"
done < <(git config --global --get-all "$setting_key" 2>/dev/null || true)
}
snapshot_git_config_key_if_missing() {
local state_file="$1"
local setting_key="$2"
local setting_id
setting_id=$(git_config_setting_id "$setting_key")
if git config --file "$state_file" --get-all "snapshot.$setting_id" >/dev/null 2>&1; then
return
fi
snapshot_git_config_value "$state_file" "$setting_id" "$setting_key"
}
snapshot_git_default_from_baseline() {
local state_file="$1"
local setting_key="$2"
local setting_id
setting_id=$(git_config_setting_id "$setting_key")
snapshot_git_config_value "$state_file" "$setting_id" "$setting_key"
}
git_config_setting_id() {
printf '%s' "$1" | tr '[:upper:].' '[:lower:]-'
}
for_each_git_default() {
local callback="$1"
shift
local setting_key
local setting_value
local callback_failed=false
while IFS='=' read -r setting_key setting_value; do
if [ -z "$setting_key" ] || [[ "$setting_key" == \#* ]]; then
continue
fi
if [ -z "$setting_value" ]; then
echo "❌ Invalid Git defaults entry: $setting_key" >&2
return 2
fi
if ! "$callback" "$@" "$setting_key" "$setting_value"; then
callback_failed=true
fi
done <"$GIT_DEFAULTS_FILE"
if $callback_failed; then
return 1
fi
}
snapshot_git_config_before_install() {
if [ -f "$GIT_CONFIG_STATE_FILE" ]; then
return
fi
mkdir -p "$GIT_CONFIG_STATE_DIR"
local state_tmp
state_tmp=$(mktemp "$GIT_CONFIG_STATE_DIR/git-config.before.XXXXXX")
chmod 600 "$state_tmp"
git config --file "$state_tmp" installer.version 1
snapshot_git_config_value "$state_tmp" "core-excludesfile" "core.excludesfile"
for_each_git_default snapshot_git_default_from_baseline "$state_tmp"
mv "$state_tmp" "$GIT_CONFIG_STATE_FILE"
}
configure_git_default() {
local setting_key="$1"
local setting_value="$2"
local setting_id
setting_id=$(git_config_setting_id "$setting_key")
git config --global "$setting_key" "$setting_value"
git config --file "$GIT_CONFIG_STATE_FILE" --replace-all "managed.$setting_id" "$setting_value"
}
configure_git_default_from_baseline() {
configure_git_default "$1" "$2"
}
configure_delta_git() {
local setting_entry setting_key setting_value
if ! command -v delta >/dev/null 2>&1; then
return 0
fi
snapshot_git_config_before_install
for setting_entry in "${DELTA_GIT_DEFAULTS[@]}"; do
IFS='=' read -r setting_key setting_value <<<"$setting_entry"
snapshot_git_config_key_if_missing "$GIT_CONFIG_STATE_FILE" "$setting_key"
configure_git_default "$setting_key" "$setting_value"
done
echo "✅ Configured Git to use delta with navigation, side-by-side output, and line numbers."
}
restore_git_default() {
local setting_key="$1"
local setting_id
local installed_value
local current_value
local snapshot_value
setting_id=$(git_config_setting_id "$setting_key")
installed_value=$(git config --file "$GIT_CONFIG_STATE_FILE" --get "managed.$setting_id" 2>/dev/null || true)
if [ -z "$installed_value" ]; then
return
fi
current_value=$(git config --global --get-all "$setting_key" 2>/dev/null || true)
if [ "$current_value" != "$installed_value" ]; then
echo "⚠️ Keeping Git setting $setting_key because it changed after installation."
return 1
fi
git config --global --unset-all "$setting_key" || true
while IFS= read -r snapshot_value; do
git config --global --add "$setting_key" "$snapshot_value"
done < <(git config --file "$GIT_CONFIG_STATE_FILE" --get-all "snapshot.$setting_id" 2>/dev/null || true)
}
restore_git_default_from_baseline() {
restore_git_default "$1"
}
restore_git_config_before_install() {
if [ ! -f "$GIT_CONFIG_STATE_FILE" ]; then
return
fi
if $DRY_RUN; then
echo "🧪 Would restore Git settings managed by the installer."
return
fi
if ! command -v git >/dev/null 2>&1; then
echo "⚠️ git not found; leaving installer-managed Git settings in place."
return
fi
local restore_failed=false
echo "🔧 Restoring Git settings managed by the installer..."
restore_git_default "core.excludesfile" || restore_failed=true
for_each_git_default restore_git_default_from_baseline || restore_failed=true
local delta_entry delta_key
for delta_entry in "${DELTA_GIT_DEFAULTS[@]}"; do
delta_key="${delta_entry%%=*}"
restore_git_default "$delta_key" || restore_failed=true
done
if ! $restore_failed; then
rm -f "$GIT_CONFIG_STATE_FILE"
rmdir "$GIT_CONFIG_STATE_DIR" 2>/dev/null || true
fi
}
run_uninstall() {
echo -e "\n🧹 Uninstalling dotfiles symlinks and shell rc additions..."
restore_git_config_before_install
# Remove symlinks we manage if they point to our repo
for i in "${!SYMLINK_KEYS[@]}"; do
local dest src
dest="${SYMLINK_KEYS[$i]}"
src="${SYMLINK_VALUES[$i]}"
if [ -L "$dest" ] && [ "$(readlink "$dest")" = "$src" ]; then
if $DRY_RUN; then
echo "🧪 Would remove symlink $dest"
else
echo "🗑️ Removing symlink $dest"
rm -f "$dest"
fi
fi
done
# Remove startup additions (guarded lines)
for rc in ~/.zshrc ~/.bashrc; do
apply_rc_lines remove "$rc" "${COMMON_RC_LINES[@]}"
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && nice_print_aliases"
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && { fastfetch 2>/dev/null; } &>/dev/null"
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && { screenfetch 2>/dev/null; } &>/dev/null"
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && { fastfetch 2>/dev/null; } 2>/dev/null"
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && { screenfetch 2>/dev/null; } 2>/dev/null"
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && fastfetch 2>/dev/null"
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && screenfetch 2>/dev/null"
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && eval \"\$(zoxide init zsh)\""
remove_from_rc_if_present "$rc" "[[ \$- == *i* ]] && eval \"\$(zoxide init bash)\""
remove_from_rc_if_present "$rc" "source /usr/share/fzf/key-bindings.bash"
remove_from_rc_if_present "$rc" "source /usr/share/fzf/completion.bash"
remove_from_rc_if_present "$rc" "source /usr/share/fzf/key-bindings.zsh"
remove_from_rc_if_present "$rc" "source /usr/share/fzf/completion.zsh"
remove_from_rc_if_present "$rc" "$PATH_DEDUP_MARKER"
remove_from_rc_if_present "$rc" 'typeset -U path'
remove_from_rc_if_present "$rc" '[ -x /usr/bin/awk ] && [ -x /usr/bin/paste ] && [ -x /usr/bin/tr ] && PATH="$([ -x /usr/bin/printf ] && /usr/bin/printf %s "$PATH" | /usr/bin/tr ":" "\n" | /usr/bin/awk '\''!seen[$0]++'\'' | /usr/bin/paste -sd:)" && export PATH'
apply_rc_lines remove "$rc" "${NVM_RC_LINES[@]}"
done
for rc in ~/.zprofile ~/.bash_profile; do
apply_rc_lines remove "$rc" "${HOMEBREW_RC_LINES[@]}"
done
}
if [ -n "$TEST_FUNCTION" ]; then
case "$TEST_FUNCTION" in
gh-auth)
offer_github_authentication
;;
nvm-migrate)
offer_nvm_global_package_migration "${DOTFILES_TEST_NVM_PREVIOUS:-v0.0.0}"
;;
remote-script)
run_remote_script "${DOTFILES_TEST_REMOTE_URL:-https://example.invalid/script.sh}" --sha256 "${DOTFILES_TEST_REMOTE_SHA256:-}"
;;
*)
echo "❌ Unknown DOTFILES_TEST_FUNCTION: $TEST_FUNCTION" >&2
exit 2
;;
esac
exit 0
fi
if $UNINSTALL_MODE; then
run_uninstall
echo -e "\n🚀 Uninstall complete."
exit 0
fi
### === Symlink Files ===
echo -e "\n🔗 Linking or copying files..."
for i in "${!SYMLINK_KEYS[@]}"; do
dest="${SYMLINK_KEYS[$i]}"
src="${SYMLINK_VALUES[$i]}"
if $DRY_RUN; then
action=$([[ $COPY_MODE == true ]] && echo "copy" || echo "link")
echo "🧪 Would $action $dest → $src"
elif $COPY_MODE; then
if [ -e "$dest" ] || [ -L "$dest" ]; then
backup_path=$(backup_existing_path "$dest")
echo "📦 Backed up $dest -> $backup_path"
fi
cp -a "$src" "$dest"
echo "📄 Copied $src → $dest"
else
if [ -e "$dest" ] || [ -L "$dest" ]; then
if [ ! -L "$dest" ] || [ "$(readlink "$dest")" != "$src" ]; then
backup_path=$(backup_existing_path "$dest")
echo "📦 Backed up existing $dest -> $backup_path"
fi
fi
ln -sf "$src" "$dest"
echo "✅ Linked $dest → $src"
fi
done
### === Optional Tools Install ===
if ! $MINIMAL_MODE && ! $SKIP_TOOLS && ! $DRY_RUN; then
if $INSTALL_ALL; then
do_install="y"
else
if [[ "$OS" == "Darwin" ]]; then
read -r -p $'\n✨ Install macOS packages and defaults? (Brewfile + recommended system settings + Dock layout)? [y/N]: ' do_install
else
read -r -p $'\n✨ Install optional tools? (fzf, eza, bat, zoxide, oh-my-posh, nano, fd, ripgrep, shellcheck, shfmt, uv, swiftly, gh)? [y/N]: ' do_install
fi
fi
if [[ "$do_install" =~ ^[Yy]$ ]]; then
OPTIONAL_INSTALL_APPROVED=true
echo -e "\n📦 Installing tools..."
case "$OS" in
Darwin)
if ! ensure_macos_xcode_tools; then
exit 1
fi
if ! command -v brew >/dev/null; then
echo "🍺 Homebrew not found. Installing..."
if [ -n "${DOTFILES_HOMEBREW_INSTALL_SHA256:-}" ]; then
brew_install_args=(--sha256 "$DOTFILES_HOMEBREW_INSTALL_SHA256")
else
brew_install_args=()
fi
if ! run_remote_script "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh" "${brew_install_args[@]}"; then
echo "❌ Homebrew installation failed." >&2
exit 1
fi
# Initialize Homebrew in current session and future shells
if [ -x /opt/homebrew/bin/brew ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
apply_rc_lines add "$HOME/.zprofile" "${HOMEBREW_RC_LINES[0]}"
apply_rc_lines add "$HOME/.bash_profile" "${HOMEBREW_RC_LINES[0]}"
elif [ -x /usr/local/bin/brew ]; then
eval "$(/usr/local/bin/brew shellenv)"
apply_rc_lines add "$HOME/.zprofile" "${HOMEBREW_RC_LINES[1]}"
apply_rc_lines add "$HOME/.bash_profile" "${HOMEBREW_RC_LINES[1]}"
fi
fi
if $TRUST_BREW_TAPS; then
phase_banner "Homebrew tap trust"
trust_brewfile_taps
fi
phase_banner "Homebrew Bundle"
if [ -f "$MACOS_BREWFILE" ]; then
echo "📦 Installing macOS packages from Brewfile..."
if $BREW_UPGRADE; then
brew bundle install --verbose --file "$MACOS_BREWFILE"
else
# Bootstrap missing packages without taking ownership of every installed update.
brew bundle install --no-upgrade --verbose --file "$MACOS_BREWFILE"
fi
else
echo "⚠️ Missing macOS Brewfile at $MACOS_BREWFILE; skipping package install."
fi
ensure_local_bin_on_path
phase_banner "Shell integration"
# fzf keybindings/completions (Homebrew layout)
if $INSTALL_ALL || $FORCE_MODE; then configure_fzf="y"; else read -r -p $'\n🎹 Enable fzf keybindings and completions? [y/N]: ' configure_fzf; fi
if [[ "$configure_fzf" =~ ^[Yy]$ ]]; then
echo "⚙️ Configuring fzf keybindings/completions..."
"$(brew --prefix)/opt/fzf/install" --key-bindings --completion --no-update-rc || true
zsh_bind="$(brew --prefix)/opt/fzf/shell/key-bindings.zsh"
zsh_comp="$(brew --prefix)/opt/fzf/shell/completion.zsh"
bash_bind="$(brew --prefix)/opt/fzf/shell/key-bindings.bash"
bash_comp="$(brew --prefix)/opt/fzf/shell/completion.bash"
if [[ "$SHELL_NAME" == "zsh" ]]; then
if [ -f "$zsh_bind" ]; then apply_rc_lines add "$HOME/.zshrc" "source $zsh_bind"; fi
if [ -f "$zsh_comp" ]; then apply_rc_lines add "$HOME/.zshrc" "source $zsh_comp"; fi
else
if [ -f "$bash_bind" ]; then apply_rc_lines add "$HOME/.bashrc" "source $bash_bind"; fi
if [ -f "$bash_comp" ]; then apply_rc_lines add "$HOME/.bashrc" "source $bash_comp"; fi
fi
fi
phase_banner "macOS system preferences"
if [ -f "$MACOS_DEFAULTS_SCRIPT" ]; then
if $INSTALL_ALL || $FORCE_MODE; then apply_macos_defaults="y"; else read -r -p $'\n🍎 Apply recommended macOS defaults? [y/N]: ' apply_macos_defaults; fi
if [[ "$apply_macos_defaults" =~ ^[Yy]$ ]]; then
bash "$MACOS_DEFAULTS_SCRIPT" --restart
fi
else
echo "⚠️ Missing macOS defaults script at $MACOS_DEFAULTS_SCRIPT; skipping defaults."
fi
if [ -f "$MACOS_DOCK_SCRIPT" ]; then
if $INSTALL_ALL || $FORCE_MODE; then apply_macos_dock="y"; else read -r -p $'\n🧷 Apply saved Dock layout? [y/N]: ' apply_macos_dock; fi
if [[ "$apply_macos_dock" =~ ^[Yy]$ ]]; then
bash "$MACOS_DOCK_SCRIPT" --restart
fi
else
echo "⚠️ Missing macOS Dock script at $MACOS_DOCK_SCRIPT; skipping Dock layout."
fi
if $INSTALL_ALL || $FORCE_MODE; then check_macos_sudo_touch_id="y"; else read -r -p $'\n🔐 Check Touch ID for sudo configuration? [Y/n]: ' check_macos_sudo_touch_id; fi
if [[ ! "$check_macos_sudo_touch_id" =~ ^[Nn]$ ]]; then
report_macos_sudo_touch_id_status
fi
# Optional: install pinned Python via uv
phase_banner "Toolchain checks"
if command -v uv >/dev/null 2>&1; then
if $INSTALL_ALL || $FORCE_MODE; then uv_install_py="y"; else read -r -p $'🐍 Install Python 3.13 via uv? [y/N]: ' uv_install_py; fi
if [[ "$uv_install_py" =~ ^[Yy]$ ]]; then
install_uv_python_version
fi
fi
# Optional: install latest stable Swift toolchain via swiftly
if command -v swiftly >/dev/null 2>&1; then
if $INSTALL_ALL || $FORCE_MODE; then sw_install_tc="y"; else read -r -p $'🦅 Install latest stable Swift toolchain via swiftly? [y/N]: ' sw_install_tc; fi
if [[ "$sw_install_tc" =~ ^[Yy]$ ]]; then
swiftly install stable || true
fi
fi
phase_banner "Post-install hints"
report_macos_stats_quarantine_hint
;;
Linux)