-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·425 lines (371 loc) · 12.7 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·425 lines (371 loc) · 12.7 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
#!/usr/bin/env bash
set -e
DOTFILES_DIR="$HOME/.dotfiles"
BACKUP_SUFFIX=".bak"
DRY_RUN=0
NON_INTERACTIVE=0
print_usage() {
cat <<EOF
Usage: $0 [OPTION]
Options:
-n, --dry-run Show what would happen without making changes
-y, --yes Run non-interactively and apply changes
-h, --help Show this help message
If no option is provided, the script will ask whether to run a dry run
or apply changes.
EOF
}
run_cmd() {
if [ "$DRY_RUN" -eq 1 ]; then
printf ' WOULD run:'
printf ' %q' "$@"
printf '\n'
else
"$@"
fi
}
for arg in "$@"; do
case "$arg" in
-n | --dry-run)
DRY_RUN=1
;;
-y | --yes)
NON_INTERACTIVE=1
;;
-h | --help)
print_usage
exit 0
;;
*)
echo "Error: unknown argument: $arg" >&2
print_usage >&2
exit 1
;;
esac
done
echo "================================================"
echo " Dotfiles Installation Script"
echo "================================================"
echo ""
if [ ! -d "$DOTFILES_DIR" ]; then
echo "Error: $DOTFILES_DIR not found" >&2
exit 1
fi
if ! command -v stow &>/dev/null; then
echo "Error: GNU Stow is not installed" >&2
echo "Install it with:"
echo " macOS: brew install stow"
echo " Arch: sudo pacman -S stow"
echo " Debian: sudo apt install stow"
exit 1
fi
if [ "$NON_INTERACTIVE" -eq 0 ] && [ "$DRY_RUN" -eq 0 ]; then
echo "Choose mode:"
echo " 1) Dry run (show what would happen)"
echo " 2) Apply changes"
echo ""
echo -n "Select an option (1-2, default 1): "
read -r mode_choice
case "${mode_choice:-1}" in
1)
DRY_RUN=1
;;
2)
DRY_RUN=0
;;
*)
echo "Invalid option. Defaulting to dry run."
DRY_RUN=1
;;
esac
echo ""
fi
if [ "$DRY_RUN" -eq 1 ]; then
echo "DRY RUN MODE: no filesystem changes will be made"
echo ""
fi
cd "$DOTFILES_DIR" || exit 1
echo "Step 1: Analyzing .dotfiles structure..."
# Build explicit packages array.
# Include .opencode if present, but exclude .git.
packages=()
while IFS= read -r dir; do
dir_name=$(basename "$dir")
if [ "$dir_name" != ".git" ] && [ -d "$dir_name" ]; then
packages+=("$dir_name")
fi
done < <(find . -maxdepth 1 -mindepth 1 -type d ! -name '.git' | sort)
if [ ${#packages[@]} -eq 0 ]; then
echo "Error: no stow packages found in $DOTFILES_DIR" >&2
exit 1
fi
echo " Detected ${#packages[@]} package(s): ${packages[*]}"
# Detect base directories
base_dirs=()
for package in "${packages[@]}"; do
while IFS= read -r -d '' subdir; do
dirname=$(basename "$subdir")
if [[ "$dirname" =~ ^\. ]] || [ "$dirname" = "Library" ]; then
if [[ ! " ${base_dirs[*]} " =~ $dirname ]]; then
base_dirs+=("$dirname")
fi
fi
done < <(find "$package" -mindepth 1 -maxdepth 1 -type d -print0)
done
if [ ${#base_dirs[@]} -eq 0 ]; then
echo " No base directories detected"
else
echo " Detected base directories: ${base_dirs[*]}"
fi
echo ""
echo "Step 2: Creating base directories..."
for dir in "${base_dirs[@]}"; do
target="$HOME/$dir"
if [ ! -d "$target" ]; then
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD create $target"
else
mkdir -p "$target"
echo " ✓ Created $target"
fi
else
echo " - Skipping $target (already exists)"
fi
done
echo ""
echo "Step 3: Checking for conflicts..."
conflicts=()
stow_output=$(stow --simulate --restow "${packages[@]}" 2>&1 || true)
if echo "$stow_output" | grep -q "existing target is"; then
while IFS= read -r line; do
if [[ "$line" =~ existing\ target\ is ]]; then
conflict_file=$(echo "$line" | sed -E 's/.*existing target is (neither a link nor a directory|not owned by stow): (.*)/\2/')
if [ -n "$conflict_file" ]; then
conflicts+=("$conflict_file")
fi
fi
done <<<"$stow_output"
fi
# Separate conflicts into files and directories
file_conflicts=()
dir_conflicts=()
for conflict in "${conflicts[@]}"; do
if [ -f "$HOME/$conflict" ] && [ ! -L "$HOME/$conflict" ]; then
file_conflicts+=("$conflict")
elif [ -d "$HOME/$conflict" ] && [ ! -L "$HOME/$conflict" ]; then
dir_conflicts+=("$conflict")
fi
done
if [ ${#file_conflicts[@]} -eq 0 ] && [ ${#dir_conflicts[@]} -eq 0 ]; then
echo " No conflicts detected"
else
echo " Found ${#conflicts[@]} conflict(s)"
fi
echo ""
if [ ${#file_conflicts[@]} -gt 0 ] || [ ${#dir_conflicts[@]} -gt 0 ]; then
echo "Step 4: Handling conflicts..."
if [ ${#file_conflicts[@]} -gt 0 ]; then
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD back up ${#file_conflicts[@]} conflicting file(s):"
else
echo " Backing up ${#file_conflicts[@]} conflicting file(s)..."
fi
for conflict in "${file_conflicts[@]}"; do
backup="$HOME/${conflict}${BACKUP_SUFFIX}"
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD move $HOME/$conflict -> $backup"
else
mv "$HOME/$conflict" "$backup"
echo " ✓ Moved $conflict → ${conflict}${BACKUP_SUFFIX}"
fi
done
fi
if [ ${#dir_conflicts[@]} -gt 0 ]; then
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD skip ${#dir_conflicts[@]} conflicting directory(s):"
else
echo " ⚠️ Skipping ${#dir_conflicts[@]} conflicting directory(s):"
fi
for conflict in "${dir_conflicts[@]}"; do
echo " - $conflict (directory conflicts require manual review)"
done
echo " Note: Directories are not automatically backed up for safety."
fi
echo ""
fi
echo "Step 5: Running stow..."
if [ "$DRY_RUN" -eq 1 ]; then
printf ' WOULD run:'
printf ' %q' stow --restow "${packages[@]}"
printf '\n'
echo " ✓ Dry run completed successfully"
else
if stow --restow "${packages[@]}" 2>&1; then
echo " ✓ Stow completed successfully"
else
echo " ✗ Stow encountered errors" >&2
exit 1
fi
fi
echo ""
echo "================================================"
if [ "$DRY_RUN" -eq 1 ]; then
echo " Dry Run Complete!"
else
echo " Installation Complete!"
fi
echo "================================================"
echo ""
echo "Summary:"
echo " - Base directories detected: ${#base_dirs[@]}"
echo " - Packages processed: ${#packages[@]}"
if [ ${#file_conflicts[@]} -gt 0 ]; then
if [ "$DRY_RUN" -eq 1 ]; then
echo " - Conflicting files that would be backed up: ${#file_conflicts[@]}"
else
echo " - Conflicting files backed up: ${#file_conflicts[@]}"
fi
fi
if [ ${#dir_conflicts[@]} -gt 0 ]; then
echo " - Conflicting directories skipped: ${#dir_conflicts[@]}"
fi
echo ""
if [ "$DRY_RUN" -eq 1 ]; then
echo "No changes were made."
else
echo "Backup files (if any) are saved with '$BACKUP_SUFFIX' extension"
echo "You can now start a new shell or run: exec zsh"
fi
echo ""
echo "Would you like to run the troubleshooting menu? (y/N)"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
while true; do
echo ""
echo "================================================"
echo " Troubleshooting Menu"
echo "================================================"
echo ""
echo " 1) Fix Tmux Plugin Manager (TPM)"
echo " 2) Reinstall all Tmux plugins"
echo " 3) Reset Tmux configuration completely"
echo " 4) Exit"
echo ""
echo -n "Select an option (1-4): "
read -r choice
case $choice in
1)
echo ""
echo "Fixing Tmux Plugin Manager..."
if [ -d "$HOME/.tmux/plugins/tpm" ]; then
echo " - TPM directory already exists"
else
echo " - Installing TPM..."
run_cmd git clone https://github.com/tmux-plugins/tpm "$HOME/.tmux/plugins/tpm"
[ "$DRY_RUN" -eq 1 ] || echo " ✓ TPM installed"
fi
if [ -f "$HOME/.config/tmux/tmux.conf" ]; then
echo " - Reloading tmux configuration..."
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD run: tmux source-file $HOME/.config/tmux/tmux.conf"
else
tmux source-file "$HOME/.config/tmux/tmux.conf" 2>/dev/null && echo " ✓ Configuration reloaded" || echo " ! No active tmux session"
fi
fi
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD create $HOME/.tmux_tpm_setup_complete"
else
touch "$HOME/.tmux_tpm_setup_complete"
echo " ✓ TPM setup marker created"
fi
echo ""
echo "To install plugins, either:"
echo " - Press Ctrl+A then I (inside tmux)"
echo " - Run: ~/.tmux/plugins/tpm/bin/install_plugins"
;;
2)
echo ""
echo "Reinstalling all Tmux plugins..."
if [ ! -d "$HOME/.tmux/plugins/tpm" ]; then
echo " ! TPM not found. Please run option 1 first."
else
echo " - Cleaning old plugins..."
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD remove directories under $HOME/.tmux/plugins except tpm"
else
find "$HOME/.tmux/plugins" -mindepth 1 -maxdepth 1 -type d ! -name 'tpm' -exec rm -rf {} +
fi
echo " - Installing plugins..."
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD run: $HOME/.tmux/plugins/tpm/bin/install_plugins"
else
"$HOME/.tmux/plugins/tpm/bin/install_plugins"
echo " ✓ Plugins reinstalled"
fi
if [ -f "$HOME/.config/tmux/tmux.conf" ]; then
echo " - Reloading configuration..."
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD run: tmux source-file $HOME/.config/tmux/tmux.conf"
else
tmux source-file "$HOME/.config/tmux/tmux.conf" 2>/dev/null && echo " ✓ Configuration reloaded" || echo " ! No active tmux session"
fi
fi
fi
;;
3)
echo ""
echo "⚠️ WARNING: This will remove ALL tmux plugins and reinstall from scratch"
echo -n "Are you sure? (y/N): "
read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo " - Removing all plugin directories..."
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD remove $HOME/.config/tmux/plugins and $HOME/.tmux/plugins"
else
rm -rf "$HOME/.config/tmux/plugins" "$HOME/.tmux/plugins" 2>/dev/null
fi
echo " - Creating fresh plugin directory..."
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD create $HOME/.tmux/plugins"
else
mkdir -p "$HOME/.tmux/plugins"
fi
echo " - Cloning TPM..."
run_cmd git clone https://github.com/tmux-plugins/tpm "$HOME/.tmux/plugins/tpm"
echo " - Installing plugins..."
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD run: $HOME/.tmux/plugins/tpm/bin/install_plugins"
else
"$HOME/.tmux/plugins/tpm/bin/install_plugins"
fi
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD create $HOME/.tmux_tpm_setup_complete"
else
touch "$HOME/.tmux_tpm_setup_complete"
echo " ✓ Complete reset finished"
fi
if [ -f "$HOME/.config/tmux/tmux.conf" ]; then
echo " - Reloading configuration..."
if [ "$DRY_RUN" -eq 1 ]; then
echo " WOULD run: tmux source-file $HOME/.config/tmux/tmux.conf"
else
tmux source-file "$HOME/.config/tmux/tmux.conf" 2>/dev/null && echo " ✓ Configuration reloaded" || echo " ! Start a new tmux session to apply changes"
fi
fi
else
echo " Cancelled."
fi
;;
4)
echo ""
echo "Exiting troubleshooting menu."
break
;;
*)
echo ""
echo "Invalid option. Please select 1-4."
;;
esac
done
fi
echo ""