Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·1548 lines (1309 loc) · 48.2 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·1548 lines (1309 loc) · 48.2 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 zsh
# setup.sh — Bootstrap personal MacBook from scratch
# Run: ~/Workspace/dotfiles/setup.sh [--reset]
#
# Default mode: skip what's already done (safe to re-run)
# --reset mode: force re-apply everything to match dotfiles
set -o pipefail
CANONICAL_DOTFILES="$HOME/Workspace/dotfiles"
SCRIPT_DIR="${0:A:h}"
if [[ -z "${DOTFILES:-}" ]]; then
if [[ -d "$CANONICAL_DOTFILES/.git" ]]; then
DOTFILES="$CANONICAL_DOTFILES"
elif [[ -d "$SCRIPT_DIR/.git" ]]; then
DOTFILES="$SCRIPT_DIR"
else
DOTFILES="$CANONICAL_DOTFILES"
fi
fi
DOTFILES="${DOTFILES:A}"
DOTFILES_COMPAT_LINK="$HOME/.dotfiles"
GITHUB_REPO="mhismail3/dotfiles"
DOTFILES_BRANCH="main"
DOTFILES_REMOTE="https://github.com/$GITHUB_REPO.git"
RESET=false
# Parse args
for arg in "$@"; do
case "$arg" in
--reset) RESET=true ;;
--help|-h)
echo "Usage: ./setup.sh [--reset]"
echo ""
echo " Default: skip what's already done (idempotent)"
echo " --reset: force re-apply everything to match dotfiles"
exit 0
;;
esac
done
###############################################################################
# Helpers
###############################################################################
info() { printf "\n\033[1;34m→ %s\033[0m\n" "$1"; }
success() { printf "\033[1;32m✓ %s\033[0m\n" "$1"; }
warn() { printf "\033[1;33m⚠ %s\033[0m\n" "$1"; }
err() { printf "\033[1;31m✗ %s\033[0m\n" "$1" >&2; }
CONFIG_LINKS=(
".zshrc:$HOME/.zshrc"
".gitconfig:$HOME/.gitconfig"
".gitignore_global:$HOME/.gitignore_global"
".tmux.conf:$HOME/.tmux.conf"
"starship.toml:$HOME/.config/starship.toml"
)
confirm() {
local prompt="$1" default="${2:-y}" reply
if [[ "$default" == "y" ]]; then
echo -n "$prompt (Y/n) "
else
echo -n "$prompt (y/N) "
fi
read reply </dev/tty 2>/dev/null || reply="$default"
[[ -z "$reply" ]] && reply="$default"
[[ "$reply" =~ ^[Yy]$ ]]
}
symlink() {
local src="$1" dst="$2" current
[[ ! -e "$src" ]] && { warn "Source not found: $src"; return 1; }
mkdir -p "$(dirname "$dst")"
if [[ "$RESET" == "true" ]]; then
# Force: remove whatever's there and re-link
[[ -L "$dst" ]] && rm "$dst"
[[ -e "$dst" ]] && mv "$dst" "$dst.bak.$(date +%s)"
ln -s "$src" "$dst" && echo " Linked: $dst -> $src"
return 0
fi
if [[ -L "$dst" ]]; then
current="$(readlink "$dst")"
if [[ "$current" == "$src" || "${current:A}" == "${src:A}" ]]; then
echo " Already linked: $dst"
return 0
fi
fi
if [[ -L "$dst" ]]; then
mv "$dst" "$dst.bak.$(date +%s)"
ln -s "$src" "$dst" && echo " Linked: $dst -> $src"
return 0
fi
if [[ -e "$dst" ]]; then
mv "$dst" "$dst.bak.$(date +%s)"
ln -s "$src" "$dst" && echo " Linked: $dst -> $src"
return 0
fi
ln -s "$src" "$dst" && echo " Linked: $dst -> $src"
}
ensure_dotfiles_compat_link() {
local current
if [[ "$DOTFILES" == "$DOTFILES_COMPAT_LINK" ]]; then
return 0
fi
if [[ -L "$DOTFILES_COMPAT_LINK" ]]; then
current="$(readlink "$DOTFILES_COMPAT_LINK")"
if [[ "${current:A}" == "$DOTFILES" ]]; then
success "$DOTFILES_COMPAT_LINK points to $DOTFILES"
return 0
fi
mv "$DOTFILES_COMPAT_LINK" "$DOTFILES_COMPAT_LINK.bak.$(date +%s)"
elif [[ -e "$DOTFILES_COMPAT_LINK" ]]; then
warn "$DOTFILES_COMPAT_LINK exists and is not a symlink; leaving it in place"
return 0
fi
ln -s "$DOTFILES" "$DOTFILES_COMPAT_LINK"
success "Linked $DOTFILES_COMPAT_LINK -> $DOTFILES"
}
ensure_existing_ssh_key() {
local key="$1"
chmod 600 "$key" 2>/dev/null || true
if [[ ! -f "$key.pub" ]]; then
ssh-keygen -y -f "$key" > "$key.pub" 2>/dev/null || \
warn "Could not regenerate SSH public key"
fi
[[ -f "$key.pub" ]] && chmod 644 "$key.pub" 2>/dev/null || true
ssh-add -K "$key" 2>/dev/null || \
ssh-add --apple-use-keychain "$key" 2>/dev/null || \
ssh-add "$key" 2>/dev/null || true
}
fix_zsh_compinit_permissions() {
local dirs=(
"$HOMEBREW_PREFIX/share"
"$HOMEBREW_PREFIX/share/zsh"
"$HOMEBREW_PREFIX/share/zsh/site-functions"
"$HOMEBREW_PREFIX/share/zsh-completions"
)
local d
for d in "${dirs[@]}"; do
[[ -d "$d" ]] && chmod go-w "$d" 2>/dev/null || true
done
}
repair_private_internet_access() {
local app="/Applications/Private Internet Access.app"
if [[ -d "$app" ]]; then
xattr -d com.apple.quarantine "$app" 2>/dev/null || true
fi
brew install --cask private-internet-access --adopt
}
write_ssh_config() {
mkdir -p "$HOME/.ssh" && chmod 700 "$HOME/.ssh"
if [[ ! -f "$HOME/.ssh/config" ]] || [[ "$RESET" == "true" ]]; then
cat > "$HOME/.ssh/config" << 'EOF'
Host *
IgnoreUnknown UseKeychain,AddKeysToAgent
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
EOF
chmod 600 "$HOME/.ssh/config"
fi
}
toml_python() {
local py
for py in python3.14 python3.13 python3.12 python3.11 python3; do
if command -v "$py" &>/dev/null && "$py" -c 'import tomllib' &>/dev/null; then
print -r -- "$py"
return 0
fi
done
return 1
}
merge_codex_config() {
local baseline="$1" live="$2" py
py="$(toml_python)" || {
warn "No Python with tomllib found; leaving existing Codex config in place"
return 0
}
"$py" - "$baseline" "$live" <<'PY'
import json
import pathlib
import re
import shutil
import sys
import time
import tomllib
baseline_path = pathlib.Path(sys.argv[1])
live_path = pathlib.Path(sys.argv[2])
baseline = tomllib.loads(baseline_path.read_text())
live = tomllib.loads(live_path.read_text()) if live_path.exists() else {}
def deep_merge(defaults, current):
merged = {}
for key, value in defaults.items():
if isinstance(value, dict) and isinstance(current.get(key), dict):
merged[key] = deep_merge(value, current[key])
else:
merged[key] = value
for key, value in current.items():
if key not in merged:
merged[key] = value
return merged
bare_key = re.compile(r"^[A-Za-z0-9_-]+$")
def toml_key(key):
return key if bare_key.match(key) else json.dumps(key)
def toml_value(value):
if isinstance(value, bool):
return "true" if value else "false"
if isinstance(value, str):
return json.dumps(value)
if isinstance(value, int):
return str(value)
if isinstance(value, float):
return repr(value)
if isinstance(value, list):
return "[" + ", ".join(toml_value(item) for item in value) + "]"
raise TypeError(f"Unsupported TOML value: {value!r}")
def emit_table(mapping, path=()):
lines = []
scalars = [(key, value) for key, value in mapping.items() if not isinstance(value, dict)]
children = [(key, value) for key, value in mapping.items() if isinstance(value, dict)]
if path and scalars:
lines.append("[" + ".".join(toml_key(part) for part in path) + "]")
for key, value in scalars:
lines.append(f"{toml_key(key)} = {toml_value(value)}")
if scalars:
lines.append("")
for key, value in children:
lines.extend(emit_table(value, path + (key,)))
return lines
merged = deep_merge(baseline, live)
# Older bootstrap revisions wrote app configuration keys under [desktop]. The
# Codex app reads these as top-level configuration keys, so prune stale copies.
desktop = merged.get("desktop")
if isinstance(desktop, dict):
for key in (
"followUpQueueMode",
"conversationDetailMode",
"ambient-suggestions-enabled",
):
desktop.pop(key, None)
if not desktop:
merged.pop("desktop", None)
if live_path.exists():
backup = live_path.with_name(live_path.name + f".bak.{time.strftime('%Y%m%d%H%M%S')}")
shutil.copy2(live_path, backup)
live_path.parent.mkdir(parents=True, exist_ok=True)
live_path.write_text("\n".join(emit_table(merged)).rstrip() + "\n")
live_path.chmod(0o600)
PY
}
###############################################################################
# Step 1: Xcode Command Line Tools
###############################################################################
step_xcode() {
info "Xcode Command Line Tools"
if xcode-select -p &>/dev/null; then
success "Already installed"
else
xcode-select --install
echo "Waiting for installation..."
until xcode-select -p &>/dev/null; do sleep 5; done
success "Installed"
fi
}
###############################################################################
# Step 2: Homebrew
###############################################################################
step_homebrew() {
info "Homebrew"
if [[ $(uname -m) == "arm64" ]]; then
HOMEBREW_PREFIX="/opt/homebrew"
else
HOMEBREW_PREFIX="/usr/local"
fi
if [[ ! -f "$HOMEBREW_PREFIX/bin/brew" ]]; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" </dev/tty
success "Installed"
else
success "Already installed"
fi
eval "$($HOMEBREW_PREFIX/bin/brew shellenv)"
brew update || warn "Update failed (continuing)"
}
###############################################################################
# Step 3: Clone dotfiles
###############################################################################
step_dotfiles() {
info "Dotfiles"
mkdir -p "$(dirname "$DOTFILES")"
if [[ -d "$DOTFILES/.git" ]]; then
success "Already at $DOTFILES"
(cd "$DOTFILES" && git fetch origin "$DOTFILES_BRANCH" 2>/dev/null) || true
if [[ "$(cd "$DOTFILES" && git branch --show-current)" == "$DOTFILES_BRANCH" ]]; then
(cd "$DOTFILES" && git pull --rebase origin "$DOTFILES_BRANCH" 2>/dev/null) || true
else
warn "Dotfiles repo is not on $DOTFILES_BRANCH; leaving branch unchanged"
fi
elif [[ -e "$DOTFILES" ]]; then
err "$DOTFILES exists but is not a git repo"
return 1
else
git clone --branch "$DOTFILES_BRANCH" "$DOTFILES_REMOTE" "$DOTFILES" || \
{ err "Failed to clone"; return 1; }
success "Cloned"
fi
ensure_dotfiles_compat_link
}
###############################################################################
# Step 4: Brew bundle
###############################################################################
step_packages() {
info "Installing packages from Brewfile"
sudo -v < /dev/tty 2>/dev/null || sudo -v
(while true; do sudo -n true; sleep 30; kill -0 "$$" 2>/dev/null || exit; done) >/dev/null 2>&1 &
local keepalive=$!
local bundle_status=0 package_status=0
if [[ "$RESET" == "true" ]]; then
# Force reinstall/upgrade everything
brew bundle --file="$DOTFILES/Brewfile" --force || bundle_status=$?
else
brew bundle --file="$DOTFILES/Brewfile" --no-upgrade || bundle_status=$?
fi
if (( bundle_status != 0 )); then
warn "brew bundle reported failures; trying known repairs"
repair_private_internet_access || true
fi
if ! brew bundle check --no-upgrade --file="$DOTFILES/Brewfile"; then
package_status=1
fi
kill "$keepalive" 2>/dev/null || true
wait "$keepalive" 2>/dev/null || true
# Fix zsh compinit permissions
fix_zsh_compinit_permissions
if (( package_status != 0 )); then
err "Brewfile dependencies are still not satisfied"
return 1
fi
success "Packages installed"
}
###############################################################################
# Step 4b: Full Xcode app
###############################################################################
step_xcode_app() {
info "Xcode app"
local developer_dir="/Applications/Xcode.app/Contents/Developer"
if [[ ! -d "$developer_dir" ]]; then
warn "Xcode.app not installed"
echo " App Store installs can require Apple ID approval."
echo " Re-run: mas install 497799835"
return 0
fi
sudo xcode-select -s "$developer_dir" || warn "Could not select Xcode developer directory"
sudo xcodebuild -license accept 2>/dev/null || true
sudo xcodebuild -runFirstLaunch 2>/dev/null || warn "Xcode first-launch setup did not complete"
success "Xcode selected"
}
###############################################################################
# Step 5: Symlinks
###############################################################################
step_symlinks() {
info "Creating symlinks"
local entry src dst
for entry in "${CONFIG_LINKS[@]}"; do
src="${entry%%:*}"
dst="${entry#*:}"
symlink "$DOTFILES/$src" "$dst"
done
success "Symlinks created"
}
step_codex_config() {
info "Codex config"
local src="$DOTFILES/codex.config.toml"
local dst="$HOME/.codex/config.toml"
mkdir -p "$HOME/.codex"
if [[ ! -f "$src" ]]; then
warn "Baseline Codex config not found: $src"
return 0
fi
if [[ ! -e "$dst" ]]; then
cp "$src" "$dst"
chmod 600 "$dst" 2>/dev/null || true
success "Seeded $dst from baseline"
return 0
fi
if [[ -L "$dst" ]] && [[ "${dst:A}" == "$HOME/Workspace/harness/"* ]]; then
success "Codex config is managed by the personal harness"
echo " Left active harness config unchanged: ${dst:A}"
return 0
fi
if [[ -L "$dst" ]] && [[ "$(readlink "$dst")" == "$src" ]]; then
success "Already linked: $dst"
return 0
fi
merge_codex_config "$src" "$dst"
success "Merged durable Codex defaults into $dst"
echo " Preserved app/plugin/project state from the live config."
}
step_onepassword_cli() {
info "1Password CLI"
if [[ ! -d "/Applications/1Password.app" ]]; then
warn "1Password app is not installed"
return 0
fi
if ! command -v op >/dev/null 2>&1; then
warn "1Password CLI is not installed"
echo " Re-run: brew bundle --file=$DOTFILES/Brewfile --no-upgrade"
return 0
fi
if op account list --format=json 2>/dev/null | /usr/bin/python3 -c 'import json,sys; raise SystemExit(0 if json.load(sys.stdin) else 1)' 2>/dev/null; then
success "1Password CLI account is configured"
else
warn "1Password CLI app integration is not ready"
echo " In 1Password: Settings > Security > enable system unlock."
echo " In 1Password: Settings > Developer > Integrate with 1Password CLI."
echo " Then run visibly: op signin"
fi
}
###############################################################################
# Step 6: SSH key
###############################################################################
step_ssh() {
info "SSH key"
local key="$HOME/.ssh/id_ed25519"
mkdir -p "$HOME/.ssh" && chmod 700 "$HOME/.ssh"
write_ssh_config
if [[ -f "$key" ]] && [[ "$RESET" != "true" ]]; then
ensure_existing_ssh_key "$key"
success "Key already exists"
return 0
fi
if [[ -f "$key" ]] && [[ "$RESET" == "true" ]]; then
ensure_existing_ssh_key "$key"
success "Key already exists (keeping existing key)"
return 0
fi
if ! confirm "Generate SSH key?" "y"; then
echo " Skipped"
return 0
fi
ssh-keygen -t ed25519 -C "mhismail3@gmail.com" -f "$key" </dev/tty
eval "$(ssh-agent -s)" > /dev/null
ssh-add -K "$key" 2>/dev/null || ssh-add --apple-use-keychain "$key" 2>/dev/null || ssh-add "$key"
chmod 600 "$key"
chmod 644 "$key.pub"
pbcopy < "$key.pub"
echo ""
echo " Public key copied to clipboard."
echo " GitHub SSH access will be verified after gh auth."
success "SSH key configured"
}
ensure_github_ssh_key() {
local key_pub="$HOME/.ssh/id_ed25519.pub"
[[ ! -f "$key_pub" ]] && return 0
local ssh_output
ssh_output="$(ssh -T git@github.com -o StrictHostKeyChecking=accept-new 2>&1)" || true
if [[ "$ssh_output" == *"successfully authenticated"* ]]; then
success "GitHub SSH configured"
return 0
fi
warn "GitHub SSH key is not accepted yet"
if confirm "Upload SSH public key to GitHub with gh?" "y"; then
gh auth refresh -h github.com -s admin:public_key </dev/tty || \
warn "Could not refresh gh token scope"
gh ssh-key add "$key_pub" \
--title "$(hostname) $(date +%F)" \
--type authentication || warn "Could not upload SSH key"
fi
}
###############################################################################
# Step 7: GitHub CLI auth
###############################################################################
step_gh_auth() {
info "GitHub CLI authentication"
if gh auth status &>/dev/null && [[ "$RESET" != "true" ]]; then
success "Already authenticated"
else
echo " This sets GitHub CLI up for SSH git operations."
gh auth login --git-protocol ssh </dev/tty
success "Authenticated"
fi
gh config set git_protocol ssh -h github.com >/dev/null 2>&1 || true
# Git LFS (always safe to re-run)
if command -v git-lfs &>/dev/null; then
git lfs install --system 2>/dev/null || git lfs install
success "Git LFS configured"
fi
ensure_github_ssh_key
}
###############################################################################
# Step 8: Shell setup
###############################################################################
step_shell() {
info "Shell"
if [[ "$SHELL" != *"zsh"* ]]; then
chsh -s "$(which zsh)" || warn "Failed to change shell"
success "Zsh set as default"
else
success "Zsh already default"
fi
mkdir -p "$HOME/Workspace" "$HOME/.local/bin"
success "Created $HOME/Workspace and $HOME/.local/bin"
}
###############################################################################
# Step 9: Language runtimes
###############################################################################
step_languages() {
info "Language runtimes"
# Rust
local rustup_cmd=""
if command -v rustup &>/dev/null; then
rustup_cmd="$(command -v rustup)"
elif [[ -n "$HOMEBREW_PREFIX" && -x "$HOMEBREW_PREFIX/opt/rustup/bin/rustup" ]]; then
rustup_cmd="$HOMEBREW_PREFIX/opt/rustup/bin/rustup"
fi
if [[ -n "$rustup_cmd" ]]; then
if [[ ! -d "$HOME/.rustup" ]]; then
"$rustup_cmd" default stable || warn "Rust install failed"
success "Rust installed"
elif [[ "$RESET" == "true" ]]; then
"$rustup_cmd" update 2>/dev/null || true
success "Rust updated"
else
success "Rust already installed"
fi
else
warn "rustup not found"
fi
# Node (via nvm)
export NVM_DIR="$HOME/.nvm"
mkdir -p "$NVM_DIR"
if [[ -s "$HOMEBREW_PREFIX/opt/nvm/nvm.sh" ]]; then
source "$HOMEBREW_PREFIX/opt/nvm/nvm.sh"
if ! command -v node &>/dev/null; then
nvm install --lts --latest-npm 2>/dev/null && \
nvm alias default "lts/*" >/dev/null 2>&1
success "Node LTS installed"
elif [[ "$RESET" == "true" ]]; then
nvm install --lts --latest-npm --reinstall-packages-from=current 2>/dev/null && \
nvm alias default "lts/*" >/dev/null 2>&1
success "Node LTS updated"
else
success "Node already installed ($(node -v))"
fi
fi
# Ruby (via rbenv)
if command -v rbenv &>/dev/null; then
success "rbenv available"
fi
# Python
if command -v uv &>/dev/null; then
success "uv available (Python managed via uv)"
fi
success "Languages configured"
}
###############################################################################
# Step 10: Ollama
###############################################################################
step_ollama() {
info "Ollama"
if command -v ollama &>/dev/null; then
brew services start ollama 2>/dev/null || true
success "Ollama installed and set to auto-start"
else
warn "Ollama not found"
fi
}
###############################################################################
# Step 11: App preferences
###############################################################################
apply_synology_drive_preferences() {
local db_path="$HOME/Library/Application Support/SynologyDrive/data/db/sys.sqlite"
if [[ ! -f "$db_path" ]]; then
warn "Synology Drive preferences skipped; sync database does not exist yet"
echo " Complete Synology Drive setup, then re-run ./setup.sh."
return 0
fi
if ! command -v sqlite3 &>/dev/null; then
warn "sqlite3 not found; could not apply Synology Drive preferences"
return 0
fi
sqlite3 "$db_path" <<'SQL'
BEGIN;
UPDATE system_table SET value = '0' WHERE key = 'enable_desktop_notification';
INSERT INTO system_table (key, value)
SELECT 'enable_desktop_notification', '0'
WHERE changes() = 0;
UPDATE system_table SET value = '1' WHERE key = 'use_black_white_icon';
INSERT INTO system_table (key, value)
SELECT 'use_black_white_icon', '1'
WHERE changes() = 0;
COMMIT;
SQL
success "Synology Drive preferences applied"
echo " File event notifications disabled; minimalist menu bar icon enabled."
}
apply_google_drive_preferences() {
local app="/Applications/Google Drive.app"
local preferences_domain="com.google.drivefs"
if [[ ! -d "$app" ]]; then
warn "Google Drive preferences skipped; app is not installed"
return 0
fi
defaults write "$preferences_domain" "NSStatusItem Visible Item-0" -bool false
killall cfprefsd >/dev/null 2>&1 || true
success "Google Drive preferences applied"
echo " Menu bar status icon hidden."
echo " If Google Drive is already running, quit and reopen it for the icon change to apply."
}
apply_docker_desktop_preferences() {
local app="/Applications/Docker.app"
local settings_path="$HOME/Library/Group Containers/group.com.docker/settings-store.json"
local preferences_domain="com.electron.dockerdesktop"
local py
if [[ ! -d "$app" ]]; then
warn "Docker Desktop preferences skipped; app is not installed"
return 0
fi
for py in python3.14 python3.13 python3.12 python3.11 python3; do
command -v "$py" &>/dev/null && break
py=""
done
if [[ -z "$py" ]]; then
warn "Python not found; Docker Desktop settings store skipped"
return 0
fi
if ! "$py" - "$settings_path" <<'PY'
import json
import pathlib
import sys
path = pathlib.Path(sys.argv[1])
path.parent.mkdir(parents=True, exist_ok=True)
if path.exists():
data = json.loads(path.read_text())
else:
data = {}
data.update({
"AutoStart": False,
"UseContainerdSnapshotter": True,
})
path.write_text(json.dumps(data, indent=2, sort_keys=True) + "\n")
PY
then
warn "Could not update Docker Desktop settings store"
return 1
fi
defaults write "$preferences_domain" "NSStatusItem Visible Item-0" -bool false
killall cfprefsd >/dev/null 2>&1 || true
success "Docker Desktop preferences applied"
echo " Login launch disabled, containerd image store enabled, menu bar status icon hidden."
echo " If Docker Desktop is already running, quit and reopen it for the icon change to apply."
}
apply_private_internet_access_preferences() {
local app="/Applications/Private Internet Access.app"
local settings_path="$HOME/Library/Preferences/com.privateinternetaccess.vpn/clientsettings.json"
local py
if [[ ! -d "$app" ]]; then
warn "Private Internet Access preferences skipped; app is not installed"
return 0
fi
if command -v piactl &>/dev/null; then
piactl set protocol openvpn >/dev/null 2>&1 || warn "Could not set PIA protocol"
piactl set region auto >/dev/null 2>&1 || warn "Could not set PIA region"
piactl set allowlan true >/dev/null 2>&1 || warn "Could not set PIA LAN access"
piactl set requestportforward false >/dev/null 2>&1 || warn "Could not set PIA port forwarding"
piactl set debuglogging false >/dev/null 2>&1 || warn "Could not set PIA debug logging"
else
warn "piactl not found; PIA daemon preferences skipped"
fi
for py in python3.14 python3.13 python3.12 python3.11 python3; do
command -v "$py" &>/dev/null && break
py=""
done
if [[ -z "$py" ]]; then
warn "Python not found; PIA client preferences skipped"
return 0
fi
"$py" - "$settings_path" <<'PY'
import json
import pathlib
import sys
path = pathlib.Path(sys.argv[1])
path.parent.mkdir(parents=True, exist_ok=True)
if path.exists():
data = json.loads(path.read_text())
else:
data = {}
data.update({
"connectOnLaunch": False,
"dashboardFrame": "popup",
"desktopNotifications": True,
"iconSet": "auto",
"regionSortKey": "latency",
"themeName": "dark",
})
path.write_text(json.dumps(data, indent=2, sort_keys=True) + "\n")
PY
success "Private Internet Access preferences applied"
echo " Auto region, OpenVPN, LAN allowed, no port forwarding, no connect-on-launch."
}
apply_qbittorrent_preferences() {
local app="/Applications/qBittorrent.app"
local config_path="$HOME/.config/qBittorrent/qBittorrent.ini"
local download_dir="$HOME/Downloads/qBittorrent"
local incomplete_dir="$download_dir/incomplete"
local py
if [[ ! -d "$app" ]]; then
warn "qBittorrent preferences skipped; app is not installed"
return 0
fi
if pgrep -x qbittorrent >/dev/null 2>&1; then
warn "qBittorrent preferences skipped; quit qBittorrent and re-run ./setup.sh"
return 0
fi
mkdir -p "$download_dir" "$incomplete_dir" "${config_path:h}"
for py in python3.14 python3.13 python3.12 python3.11 python3; do
command -v "$py" &>/dev/null && break
py=""
done
if [[ -z "$py" ]]; then
warn "Python not found; qBittorrent preferences skipped"
return 0
fi
"$py" - "$config_path" "$download_dir" "$incomplete_dir" <<'PY'
import configparser
import pathlib
import sys
config_path = pathlib.Path(sys.argv[1])
download_dir = pathlib.Path(sys.argv[2])
incomplete_dir = pathlib.Path(sys.argv[3])
config = configparser.RawConfigParser(delimiters=("=",), strict=False)
config.optionxform = str
if config_path.exists():
config.read(config_path)
def put(section, key, value):
if not config.has_section(section):
config.add_section(section)
config.set(section, key, value)
put("BitTorrent", r"Session\AddExtensionToIncompleteFiles", "true")
put("BitTorrent", r"Session\AddTorrentStopped", "true")
put("BitTorrent", r"Session\AnonymousModeEnabled", "true")
put("BitTorrent", r"Session\DefaultSavePath", str(download_dir))
put("BitTorrent", r"Session\DHTEnabled", "true")
put("BitTorrent", r"Session\LSDEnabled", "false")
put("BitTorrent", r"Session\PeXEnabled", "true")
put("BitTorrent", r"Session\TempPath", str(incomplete_dir))
put("BitTorrent", r"Session\TempPathEnabled", "true")
put("Core", "AutoDeleteAddedTorrentFile", "Never")
put("Network", "PortForwardingEnabled", "false")
put("Network", r"Proxy\HostnameLookupEnabled", "false")
put("Network", r"Proxy\Profiles\BitTorrent", "true")
put("Network", r"Proxy\Profiles\Misc", "true")
put("Network", r"Proxy\Profiles\RSS", "true")
put("Network", r"Proxy\Type", "None")
put("Preferences", r"Advanced\AnonymousMode", "true")
put("Preferences", r"Advanced\trackerPortForwarding", "false")
put("Preferences", r"Connection\UPnP", "false")
put("Preferences", r"Downloads\SavePath", str(download_dir))
put("Preferences", r"Downloads\TempPath", str(incomplete_dir))
put("Preferences", r"Downloads\TempPathEnabled", "true")
put("Preferences", r"General\StatusbarExternalIPDisplayed", "false")
put("Preferences", r"MailNotification\enabled", "false")
put("Preferences", r"MailNotification\req_auth", "true")
put("Preferences", r"WebUI\Enabled", "false")
put("Preferences", r"WebUI\UseUPnP", "false")
with config_path.open("w") as f:
config.write(f, space_around_delimiters=False)
PY
success "qBittorrent preferences applied"
echo " Downloads go to $download_dir; torrents add stopped; anonymous mode on; UPnP/WebUI off."
}
resolve_tailscale_peer_ip() {
local peer="$1" status_json py
command -v tailscale &>/dev/null || return 1
status_json="$(mktemp)"
if ! tailscale status --json >"$status_json" 2>/dev/null; then
rm -f "$status_json"
return 1
fi
for py in python3.14 python3.13 python3.12 python3.11 python3; do
command -v "$py" &>/dev/null && break
py=""
done
if [[ -z "$py" ]]; then
rm -f "$status_json"
return 1
fi
"$py" - "$peer" "$status_json" <<'PY'
import json
import pathlib
import sys
expected = sys.argv[1].casefold()
data = json.loads(pathlib.Path(sys.argv[2]).read_text())
for peer in (data.get("Peer") or {}).values():
names = [
peer.get("HostName") or "",
peer.get("DNSName") or "",
peer.get("Name") or "",
]
if any(expected in name.casefold() for name in names):
ips = peer.get("TailscaleIPs") or []
if ips:
print(ips[0])
raise SystemExit(0)
raise SystemExit(1)
PY
local status
status=$?
rm -f "$status_json"
return "$status"
}
apply_tailscale_screen_sharing_connection() {
local peer="${TAILSCALE_SERVER_PEER:-mooses-macbook-server}"
local fallback_ip="${TAILSCALE_SERVER_IP:-100.82.140.87}"
local display_name="${TAILSCALE_SERVER_DISPLAY_NAME:-Moose's MacBook Server}"
local username="${TAILSCALE_SERVER_USERNAME:-moose}"
local peer_ip url app_support_dir app_location_path share_dir share_link launcher pref_path py
app_support_dir="$HOME/Library/Containers/com.apple.ScreenSharing/Data/Library/Application Support/Screen Sharing"
app_location_path="$app_support_dir/$display_name.vncloc"
share_dir="$HOME/.local/share/dotfiles/remote-control"
share_link="$share_dir/Mooses MacBook Server.vncloc"
launcher="$HOME/.local/bin/screen-share-macbook-server"
pref_path="$HOME/Library/Containers/com.apple.ScreenSharing/Data/Library/Preferences/com.apple.ScreenSharing.plist"
peer_ip="$(resolve_tailscale_peer_ip "$peer" 2>/dev/null || true)"
if [[ -z "$peer_ip" ]]; then
peer_ip="$fallback_ip"
warn "Could not resolve $peer from Tailscale; using fallback IP $peer_ip"
fi
url="vnc://$peer"
mkdir -p "$app_support_dir" "$share_dir" "$HOME/.local/bin" "$(dirname "$pref_path")"
for py in python3.14 python3.13 python3.12 python3.11 python3; do
command -v "$py" &>/dev/null && break
py=""
done
if [[ -n "$py" ]]; then
"$py" - "$pref_path" "$app_location_path" "$url" "$display_name" "$peer" "$peer_ip" "$username" <<'PY'