-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1514 lines (1320 loc) · 46.6 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1514 lines (1320 loc) · 46.6 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
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Detect macOS
detect_platform() {
case "$(uname -s)" in
Darwin*)
echo "macos-$(uname -m)"
;;
*)
log_error "This script is for macOS only"
log_error "Unsupported operating system: $(uname -s)"
exit 1
;;
esac
}
# Legacy compatibility function
detect_os() {
echo "macos"
}
# Check if command exists
command_exists() {
command -v "$1" > /dev/null 2>&1
}
# Check package dependencies for specific applications
check_app_dependencies() {
local app_name="$1"
local missing_packages=()
local install_cmd="brew install"
case "$app_name" in
kitty)
command_exists kitty || missing_packages+=("kitty")
;;
ghostty)
command_exists ghostty || missing_packages+=("ghostty")
;;
fish)
command_exists fish || missing_packages+=("fish")
;;
tmux)
command_exists tmux || missing_packages+=("tmux")
;;
zellij)
command_exists zellij || missing_packages+=("zellij")
;;
nvim)
command_exists nvim || missing_packages+=("neovim")
;;
zsh)
command_exists zsh || missing_packages+=("zsh")
;;
rofi)
command_exists rofi || missing_packages+=("rofi")
;;
files)
# File manager options
if ! command_exists finder; then
missing_packages+=("Finder is built-in")
fi
;;
browser)
# Browser options
if ! command_exists firefox && ! command_exists google-chrome && ! command_exists chromium && ! command_exists brave; then
missing_packages+=("firefox or google-chrome or chromium or brave")
fi
;;
esac
# Report missing packages
if [[ ${#missing_packages[@]} -gt 0 ]]; then
log_warning "$app_name has missing dependencies:"
for package in "${missing_packages[@]}"; do
echo " - $package"
done
if [[ -n "$install_cmd" ]]; then
echo " Install with: $install_cmd ${missing_packages[*]}"
fi
echo
return 1
fi
return 0
}
# Validate stow prerequisites
validate_stow_environment() {
local errors=0
log_info "Validating stow environment..."
# Check stow installation
if ! command_exists stow; then
log_error "GNU Stow is not installed"
((errors++))
fi
# Check dotfiles directory structure
cd "$(dirname "$0")" || {
log_error "Cannot access dotfiles directory"
return 1
}
# Check required packages exist
if [[ ! -d "common" ]]; then
log_error "Package directory 'common' not found"
((errors++))
fi
if [[ ! -d "macos" ]]; then
log_error "Package directory 'macos' not found"
((errors++))
fi
# Check write permissions
if [[ ! -w "$HOME" ]]; then
log_error "No write permission to HOME directory: $HOME"
((errors++))
fi
# Check for conflicting management systems
if [[ -d "$HOME/.oh-my-zsh" ]] && [[ -d "common/.config/zsh" || -d "macos/.config/zsh" ]]; then
log_warning "Oh-My-Zsh detected - may conflict with zsh dotfiles"
fi
if [[ -d "$HOME/.vim" ]] && [[ -L "$HOME/.vim" ]] && [[ -d "common/.config/nvim" ]]; then
log_warning "Existing vim setup detected - may conflict with nvim config"
fi
# Ensure Claude directories exist with proper structure
if [[ -d "common/.config/claude" ]] && [[ ! -d "common/.claude" ]]; then
log_info "Setting up Claude configuration structure..."
mkdir -p "common/.claude"
# Copy Claude configs to .claude if they don't exist
for file in common/.config/claude/*; do
if [[ -e "$file" ]]; then
basename_file=$(basename "$file")
if [[ ! -e "common/.claude/$basename_file" ]]; then
cp -r "$file" "common/.claude/$basename_file"
fi
fi
done
# Create any missing subdirectories
for dir in config ide local output-modes; do
mkdir -p "common/.claude/$dir"
done
fi
return $errors
}
# Cross-platform realpath implementation
portable_realpath() {
local path="$1"
if command_exists realpath; then
realpath "$path" 2> /dev/null
elif command_exists greadlink; then
# macOS with GNU coreutils
greadlink -f "$path" 2> /dev/null
else
# macOS native fallback
perl -MCwd -e 'print Cwd::abs_path shift' "$path" 2> /dev/null
fi
}
# Install stow via Homebrew
install_stow() {
if command_exists stow; then
log_info "GNU Stow is already installed"
return 0
fi
log_info "Installing GNU Stow..."
if command_exists brew; then
brew install stow
else
log_info "Homebrew not found. Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH for current session
if [[ -f "/opt/homebrew/bin/brew" ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ -f "/usr/local/bin/brew" ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
if command_exists brew; then
log_success "Homebrew installed successfully"
brew install stow
else
log_error "Failed to install Homebrew. Please install manually: https://brew.sh/"
exit 1
fi
fi
log_success "GNU Stow installed successfully"
}
# Interactive prompts
ask_yes_no() {
local question="$1"
local default="${2:-n}"
local response
while true; do
if [[ "$default" == "y" ]]; then
read -p "$question [Y/n]: " response
response=${response:-y}
else
read -p "$question [y/N]: " response
response=${response:-n}
fi
case "$response" in
[Yy] | [Yy][Ee][Ss])
return 0
;;
[Nn] | [Nn][Oo])
return 1
;;
*)
echo "Please answer yes or no."
;;
esac
done
}
# Interactive backup selection
interactive_backup_choice() {
local files_to_backup=(
".gitconfig"
".zshrc"
".bashrc"
".vimrc"
".tmux.conf"
".config/nvim"
".config/fish"
".config/ghostty"
".config/kitty"
".config/helix"
".config/lazygit"
".config/zellij"
".config/tmux"
".config/mise"
".config/claude"
".claude"
)
local existing_files=()
# Check which files exist
log_info "Checking for existing dotfiles..."
for file in "${files_to_backup[@]}"; do
local full_path="$HOME/$file"
if [[ -e "$full_path" ]] && [[ ! -L "$full_path" ]]; then
existing_files+=("$file")
fi
done
if [[ ${#existing_files[@]} -eq 0 ]]; then
log_info "No existing dotfiles found that would conflict."
return 0
fi
echo ""
log_warning "Found ${#existing_files[@]} existing dotfile(s) that would be overwritten:"
for file in "${existing_files[@]}"; do
echo " - $file"
done
echo ""
# Ask for backup choice
if ask_yes_no "Do you want to backup these files before installation?" "y"; then
return 0 # Yes, backup
else
if ask_yes_no "Are you sure you want to proceed WITHOUT backing up? This may overwrite your existing configs!" "n"; then
return 1 # No backup, but proceed
else
log_info "Installation cancelled by user."
exit 0
fi
fi
}
# Backup existing dotfiles
backup_existing_dotfiles() {
local backup_dir="$HOME/dotfiles-backup-$(date +%Y%m%d-%H%M%S)"
local files_to_backup=(
".gitconfig"
".zshrc"
".bashrc"
".vimrc"
".tmux.conf"
".config/nvim"
".config/fish"
".config/ghostty"
".config/kitty"
".config/helix"
".config/lazygit"
".config/zellij"
".config/tmux"
".config/mise"
".config/claude"
".claude"
)
local backup_created=false
log_info "Checking for existing dotfiles to backup..."
for file in "${files_to_backup[@]}"; do
local full_path="$HOME/$file"
# Check if file/directory exists and is not a symlink managed by stow
if [[ -e "$full_path" ]] && [[ ! -L "$full_path" ]]; then
# Create backup directory on first file found
if [[ "$backup_created" == "false" ]]; then
mkdir -p "$backup_dir"
backup_created=true
log_info "Creating backup directory: $backup_dir"
fi
# Backup the file/directory
local backup_path="$backup_dir/$file"
mkdir -p "$(dirname "$backup_path")"
if [[ -d "$full_path" ]]; then
cp -r "$full_path" "$backup_path"
rm -rf "$full_path"
log_info "Backed up and removed directory: $file"
else
cp "$full_path" "$backup_path"
rm "$full_path"
log_info "Backed up and removed file: $file"
fi
fi
done
if [[ "$backup_created" == "true" ]]; then
log_success "Backup completed: $backup_dir"
log_info "Backed up files are hidden (start with .). Use 'ls -la $backup_dir' to see them."
log_info "You can restore files with: cp -r $backup_dir/.* ~/ (be careful with . and ..)"
else
log_info "No existing dotfiles found to backup"
fi
}
# Interactive package selection
interactive_package_choice() {
local packages_to_install=()
echo "" >&2
log_info "Available package groups for installation:" >&2
echo " 1. common - Cross-platform configs (nvim, fish, git, etc.)" >&2
echo " 2. macos - macOS-specific configs" >&2
echo "" >&2
if ask_yes_no "Install common (cross-platform) configurations?" "y"; then
packages_to_install+=("common")
fi
if ask_yes_no "Install macOS-specific configurations?" "y"; then
packages_to_install+=("macos")
fi
if [[ ${#packages_to_install[@]} -eq 0 ]]; then
log_warning "No packages selected for installation." >&2
if ask_yes_no "Exit without installing anything?" "y"; then
log_info "Installation cancelled by user." >&2
exit 0
else
# Recurse to ask again
interactive_package_choice
return
fi
fi
echo "${packages_to_install[@]}"
}
# Stow packages
stow_packages() {
local skip_backup="${1:-false}"
local interactive="${2:-false}"
local simulate="${3:-false}"
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION MODE: Stowing packages for macOS (dry run)..."
else
log_info "Stowing packages for macOS..."
fi
# Change to dotfiles directory
cd "$(dirname "$0")"
# Validate environment before proceeding (skip in simulation mode)
if [[ "$simulate" != "true" ]]; then
if ! validate_stow_environment; then
log_error "Environment validation failed. Aborting installation."
return 1
fi
fi
# Interactive backup choice if not skipped and interactive mode
if [[ "$skip_backup" != "true" && "$interactive" == "true" && "$simulate" != "true" ]]; then
if interactive_backup_choice; then
backup_existing_dotfiles
fi
elif [[ "$skip_backup" != "true" && "$simulate" != "true" ]]; then
backup_existing_dotfiles
elif [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would backup existing dotfiles (skipped in dry run)"
fi
# Interactive package selection
if [[ "$interactive" == "true" ]]; then
local packages_string
packages_string=$(interactive_package_choice)
local packages=($packages_string)
for package in "${packages[@]}"; do
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would stow $package configurations..."
stow -t "$HOME" -nv --ignore='.*\.DS_Store.*' "$package"
else
log_info "Stowing $package configurations..."
stow -t "$HOME" -v --ignore='.*\.DS_Store.*' --adopt "$package"
fi
done
else
# Non-interactive: install both common and macOS-specific
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would stow common configurations..."
stow -t "$HOME" -nv --ignore='.*\.DS_Store.*' common
log_info "SIMULATION: Would stow macOS-specific configurations..."
stow -t "$HOME" -nv --ignore='.*\.DS_Store.*' macos
else
log_info "Stowing common configurations..."
stow -t "$HOME" -v --ignore='.*\.DS_Store.*' --adopt common
log_info "Stowing macOS-specific configurations..."
stow -t "$HOME" -v --ignore='.*\.DS_Store.*' --adopt macos
fi
fi
log_success "All selected packages stowed successfully!"
# Run stow verification check
local script_dir="$(dirname "$0")"
if [[ -x "$script_dir/scripts/check-stow.sh" && "$simulate" != "true" ]]; then
echo ""
log_info "Running stow verification check..."
"$script_dir/scripts/check-stow.sh" || true # Don't fail install on check failure
fi
# Setup fish plugins if fish config was stowed
if [[ "$simulate" != "true" ]]; then
if [[ -f "$HOME/.config/fish/fish_plugins" ]] && command_exists fish; then
echo ""
log_info "Setting up fish plugins..."
setup_fish_plugins || {
log_warning "Fish plugins setup failed. You can run it manually later: $script_dir/scripts/setup-fish-plugins.sh"
}
fi
fi
}
# Helper function to detect and clean orphaned dotfiles symlinks
cleanup_orphaned_symlinks() {
log_info "Cleaning up orphaned dotfiles symlinks..."
local orphans_found=false
local dotfiles_dir="$(pwd)"
# Function to check if a symlink points to our dotfiles directory
is_dotfiles_symlink() {
local symlink="$1"
local target resolved_target
target=$(readlink "$symlink" 2> /dev/null) || return 1
# Convert to absolute path if relative
if [[ "$target" == /* ]]; then
# Already absolute
resolved_target="$target"
else
# Convert relative to absolute using our portable function
resolved_target="$(cd "$(dirname "$symlink")" 2> /dev/null && portable_realpath "$target")" || return 1
fi
# Check if target is within our dotfiles directory
[[ "$resolved_target" == "$dotfiles_dir"* ]]
}
# Find and clean orphaned symlinks in common locations
local search_paths=(
"$HOME/.config"
"$HOME"
)
for search_path in "${search_paths[@]}"; do
if [[ -d "$search_path" ]]; then
while IFS= read -r -d '' symlink; do
if is_dotfiles_symlink "$symlink" && [[ ! -e "$symlink" ]]; then
log_info "Removing orphaned symlink: $symlink"
rm "$symlink" 2> /dev/null || true
orphans_found=true
fi
done < <(find "$search_path" -maxdepth 3 -type l -print0 2> /dev/null)
fi
done
# Clean up empty directories
find "$HOME/.config" -type d -empty 2> /dev/null | while read -r empty_dir; do
if [[ "$empty_dir" != "$HOME/.config" ]]; then
log_info "Removing empty directory: $empty_dir"
rmdir "$empty_dir" 2> /dev/null || true
fi
done
if [[ "$orphans_found" == "false" ]]; then
log_info "No orphaned dotfiles symlinks found"
fi
}
# Enhanced unstow packages with fallback cleanup
unstow_packages() {
log_info "Unstowing packages for macOS..."
cd "$(dirname "$0")"
# First, try regular stow unstow for packages that exist
local unstow_success=true
# Unstow in reverse order with verbose output and explicit target
log_info "Unstowing macOS-specific configurations..."
if [[ -d "macos" ]]; then
if stow -t "$HOME" -Dv --ignore='.*\.DS_Store.*' macos 2> /dev/null; then
log_success "macOS configurations unstowed successfully"
else
log_warning "Some macOS configurations may not have been fully unstowed"
unstow_success=false
fi
else
log_warning "Package directory 'macos' not found, skipping stow unstow"
unstow_success=false
fi
log_info "Unstowing common configurations..."
if [[ -d "common" ]]; then
if stow -t "$HOME" -Dv --ignore='.*\.DS_Store.*' common 2> /dev/null; then
log_success "Common configurations unstowed successfully"
else
log_warning "Some common configurations may not have been fully unstowed"
unstow_success=false
fi
else
log_warning "Package directory 'common' not found, skipping stow unstow"
unstow_success=false
fi
# Fallback: Clean up any orphaned symlinks that stow couldn't handle
if [[ "$unstow_success" == "false" ]]; then
log_info "Running fallback cleanup for orphaned symlinks..."
cleanup_orphaned_symlinks
fi
log_success "Unstow process completed!"
}
# Get available apps for stowing
get_available_apps() {
local apps=()
# Check common apps
if [[ -d "common/.config" ]]; then
for app_dir in common/.config/*/; do
if [[ -d "$app_dir" ]]; then
local app_name=$(basename "$app_dir")
apps+=("$app_name")
fi
done
fi
# Check macOS-specific apps
if [[ -d "macos/.config" ]]; then
for app_dir in macos/.config/*/; do
if [[ -d "$app_dir" ]]; then
local app_name=$(basename "$app_dir")
apps+=("$app_name")
fi
done
fi
# Remove duplicates and sort
printf '%s\n' "${apps[@]}" | sort -u
}
# Validate app name
validate_app() {
local app_name="$1"
local available_apps
available_apps=($(get_available_apps))
for available_app in "${available_apps[@]}"; do
if [[ "$available_app" == "$app_name" ]]; then
return 0
fi
done
return 1
}
# Stow a specific app
stow_app() {
local app_name="$1"
local simulate="${2:-false}"
# Change to dotfiles directory
cd "$(dirname "$0")"
# Check package dependencies before stowing
if [[ "$simulate" != "true" ]]; then
if ! check_app_dependencies "$app_name"; then
log_warning "Consider installing missing dependencies before using $app_name"
fi
fi
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would stow $app_name configuration..."
else
log_info "Stowing $app_name configuration..."
fi
local stowed=false
# Helper function to create symlink directly for individual app
stow_app_config() {
local package="$1"
local simulate_flag="$2"
local app_config_path="$package/.config/$app_name"
local target_path="$HOME/.config/$app_name"
local dotfiles_dir="$(pwd)"
if [[ ! -d "$app_config_path" ]]; then
return 1
fi
# Calculate relative path from target to source
local relative_path="../.dotfiles/$app_config_path"
if [[ "$simulate_flag" == "true" ]]; then
log_info "SIMULATION: Would create symlink $target_path -> $relative_path"
else
# Create .config directory if it doesn't exist
mkdir -p "$(dirname "$target_path")"
# Remove existing file/directory if it exists
if [[ -e "$target_path" ]] || [[ -L "$target_path" ]]; then
if [[ -d "$target_path" ]] && [[ ! -L "$target_path" ]]; then
log_info "Backing up existing directory: $target_path -> ${target_path}_backup_$(date +%Y%m%d_%H%M%S)"
mv "$target_path" "${target_path}_backup_$(date +%Y%m%d_%H%M%S)"
else
log_info "Removing existing file/symlink: $target_path"
rm -rf "$target_path"
fi
fi
# Create the symlink
ln -s "$relative_path" "$target_path"
log_info "LINK: .config/$app_name -> $relative_path"
fi
return 0
}
# Check if app exists in common
if [[ -d "common/.config/$app_name" ]]; then
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would stow common/$app_name..."
stow_app_config "common" "true"
else
log_info "Stowing common/$app_name..."
stow_app_config "common" "false"
fi
stowed=true
fi
# Check if app exists in macOS-specific (will override common if exists)
if [[ -d "macos/.config/$app_name" ]]; then
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would stow macos/$app_name..."
stow_app_config "macos" "true"
else
log_info "Stowing macos/$app_name..."
stow_app_config "macos" "false"
fi
stowed=true
fi
if [[ "$stowed" == "true" ]]; then
log_success "$app_name configuration stowed successfully!"
else
log_error "$app_name configuration not found for macOS"
return 1
fi
}
# Unstow a specific app
unstow_app() {
local app_name="$1"
local simulate="${2:-false}"
# Change to dotfiles directory
cd "$(dirname "$0")"
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would unstow $app_name configuration..."
else
log_info "Unstowing $app_name configuration..."
fi
local unstowed=false
# Remove specific app symlinks from common
if [[ -d "common/.config/$app_name" ]]; then
local target_path="$HOME/.config/$app_name"
if [[ -L "$target_path" ]]; then
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would remove $target_path"
else
log_info "Removing symlink: $target_path"
rm "$target_path"
fi
unstowed=true
fi
fi
# Remove specific app symlinks from macOS-specific
if [[ -d "macos/.config/$app_name" ]]; then
local target_path="$HOME/.config/$app_name"
if [[ -L "$target_path" ]]; then
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would remove $target_path"
else
log_info "Removing symlink: $target_path"
rm "$target_path"
fi
unstowed=true
fi
fi
# Handle special macOS apps with root-level configs
case "$app_name" in
tmux)
# Special handling for tmux which has multiple symlinks
local tmux_files=("$HOME/.tmux.conf" "$HOME/.tmux.conf.local")
for tmux_file in "${tmux_files[@]}"; do
if [[ -L "$tmux_file" ]]; then
if [[ "$simulate" == "true" ]]; then
log_info "SIMULATION: Would remove $tmux_file"
else
log_info "Removing symlink: $tmux_file"
rm "$tmux_file"
fi
unstowed=true
fi
done
;;
esac
if [[ "$unstowed" == "true" ]]; then
log_success "$app_name configuration unstowed successfully!"
else
log_warning "$app_name configuration was not found or already unstowed for macOS"
# Check if config exists in filesystem and suggest manual removal
local config_exists=false
local manual_remove_paths=()
# Check common config location
if [[ -d "$HOME/.config/$app_name" ]] && [[ ! -L "$HOME/.config/$app_name" ]]; then
config_exists=true
manual_remove_paths+=("$HOME/.config/$app_name")
fi
if [[ "$config_exists" == "true" ]]; then
log_info "Found non-symlink $app_name configuration in your home directory."
log_info "To manually remove config files, run:"
for path in "${manual_remove_paths[@]}"; do
if [[ -d "$path" ]]; then
echo " rm -rf \"$path\""
else
echo " rm \"$path\""
fi
done
fi
# Suggest package removal via Homebrew
local pkg_name=""
# Map app names to package names
case "$app_name" in
waybar) pkg_name="waybar" ;;
nvim) pkg_name="neovim" ;;
fish) pkg_name="fish" ;;
tmux) pkg_name="tmux" ;;
zellij) pkg_name="zellij" ;;
kitty) pkg_name="kitty" ;;
ghostty) pkg_name="ghostty" ;;
rofi) pkg_name="rofi" ;;
esac
if [[ -n "$pkg_name" ]]; then
if command_exists brew; then
echo ""
log_info "To remove the $app_name package, run:"
echo " brew uninstall $pkg_name"
fi
fi
fi
}
# Show usage
show_usage() {
echo "Usage: $0 [install|uninstall|restow|stow-app|unstow-app|tools|fonts|fish|fish-plugins|zellij|mcp|submodules|all|backup|cleanup|status]"
echo ""
echo "Commands:"
echo " install - Install dotfiles only (default)"
echo " uninstall [app] - Remove all dotfiles symlinks or specific app (e.g., nvim)"
echo " restow - Remove and reinstall dotfiles"
echo " stow-app <app> - Stow a specific app configuration (e.g., tmux, nvim)"
echo " unstow-app <app> - Unstow a specific app configuration"
echo " tools - Install development tools with mise"
echo " fonts - Install Maple Mono Nerd Font and Font Awesome"
echo " fish - Install Fish shell and set as default shell"
echo " fish-plugins - Setup Fish shell plugins (Fisher and Pure theme)"
echo " zellij - Install Zellij terminal multiplexer"
echo " mcp - Setup MCP servers for Claude"
echo " submodules - Update git submodules"
echo " all - Install dotfiles, tools, and update submodules"
echo " backup - Backup existing dotfiles only"
echo " cleanup - Remove orphaned dotfiles symlinks"
echo " status - Show current dotfiles installation status"
echo ""
echo "Options:"
echo " --with-tools - Install tools along with dotfiles"
echo " --update-subs - Update submodules along with dotfiles"
echo " --no-backup - Skip backing up existing dotfiles"
echo " --interactive - Interactive mode with prompts for choices"
echo " --simulate - Dry run - show what would be done without doing it"
echo ""
echo "Examples:"
echo " $0 stow-app tmux # Stow only tmux configuration"
echo " $0 uninstall nvim # Remove nvim configuration symlinks"
echo " $0 unstow-app tmux # Remove tmux configuration symlinks"
echo " $0 stow-app nvim --simulate # Preview nvim stowing without changes"
echo " $0 uninstall nvim --simulate # Preview nvim unstowing without changes"
echo ""
echo "This script will automatically detect macOS and install appropriate configs."
echo "By default, existing dotfiles are backed up before installation."
echo "Use --interactive for guided installation with user prompts."
echo "Use --simulate to preview changes before applying them."
}
# Install fonts required for terminal applications
install_fonts() {
log_info "Installing required fonts..."
# Check if Homebrew is available
if ! command_exists brew; then
log_error "Homebrew is required for font installation on macOS"
return 1
fi
# Install Maple Mono Nerd Font
if brew list --cask font-maple-mono-nf > /dev/null 2>&1; then
log_info "Maple Mono Nerd Font is already installed"
else
log_info "Installing Maple Mono Nerd Font via Homebrew..."
if brew install --cask font-maple-mono-nf; then
log_success "Maple Mono Nerd Font installed successfully"
else
log_error "Failed to install Maple Mono Nerd Font via Homebrew"
return 1
fi
fi
# Install Font Awesome
if brew list --cask font-fontawesome > /dev/null 2>&1; then
log_info "Font Awesome is already installed"
else
log_info "Installing Font Awesome via Homebrew..."
if brew install --cask font-fontawesome; then
log_success "Font Awesome installed successfully"
else
log_warning "Failed to install Font Awesome"
fi
fi
}
# Install development tools
install_tools() {
log_info "Installing development tools..."
local script_dir="$(dirname "$0")"
if [[ -x "$script_dir/scripts/install-tools.sh" ]]; then
"$script_dir/scripts/install-tools.sh"
else
log_error "install-tools.sh script not found or not executable"
return 1
fi
}
# Install Fish shell
install_fish() {
# Check if fish is already installed
if command_exists fish; then
log_info "Fish shell is already installed"
else
log_info "Installing Fish shell..."
if command_exists brew; then
brew install fish
else
log_error "Homebrew is not installed. Please install Homebrew first:"
log_error " /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
log_error "Then run: brew install fish"
return 1
fi
log_success "Fish shell installed successfully"
fi
# Change default shell to fish
local fish_path
fish_path=$(command -v fish)
if [[ -n "$fish_path" ]]; then
# Check if fish is in /etc/shells
if ! grep -q "^$fish_path$" /etc/shells 2> /dev/null; then
log_info "Adding fish to /etc/shells..."
echo "$fish_path" | sudo tee -a /etc/shells > /dev/null
fi
# Check if fish is already the default shell
if [[ "$SHELL" == "$fish_path" ]]; then
log_info "Fish is already the default shell"
else
log_info "Changing default shell to fish..."
if chsh -s "$fish_path"; then
log_success "Default shell changed to fish"
log_info "Please restart your terminal or log out and back in for the change to take effect"
else
log_warning "Failed to change default shell. You can manually run: chsh -s $fish_path"
fi
fi
else
log_error "Fish shell not found in PATH"
return 1
fi
log_info "Fish plugins will be setup after dotfiles are stowed"
}
# Setup fish plugins after dotfiles installation
setup_fish_plugins() {
log_info "Setting up fish plugins..."